我试图获取权限文件和文件夹,并导出到csv文件。我可以将信息显示在屏幕上,但是当我尝试导出时,生成的csv文件是空的。无法在PowerShell中导出csv,为空的CSV文件
代码:
function Test-IsWritable(){
<#
.Synopsis
Command tests if a file is present and writable.
.Description
Command to test if a file is writeable. Returns true if file can be opened for write access.
.Example
Test-IsWritable -path $foo
Test if file $foo is accesible for write access.
.Example
$bar | Test-IsWriteable
Test if each file object in $bar is accesible for write access.
.Parameter Path
Psobject containing the path or object of the file to test for write access.
#>
[CmdletBinding()]
param([Parameter(Mandatory=$true,ValueFromPipeline=$true)][psobject]$path)
process{
Write-Host "Test if file $path is writeable"
if (Test-Path -Path $path -PathType Any){
$target = Get-Item $path -Force
try{
$writestream = $target.Openwrite()
$writestream.Close() | Out-Null
Remove-Variable -Name writestream
Write-Host "File is writable" -ForegroundColor DarkGreen
Write-Output $true
}
catch{
Write-Host "File is not writable" -ForegroundColor DarkRed
Write-Output $false
}
Remove-Variable -Name target
}
else{
Write-Host "File $path does not exist or is a directory" -ForegroundColor Red
Write-Output $false
}
}
}
write-host "WARNING: If checking deep folders (where the full path is longer than 248 characters) please " -foregroundcolor Yellow -NoNewline
Write-Host "MAP THE DRIVE " -ForegroundColor Red -NoNewline
Write-Host "in order to keep the names as short as possible" -ForegroundColor Yellow
$basefolder = Read-Host -Prompt 'What is the folder or files you want to get permissions of?'
write-host "WARNING: if permissions.csv already exists, it will be overwritten!" -foregroundcolor Yellow
Write-Host 'Export results to CSV? (y/n): ' -ForegroundColor Magenta -NoNewline
$export = Read-Host
if ($export -like "y")
{
Write-Host "Name the file (ex: permissions.csv): " -ForegroundColor Magenta -NoNewline
$FileName = Read-Host
$Outfile = “$PSScriptRoot\$FileName”
write-host "Will write results to $PSScriptRoot\$FileName" -ForegroundColor Green
}
else
{
write-host "User did not type 'y', continuing" -ForegroundColor DarkYellow
}
$files = get-childitem $basefolder -recurse -File
Write-Host $files
Write-Host "=========================" -ForegroundColor Black
#$subfiles = Get-ChildItem $folders -Recurse -File
#Write-Host $folders
#Write-Host "=========================" -ForegroundColor Black
#Write-Host $subfiles
$results = foreach($folder in $files) {
New-Object psobject -Property @{
File = $folder;
Access = "$basefolder\$folder" | Test-IsWritable
}
Write-Host $folder
}
#$subresults = foreach($subfile in $subfiles) {
# New-Object psobject -Property @{
# File = $subfiles;
# Access = $subfile | Test-IsWritable;
# }
#}
Write-Host $results
Write-Host "Finished combo loop, exporting..." -ForegroundColor Green
$results | Export-Csv $Outfile -NoTypeInformation -Delimiter ";"
Write-Host "Converting delimited CSV to Column Excel Spreadsheet"
$outputXLSX = $PSScriptRoot + "\$Filename.xlsx"
$excel = New-Object -ComObject excel.application
$workbook = $excel.Workbooks.Add(1)
$worksheet = $workbook.worksheets.Item(1)
$TxtConnector = ("TEXT;" + $Outfile)
$Connector = $worksheet.QueryTables.add($TxtConnector,$worksheet.Range("A1"))
$query = $worksheet.QueryTables.item($Connector.name)
$query.TextFileOtherDelimiter = ';'
$query.TextFileParseType = 1
$query.TextFileColumnDataTypes = ,2 * $worksheet.Cells.Columns.Count
$query.AdjustColumnWidth = 1
$query.Refresh()
$query.Delete()
$Workbook.SaveAs($outputXLSX,51)
$excel.Quit()
Remove-Item $Outfile
Write-Host "See $PSScriptRoot\$Filename.xlsx for results" -ForegroundColor Green
UPDATE:主要是工作,奇怪的输出虽然:
Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE1.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ FILE2.TXT Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar Z:\ testfolder \ file3.rar 指定的路径,文件名或二者都太长 长。完全限定的文件名必须少于260个字符, 和目录名称必须少于248个字符。
在下一列:
的FileAccess FullControl FullControl FullControl 修改,同步 ReadAndExecute,同步 修改,同步 修改,同步 FullControl FullControl FullControl 修改,同步 ... 规格ified路径,文件名或两者太长。完整 限定文件名必须少于260个字符,并且 目录名称必须少于248个字符。
我不知道为什么它显示同一个文件多行,我想每个文件有一行与真正的文件访问。
感谢您的快速回答,虽然当我这样做时,我只在CSV中获得一个条目,因为我基本上替换了以前的迭代CSV文件。我现在使用'Add-Content',它显示的很好,但没有内置的分隔符。有任何想法吗? – Austin
是的,我编辑了我的答案来解决这个问题。 – sodawillow
再次感谢您,我做了一些操作并更新了上面的代码,感谢您的代码,我使用分隔的CSV文件制作了一个柱状的电子表格。最后一个问题是为什么当我测试文件和文件夹时,我获得了单个文件的多个权限,如上所示 – Austin