2017-08-31 25 views
0

我正在開發一個腳本來比較兩個不同文件位置之間的訪問權限。我的問題類似於this question about System.Collections,因爲我將信息記錄到散列表,但它不以可讀方式打印到文件。如何將Powershell ACL信息以散列形式很好地打印到文件

這裏是我的代碼

Get-ChildItem -path $location1 -recurse | ForEach{$original_file_permissions[$_.name] = (Get-ACL $_.fullname).Access; Write-Host Parsing $_.fullname} 
$original_file_permissions.getenumerator() | Format-Table | Out-File "U:\file_folder\sub_folder\original_file_permissions.txt 

我的代碼分析文件的位置,使用Get-ACL方法來捕獲該文件或文件夾的訪問細節,把它作爲哈希表中的值,其中關鍵是文件或文件夾標題。然而,散列打印到的文件不打印確切的細節,它相反,打印,我相信,一個系統類?我不完全確定。

這裏是什麼文件看起來像

sys.a90      {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                     
pickering-app_beta_02.hex  {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                     
release_notes.txt    {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule}                                     
126037AA      {System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule, System.Security.AccessControl.FileSystemAccessRule} 

然而,當我運行PowerShell的終端相同的命令和訪問的關鍵,我感到我正在尋找的ACL信息。

PS C:\file_folder\sub_folder> $hash["126037AA"] 


FileSystemRights : FullControl 
AccessControlType : Allow 
IdentityReference : user123 
IsInherited  : True 
InheritanceFlags : ContainerInherit, ObjectInherit 
PropagationFlags : None 

FileSystemRights : FullControl 
AccessControlType : Allow 
IdentityReference : admin 
IsInherited  : True 
InheritanceFlags : ContainerInherit, ObjectInherit 
PropagationFlags : None       
... 

爲什麼會發生這種情況?我該如何做到這一點,以便哈希中的ACL細節是打印到文件的內容?

+0

您需要訪問您要打印的成員,否則它會將對象類型打印到文件中。 – TheIncorrigible1

+0

好吧,它打印對象類型。 .GetEnumerator()是否不訪問散列表的每個成員? – gsamerica

+0

不,它與輸入'$ Hash'基本相同,除非你不能通過'.GetEnumerator()來訪問成員。「# – TheIncorrigible1

回答

2

您正試圖將對象輸出到flatfile(txt文件),因此acl對象正在寫入類型。換句話說,out-file不知道如何編寫acl對象,所以我們需要將對象轉換爲字符串,然後輸出文件。

$original_file_permissions.getenumerator() | 
    Foreach-Object { 
    '{0}{1}' -f $_.key, ($_.Value | out-string) 
    } | out-file c:\temp\acl.txt 

$original_file_permissions.getenumerator() | 
    Foreach-Object { 
    $_.Value | Add-Member Noteproperty Name $_.key -PassThru -Force  
    } | Format-Table | Out-File c:\temp\acl.txt 

你可以寫ACL信息爲CSV,但你將需要投入更多的工作。

另請注意,我刪除了format-table - 這是因爲format-table更改了對象類型,通常只用於在控制檯中顯示內容。

+0

非常感謝。另一個答案不適合我,因爲我在格式表中保留了格式表,認爲這是必要的。奇怪的是,它通常只在控制檯中使用,我已經在其他許多答案中看到了它。 – gsamerica

+0

歡迎...看起來out-file理解來自格式表的對象,這意味着我們可以改進輸出:)...請參閱編輯...但是格式表輸出格式對象不能與其他cmdlet一起玩......但out-file似乎工作 – Kiran

0

System.Security.AccessControl.DirectorySecurity有一個ScriptPropertyAccesstostring。你可以使用它,但你仍然有問題,你有一個文件對象,但一個或多個訪問條目。這不適合你的文本輸出。

我想你將不得不下達到每個訪問規則,並添加文件名或你需要什麼信息。

$items = Get-ChildItem 
foreach ($item in $items) { 
    $acl = Get-Acl -Path $item.FullName # Before edit this was $items.FullName 
    foreach ($access in $acl.Access) { 
     [PSCustomObject]@{ 
      Name    = $item.Name 
      FullName   = $item.FullName 
      FileSystemRights = $access.FileSystemRights 
      AccessControlType = $access.AccessControlType 
      IdentityReference = $access.IdentityReference    
     } 
    } 
} 

該對象可以很容易地在文本文件中輸出爲csv或打印格式表。每個訪問規則將是一行。