我希望通过查看server.txt文件中的服务器来运行代码,而不是每次在根路径中输入servername,因为我想在20多台服务器上运行:
代码如下:
$ServerList = Get-Content "servers.txt"
$OutFile = "C:\Users\munjanga\Documents\AoN Project\Execute\$([Environment]::MachineName).txt"
$Header = "Account,Ace String,Object Path"
Del $OutFile
Add-Content -Value $Header -Path $OutFile
ForEach($Server in $ServerList) {
$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 | Select -Skip 1 | % {$_ -replace '"', ""} | Out-File $OutFile -Force -Encoding ascii -Append}
答案 0 :(得分:0)
你可以将整个事情包裹在Get-Content | ForEach-Object
中,如下所示:
Get-Content servers.txt | ForEach-Object {
$RootPath = $_
#Your code
}