2014-01-10 6 views
0

먼저 Powershell을 처음 사용합니다.배열에 요소를 다시 추가하는 방법

명령을 대기시키는 스크립트를 작성하려고합니다. 한 번에 32 개의 명령 만 처리 할 수있는 장비가 있습니다. 32 개 이상의 명령이있는 명령 파일을 작성한 다음 다른 빈 슬롯이 열릴 때까지 32 이후의 명령을 자동으로 대기시킵니다. 'else'문으로 들어가는 것을 제외하고는 모든 것이 작동하는 것 같습니다. 나는 배열에 현재 $ _를 다시 추가하려고 시도하여 잃어 버리지 않고 재 처리되지만, 작동하지 않는 것 같습니다.

나는 텍스트 파일에서 내 배열을 구축 : $commands = @(Get-Content C:\scripts\temp\commands.txt)

나는 '다른'일시 정지 문 중에 배열에 다시 _ 값을 현재 $를 추가하려고, 그것은 하나, 그래서 다시 추가하지 특정 명령은 결코 어레이에 다시 들어 가지 않습니다. 따라서 내 스크립트가 15 번 중단되어야한다면 15 개의 명령은 '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 [email protected]$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 [email protected]$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

여기서 실제로 어레이에서 명령을 제거합니까? 값에 액세스했기 때문에 값이 삭제되지 않는다는 뜻이 아닙니다. – Paul

+0

명시 적으로 제거하지 않습니다. 어쩌면 내 질문에 약간 이상한 말을했을 수도있다. 기본적으로 배열에 300 개의 값이 있으면 300 개의 모든 값이 스크립트가 일시 중지되어야하는지 여부를 'CMD.EXE/C "ssh $ user @ $ cluster -pw $ PlainPassword $ _"' '실행해야합니다. . 값이 현재 "안녕하세요 세상"이라고 해두겠습니다. 현재 32 개의 명령이 실행 중이므로 스크립트가 else 루프로 떨어집니다. 내가해야 할 일은 실행 명령이 32보다 작아지면 여전히 "hello world"가 실행되어야한다는 것입니다. 그게 도움이 되니? 죄송합니다 ... 이것은 수치가 추가 된 경우 – firestarter247

+0

배열을 점검했는지 설명하기가 힘듭니다. 루프가 이미 시작 되었기 때문에 아마도 루프를 통과하지 못했을 것입니다. 어쩌면 두 번째 배열을 만들어 명령을 추가 한 다음 첫 번째 루프가 끝난 후 순환시킬 수 있습니까? – Paul

답변

0

폴 감사합니다. 나는 temp 배열에 else 문을 내면서 작업을 끝내고 작동합니다! 도움을 감사하십시오.

function commands_temp 
{ 
if (Test-Path C:\scripts\temp\commands_temp.txt) 
{ 
[email protected]() 
[email protected](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." 
관련 문제