2016-05-17 34 views
1

我有下面的powershell腳本,它給了我一個特定文件夾的權限。如何使用PowerShell循環訪問驅動器中的所有目錄

在下面的腳本,例如我可以得到每個文件夾(包括子文件夾)的權限命名SP目錄,

如何可以遍歷所有文件夾,而不是具體的targetdirectory中?

$targetServer = '\\tst-server\f$\' #Enter hostname 
$targetDirectory = 'SP' #Enter directory name 
$target   = Join-Path -ChildPath $targetDirectory -Path $targetServer 
$arrResults  = @() #Initialize array used to store custom object output 
$exportPath  = 'C:\Users\adt\Desktop\Scripts\output\FolderPermission.csv' #Enter name of the CSV output file 

#Query target directory for all 'folders' (excludes files via Where statement) 
Get-ChildItem -Recurse -Path $target | Where { $_.PSIsContainer } | 
forEach { 
    $objPath = $_.FullName 
    $coLACL = Get-Acl -Path $objPath 
    forEach ($objACL in $colACL) { 
     forEach ($accessRight in $objACL.Access) { 
      $objResults = New-Object –TypeName PSObject 
      $objResults | Add-Member –MemberType NoteProperty –Name DirectoryPath  –Value $objPath 
      $objResults | Add-Member –MemberType NoteProperty –Name Identity   –Value $accessRight.IdentityReference 
      $objResults | Add-Member –MemberType NoteProperty –Name SystemRights  –Value $accessRight.FileSystemRights 
      $objResults | Add-Member –MemberType NoteProperty –Name SystemRightsType –Value $accessRight.AccessControlType 
      $objResults | Add-Member -MemberType NoteProperty -Name IsInherited  -Value $accessRight.IsInherited 
      $objResults | Add-Member -MemberType NoteProperty -Name InheritanceFlags -Value $accessRight.InheritanceFlags 
      $objResults | Add-Member –MemberType NoteProperty –Name RulesProtected  –Value $objACL.AreAccessRulesProtected 
      $arrResults += $objResults 
     } 
    } 
} 

$arrResults | Export-CSV -NoTypeInformation -Path $exportPath 

回答

1

你只需要設置PathF:

Get-ChildItem F: -Recurse | Where { $_.PSIsContainer } | Foreach-Object { 
    #do something 
} 
+0

感謝Jisaak,讓我給一個嘗試。 –

+0

只需將#'目標目錄下的兩行代碼替換爲我發佈的腳本 –

相關問題