我正在尝试启动许多并发的PowerShell脚本。在Windows会话锁定或断开连接之前,以下逻辑运行正常。一旦发生这种情况,脚本就会毫不留情地启动每个脚本,而不管进程数是多少。
foreach ($Script in $Scripts) {
if ((Get-Process powershell -ErrorAction SilentlyContinue).Count -lt 10) {
Start-Process powershell.exe -ArgumentList "-File `"$Script`""
Start-Sleep -s 5
} else {
while ((Get-Process powershell -ErrorAction SilentlyContinue).Count -lt 10) {
Start-Sleep -s 30
}
Start-Process powershell.exe -ArgumentList "-File `"$Script`""
Start-Sleep -s 5
}
}发布于 2019-04-25 21:07:37
我使用下面的简单powershell代码来运行文件夹中的脚本。你可以修改它
################################################
### Auto-load scripts on PowerShell startup ###
# directory where my scripts are stored
################################################
$psdir="C:\work\JumpStart\Autoload"
Write-Host "Custom PowerShell Environment Start Loading"
Write-Host " "
# load all 'autoload' scripts
Get-ChildItem "${psdir}\*.ps1" | %{.$_}
Get-ChildItem "${psdir}\*.ps1" | %{Write-host $_}
Write-Host " "
Write-Host "Custom PowerShell Environment Loaded"
################################################
### Auto-load scripts on PowerShell startup ###
################################################https://stackoverflow.com/questions/55849024
复制相似问题