无法在powershell中导出csv,空CSV文件

时间:2016-06-27 21:58:43

标签: csv powershell permissions export

我正在尝试获取权限文件和文件夹,并导出到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

更新:主要是工作,奇怪的输出:

  

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   完全控制   完全控制   完全控制   修改,同步   ReadAndExecute,Synchronize   修改,同步   修改,同步   完全控制   完全控制   完全控制   修改,同步   ...   指定的路径,文件名或两者都太长。完全   限定文件名必须少于260个字符,并且   目录名称必须少于248个字符。

我不确定为什么它会为同一个文件显示多行,我希望每个文件有一行,并且具有真正的文件访问权限。

1 个答案:

答案 0 :(得分:1)

在使用Write-Host之前删除Export-CsvWrite-Host使用管道中的数据,只在屏幕上输出。

#(...)

$i = 0

$results = foreach($acl in $acls) {
    $folder = (Convert-Path $acl.pspath)
    Write-Progress -Activity "Getting Security" -Status "checking $folder" -PercentComplete ($i / $folders.Count * 100)

    foreach($access in $acl.GetAccessRules($true, $true, [System.Security.Principal.SecurityIdentifier])) {
        New-Object psobject -Property @{
            Folder = $folder;
            User = $acl.Owner;
            Group=$acl.Group;
            Mode = $access.AccessControlType;
            FileAcess = $access.FileSystemRights;
        }
    }
    $i++
}

Write-Host "Reached End, exporting..." -ForegroundColor Green
$results | Export-Csv $Outfile -NoTypeInformation -Delimiter ";"