2014-03-06 203 views
0

我試圖使用Get-ACL函數來掃描目錄中的子文件夾。我正在修改我在這裏找到的工作腳本,將輸出轉換爲.csv文件。如果我可以得到輸出,那麼將會很好,因此每個文件夾的每行都包含全部IdentityReference值。但是我最好的嘗試導致與此錯誤:格式化Get-ACL輸出

Method invocation failed because [System.Security.Principal.NTAccount] does not contain a method named 'op_Addition'.

下面是代碼:

$OutFile = "C:\Admin\Permissions.csv" 
$Header = "FolderPath,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags" 
Del $OutFile 
Add-Content -Value $Header -Path $OutFile 

$RootPath = "I:\Users\ES" 

$Folders = GCI $RootPath\* | where {$_.psiscontainer -eq $true} 

foreach ($Folder in $Folders){ 
    $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access } 
    Foreach ($ACL in $ACLs){ 
     $OutIR = $ACL.IdentityReference 
     $OutIR += "," 
    } 
    $OutInfo = $Folder.Fullname + "," + $OutIR + "," + $ACL.AccessControlType + "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + "," + $ACL.PropagationFlags 
    Add-Content -Value $OutInfo -Path $OutFile 
} 

我試圖用變量$OutIR趕在年底全部IdentityReferences,然後添加$OutIR的結果。每個文件夾將被列出並在同一行上,所有IdentityReference將被顯示。


已更新>非常感謝所有幫助到目前爲止!兩個代碼都可以工作(真棒!),但我真的很想調整輸出。順便說一句,這是我第一次在這裏尋求幫助,它真的讓真正的腳本編寫者看着這個!

我在Windows 2012 R2機器上使用Powershell 4.0。

我得到了輸出的一個例子是:

"I:\Users\ES\aalvarez","NT AUTHORITY\SYSTEM","Allow","False","ContainerInherit, ObjectInherit","None" 
"I:\Users\ES\aalvarez","BUILTIN\Administrators","Allow","False","ContainerInherit, ObjectInherit","None" 
"I:\Users\ES\aalvarez","XXX\aralvare","Allow","False","ContainerInherit, ObjectInherit","None" 

我想有輸出是:

"I:\Users\ES\aalvarez","NT AUTHORITY\SYSTEM","BUILTIN\Administrators","XXX\aralvare","Allow","False","ContainerInherit, ObjectInherit","None" 

的想法是,我可以把我的1000+用戶在電子表格中快速比較文件夾ACL。再次感謝!

+0

嘗試所有的輸出變量轉換爲字符串,即'$ Folder.Fullname.ToString()' – slybloty

+0

什麼版本的PowerShell您使用? – mjolinor

+0

你能提供一個你想要輸出看起來像什麼的例子嗎? – mjolinor

回答

0

我看到mjolinor已經評論過了,他可能有更好的東西,因爲他很棒,但是如果是我,我會這麼做。我喜歡有一個對象來處理所有事情的說法和完成情況,以便以後我想查詢它時,我已經可以重新使用它了。然後,我們可以將其導出爲CSV格式,而不是像您在做的那樣艱難。

  • 創建一個空數組。
  • 獲取您的ACL,並通過它們循環有點像你現在。
  • 使用您希望在輸出文件中的屬性爲每個ACL創建一個PSCustomObject。
  • 將該對象添加到您之前創建的數組中。
  • 完成所有操作後,將數組導出到文件。

下面的代碼,將做到這一點:

$OutFile = "C:\Admin\Permissions.csv" 
$ACLList [email protected]() 
Del $OutFile 
$RootPath = "I:\Users\ES" 

$Folders = GCI $RootPath\* | where {$_.psiscontainer -eq $true} 

foreach ($Folder in $Folders){ 
    $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access } 
    Foreach ($ACL in $ACLs){ 
    $OutInfo = New-Object -TypeName psobject -Property @{ 
     FolderPath = $Folder.Fullname 
     IdentityReference = $ACL.IdentityReference.ToString() 
     AccessControlType = $ACL.AccessControlType.ToString() 
     IsInherited = $ACL.IsInherited 
     InheritanceFlags = $ACL.InheritanceFlags 
     PropagationFlags = $ACL.PropagationFlags} 
    $ACLList+=$OutInfo 
    } 
} 
$ACLList|select FolderPath,IdentityReference,AccessControlType,IsInherited,InheritanceFlags,PropagationFlags|export-csv C:\temp\Permissions.csv -NoTypeInformation 
1

你沒有得到你期待的結果,因爲您使用的內環的循環變量外的那循環。

更改此:

foreach ($Folder in $Folders){ 
    $ACLs = get-acl $Folder.fullname | ForEach-Object { $_.Access } 
    Foreach ($ACL in $ACLs){ 
    $OutIR = $ACL.IdentityReference 
    $OutIR += "," 
    } 
    $OutInfo = $Folder.Fullname + "," + $OutIR + "," + $ACL.AccessControlType + 
      "," + $ACL.IsInherited + "," + $ACL.InheritanceFlags + 
      "," + $ACL.PropagationFlags 
    Add-Content -Value $OutInfo -Path $OutFile 
}

成這樣:

$Folders | % { 
    $fpath = $_.FullName 
    Get-Acl $fpath | select -Expand Access | % { 
    $fpath + "," + $_.IdentityReference + "," + $_.AccessControlType + "," + 
     $_.IsInherited + "," + $_.InheritanceFlags + "," + $_.PropagationFlags 
    } 
} | Add-Content -Path $OutFile 

既然你要創建逗號分隔的輸出,你可以完全避免人工字符串連接使用ConvertTo-Csv cmdlet,並計算特性對於文件夾名稱:

$Folders | % { 
    $fpath = $_.FullName 
    Get-Acl $fpath | select -Expand Access | 
    select @{n='FolderName';e={$fpath}}, IdentityReference, AccessControlType, 
     IsInherited, InheritanceFlags, PropagationFlags 
} | ConvertTo-Csv -NoType | select -Skip 1 | Add-Content -Path $OutFile 

如果您不介意標題行,你也可以直接將數據導出爲CSV:

$Folders | % { 
    ... 
} | Export-Csv $OutFile -NoType