脚本启动脚本-限制并发脚本的数量

时间:2019-04-25 12:10:20

标签: powershell

我正在尝试启动许多并发的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
    }
}

1 个答案:

答案 0 :(得分:-1)

我使用了以下简单的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 ###
################################################