下面是我擁有的代碼,它在為單個服務器運行時完美執行
$Hostname = $env:COMPUTERNAME
$CsvData = Import-Csv -Path "C:\Ansible\status_report_2021.csv" | Where-Object{$_.ServerName -eq $Hostname} | Select-Object SystemFolderPath
foreach($path in $CsvData)
{
$path = $path.SystemFolderPath
$path = $path.trim('\')
# break inheritance on the folder and copy ACEs as uninherited
icacls $path /inheritance:d
#remove all BUILTIN\Users granted ACEs
icacls $path /remove:g BUILTIN\Users
#grant only BUILTIN\Users Read&Execute. avoid using (S,GE,GR) = RX.
#(S,GE,GR) is a specific right and icacls would create 2 ACEs.
#same meaning but if we can avoid it's better
icacls $path /grant:r "BUILTIN\Users:(OI)(CI)RX"
#remove SYSTEM
icacls $path /remove:g "NT AUTHORITY\SYSTEM"
#grant SYSTEM as Full control on "this folder, subfolder and files"
icacls $path /grant:r "NT AUTHORITY\SYSTEM:(OI)(CI)F"
icacls $path
}
請讓我知道如何為遠程服務器執行這些 icacls 命令。
uj5u.com熱心網友回復:
我想您可以使用 的組合Group-Object將所有服務器名稱及其SystemFolderPath在 csv 中的條目組合在一起并遍歷這些組。
在回圈內部Invoke-Command用于讓icacls命令在每個服務器上執行。
就像是
Import-Csv -Path "C:\Ansible\status_report_2021.csv" | Group-Object ServerName | ForEach-Object {
$server = $_.Name
foreach ($path in ($_.Group.SystemFolderPath | Select-Object -Unique)) {
Invoke-Command -ComputerName $server -ScriptBlock {
param ([string]$path)
# break inheritance on the folder and copy ACEs as uninherited
icacls $path /inheritance:d
# remove all BUILTIN\Users granted ACEs
icacls $path /remove:g BUILTIN\Users
# grant only BUILTIN\Users Read&Execute. avoid using (S,GE,GR) = RX.
# (S,GE,GR) is a specific right and icacls would create 2 ACEs.
# same meaning but if we can avoid it's better
icacls $path /grant:r "BUILTIN\Users:(OI)(CI)RX"
# remove SYSTEM
icacls $path /remove:g "NT AUTHORITY\SYSTEM"
# grant SYSTEM as Full control on "this folder, subfolder and files"
icacls $path /grant:r "NT AUTHORITY\SYSTEM:(OI)(CI)F"
icacls $path
} -ArgumentList $path.Trim('\')
}
}
uj5u.com熱心網友回復:
首先按服務器對所有 CSV 條目進行分組 - 這樣我們可以一次將所有受影響的路徑發送到每個單獨的服務器:
$PathsPerServer = Import-Csv -Path "C:\Ansible\status_report_2021.csv" | Group-Object ServerName
現在我們可以Invoke-Command針對每個不同的服務器使用并為每個相關路徑執行 icacls 陳述句:
$PathsPerServer | ForEach-Object {
# enumerate all the paths for this server, we need to pass them as arguments to Invoke-Command
$paths = $_.Group | Select-Object -Expand SystemFolderPath | ForEach-Object Trim
Invoke-Command -ComputerName $_.Name -ScriptBlock {
param([string[]]$Paths)
foreach ($path in $Paths) {
# break inheritance on the folder and copy ACEs as uninherited
icacls $path /inheritance:d
#remove all BUILTIN\Users granted ACEs
icacls $path /remove:g BUILTIN\Users
#grant only BUILTIN\Users Read&Execute. avoid using (S,GE,GR) = RX.
#(S,GE,GR) is a specific right and icacls would create 2 ACEs.
#same meaning but if we can avoid it's better
icacls $path /grant:r "BUILTIN\Users:(OI)(CI)RX"
#remove SYSTEM
icacls $path /remove:g "NT AUTHORITY\SYSTEM"
#grant SYSTEM as Full control on "this folder, subfolder and files"
icacls $path /grant:r "NT AUTHORITY\SYSTEM:(OI)(CI)F"
icacls $path
}
} -ArgumentList $paths
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/402176.html
