Powershell脚本输出csv文件重构

时间:2014-06-23 23:35:49

标签: powershell

我有一个powershell脚本,它在服务器中运行并获取安全权限,并在csv中输出。目前它输出信息但是它读取的每个文件都包括标题Account,Ace String和Object Path:我如何删除所以它只在文档的开头显示它?

帐户,Ace字符串,对象路径 NT AUTHORITY \ SYSTEM,允许FullControl,(继承),C:\ Users \ munjanga \ Documents \ Operations Orchestration \ jetty BUILTIN \ Administrators,允许FullControl,(继承),C:\ Users \ munjanga \ Documents \ Operations Orchestration \ jetty EMEA \ munjanga,允许FullControl,(继承),C:\ Users \ munjanga \ Documents \ Operations Orchestration \ jetty 帐户,Ace字符串,对象路径 NT AUTHORITY \ SYSTEM,允许FullControl,(继承),C:\ Users \ munjanga \ Documents \ Operations Orchestration \ jre1.6

    $OutFile = "C:\Users\munjanga\Documents\AoN Project\Execute\$([Environment]::MachineName).txt"  
$Header = "Folder Path,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags"
Del $OutFile
Add-Content -Value $Header -Path $OutFile 

$RootPath = "C:\Users\munjanga\Documents\Operations Orchestration"

$Folders = dir $RootPath -recurse | where {$_.psiscontainer -eq $true}

$isInherited = @{
 $true  = 'Inherited'
 $false = 'Not Inherited'
}

$inheritance = @{
 0 = 'files only'
 1 = 'this folder and subfolders'
 2 = 'this folder and files'
  3 = 'subfolders and files'
}

$fldr = $Folder.FullName

$Folders | % {
$fldr = $_.FullName
Get-Acl $fldr | select -Expand Access |
 select @{n='Account';e={$_.IdentityReference}},
     @{n='Ace String';e={"{0} {1}, {2} ({3})" -f $_.AccessControlType,
       $_.FileSystemRights, $inheritance[$_.InheritanceFlags],
       $isInherited[$_.IsInherited]}},
    @{n='Object Path';e={$fldr}} | ConvertTo-Csv -NoTypeInformation | % {$_ -replace '"', ""} | Out-File $OutFile -Force -Encoding ascii -Append}

1 个答案:

答案 0 :(得分:0)

让我们看看这是否有效:

@{n='Object Path';e={$fldr}} | ConvertTo-Csv -NoTypeInformation | Select -Skip 1 | % {$_ -replace '"', ""} | Out-File $OutFile -Force -Encoding ascii -Append

关键位是Select -Skip 1之后的ConvertTo-Csv

修改

经过多思考之后,这里有一个更好的答案。最后一行应该是这样的:

@{n='Object Path';e={$fldr}}} | ConvertTo-Csv -NoTypeInformation | % {$_ -replace '"', ""} | Out-File $OutFile -Force -Encoding ascii -Append

在转换为CSV之前,应该关闭该循环