首先,我是Powershell的新手,所以请原谅我这里编码不好的原因。
我正在尝试编写一个对命令进行排队的脚本。我们有一台设备一次只能处理32个命令。我想要做的是构建一个具有超过32个命令的命令文件,然后它会在32之后自动对任何命令进行排队,直到打开另一个空闲槽。除非我进入“其他”声明,否则一切似乎都在起作用。我尝试将当前的$ _添加回数组中,这样它就不会丢失并重新处理,但这似乎不起作用。
我从文本文件构建我的数组:
$commands = @(Get-Content C:\scripts\temp\commands.txt)
当我尝试在'else'暂停语句期间将当前$ _值添加回数组时,它永远不会重新添加,因此一个特定的命令永远不会返回到数组中。因此,如果我的脚本必须暂停15次,那么15个命令将永远不会被运行,因为它们只是被'else'语句处理并被抛出我的'if / else'循环。
这是我的if / else循环:
$commands | ForEach-Object {
If (Test-Path C:\scripts\temp\active_migrations.txt){
Remove-Item C:\scripts\temp\active_migrations.txt
}
CMD.EXE /C "ssh $user@$cluster -pw $PlainPassword lsmigrate" > C:\scripts\temp\active_migrations.txt
$count = Get-Content C:\scripts\temp\active_migrations.txt
$number_of_migrations = $count.Length / 6
IF ($number_of_migrations -lt 32)
{
Write-Host "Migrations are currently at $number_of_migrations. Proceeding with next vdisk migration."
Write-Host "Issuing command $_"
Write-Host "There are still $migrations_left migrations to execute of $total_migrations total."
Write-Host ""
CMD.EXE /C "ssh $user@$cluster -pw $PlainPassword $_"
SLEEP 2
$migrations_left--
}
ELSE
{
Write-Host "Migrations are currently at $number_of_migrations. Sleeping for 1 minute"
$commands = @($commands + $_)
Write-Host "There are still $migrations_left migrations to execute of $total_migrations total."
SLEEP 60
}}
}
非常感谢任何帮助。
答案 0 :(得分:0)
谢谢保罗。我最终在else语句中将一些东西转储到临时数组,它可以工作!感谢您的帮助。
function commands_temp
{
if (Test-Path C:\scripts\temp\commands_temp.txt)
{
$commands=@()
$commands=@(Get-Content C:\scripts\temp\commands_temp.txt)
Remove-Item C:\scripts\temp\commands_temp.txt
Proceed
}
Else
{
Write-Host "All migrations are either complete or successfully executed. Check the GUI or run lsmigrate CLI command for full status."