运行 并行作业能力 shell
Run Parallel jobs power shell
你好 PowerShell 大师
我制作此脚本是为了在远程 PC 上安装最新的 KB 补丁。它现在正在工作。 AL 虽然很粗糙,但我只是把它放在一起(在学习 PS 时)。它完成了工作……非常缓慢但有效。它需要 2:40 小时到 运行 并检查 128 台电脑。我在其他帖子中看到为此创建并行作业可能会有所帮助。我不知道如何创建并行作业,请帮助我。
我的脚本
Start-Transcript -Path "$(Get-location)\RESULTLOG-$(Get-date -Format "yyyyMMddTHHmmss").log"
Function Get-FileName{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = Get-Location
$OpenFileDialog.filter = “All files (*.*)| *.*”
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$Importfile = Get-FileName
$Clients = Get-Content $Importfile
Foreach($Client in $Clients){
#CHECK IF PC IS ENABLED IN AD <<<<feature improvement
# if(get-adcomputer -Filter 'Name -like $p' -Properties Enabled)
if (Test-Connection -ComputerName $Client -Count 2 -Quiet) {
try {
Get-HotFix -ComputerName $Client -Description 'Security Update' | Select-Object -Last 1
}
catch {
"$Client;An error occurred."
}
}
else{
"$Client;not on line "
}
}
Stop-Transcript
我还尝试在 运行 剩余代码之前检查 Active Directory 中是否启用了 PC,但同时我已经注释掉了我得到的工作。
这方面的性能可以提高吗??我知道网速可能有关系,但每台电脑的数据量应该不会这么长
看一下ForEach-Object的“-Parallel”开关:
https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/
您可能需要稍微更改您的代码以使其使用 ForEach-Object 调用而不是 foreach 块:
...
$Clients | ForEach-Object {
if (Test-Connection -ComputerName $_ -Count 2 -Quiet) {
try {
Get-HotFix -ComputerName $_ -Description 'Security Update' |
Select-Object -Last 1
}
catch {
"$_;An error occurred."
}
}
else {
"$_;not on line "
}
} -Parallel
...
编辑:正如@doug-maurer 在评论中以及我提供的 link 中所提到的,这仅适用于 PowerShell v7-Preview-3 及更高版本。
你好 PowerShell 大师
我制作此脚本是为了在远程 PC 上安装最新的 KB 补丁。它现在正在工作。 AL 虽然很粗糙,但我只是把它放在一起(在学习 PS 时)。它完成了工作……非常缓慢但有效。它需要 2:40 小时到 运行 并检查 128 台电脑。我在其他帖子中看到为此创建并行作业可能会有所帮助。我不知道如何创建并行作业,请帮助我。
我的脚本
Start-Transcript -Path "$(Get-location)\RESULTLOG-$(Get-date -Format "yyyyMMddTHHmmss").log"
Function Get-FileName{
[System.Reflection.Assembly]::LoadWithPartialName(“System.windows.forms”) | Out-Null
$OpenFileDialog = New-Object System.Windows.Forms.OpenFileDialog
$OpenFileDialog.initialDirectory = Get-Location
$OpenFileDialog.filter = “All files (*.*)| *.*”
$OpenFileDialog.ShowDialog() | Out-Null
$OpenFileDialog.filename
}
$Importfile = Get-FileName
$Clients = Get-Content $Importfile
Foreach($Client in $Clients){
#CHECK IF PC IS ENABLED IN AD <<<<feature improvement
# if(get-adcomputer -Filter 'Name -like $p' -Properties Enabled)
if (Test-Connection -ComputerName $Client -Count 2 -Quiet) {
try {
Get-HotFix -ComputerName $Client -Description 'Security Update' | Select-Object -Last 1
}
catch {
"$Client;An error occurred."
}
}
else{
"$Client;not on line "
}
}
Stop-Transcript
我还尝试在 运行 剩余代码之前检查 Active Directory 中是否启用了 PC,但同时我已经注释掉了我得到的工作。
这方面的性能可以提高吗??我知道网速可能有关系,但每台电脑的数据量应该不会这么长
看一下ForEach-Object的“-Parallel”开关: https://devblogs.microsoft.com/powershell/powershell-foreach-object-parallel-feature/
您可能需要稍微更改您的代码以使其使用 ForEach-Object 调用而不是 foreach 块:
...
$Clients | ForEach-Object {
if (Test-Connection -ComputerName $_ -Count 2 -Quiet) {
try {
Get-HotFix -ComputerName $_ -Description 'Security Update' |
Select-Object -Last 1
}
catch {
"$_;An error occurred."
}
}
else {
"$_;not on line "
}
} -Parallel
...
编辑:正如@doug-maurer 在评论中以及我提供的 link 中所提到的,这仅适用于 PowerShell v7-Preview-3 及更高版本。