【发布时间】:2014-06-23 23:35:49
【问题描述】:
我有一个 powershell 脚本,可以在服务器中运行并获取安全权限,并将这些脚本输出到 csv 中。目前它正在输出信息,但它读取的每个文件都再次包含标题 Account、Ace String 和 Object Path:我如何删除它只在文档开头显示它?
帐户、Ace 字符串、对象路径 NT AUTHORITY\SYSTEM,Allow FullControl, (Inherited),C:\Users\munjanga\Documents\Operations Orchestration\jetty BUILTIN\Administrators,Allow FullControl, (Inherited),C:\Users\munjanga\Documents\Operations Orchestration\jetty EMEA\munjanga,Allow FullControl, (Inherited),C:\Users\munjanga\Documents\Operations Orchestration\jetty 帐户、Ace 字符串、对象路径 NT AUTHORITY\SYSTEM,Allow FullControl, (Inherited),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}
【问题讨论】:
-
你现在从两个不同的地方得到不同的标题。
Add-Content行在文件顶部添加$Header值,然后ConvertTo-Csv正在添加来自Get-Acl的附加标头及后续。你两个都想要吗?还是只是顶部的标题? -
我只想保留文件顶部的标题,怎么做?它只是删除 convertTocsv 吗?
标签: powershell