我有一个PowerShell脚本,它基本上是一个迁移数据的队列。基本上,脚本会通过问卷调查,并根据问卷调查,创建批量命令。所有这些工作都很好,它正在执行我遇到问题的迁移。
在示例中,我一次迁移9个LUN。一旦unix系统报告这9个已完成(而不是正在进行零迁移),它将删除旧LUN并启动新批处理直到完成。
这是我认为我坚持的代码:
function Execute_AddVdisk {
$FileCount = 1
$vDiskNames = Get-Content $WorkingDir\vDiskNames.txt
$date = Get-Date
Write-Output "$date - vDisk copy starting..." | Out-File $WorkingDir\log.txt -Encoding Ascii -Append
Write-Output "`n" | Out-File $WorkingDir\log.txt -Encoding ascii -Append
Execute_AddVdisk-Continue
}
function Execute_AddVdisk-Continue {
if (Test-Path $WorkingDir\migrate_vdisks-$FileCount.txt) {
$date = Get-Date
Write-Host "Adding vDisk copies. Executing file $WorkingDir\migrate_vdisks-$FileCount.txt"
Write-Output "$date - Executing file $WorkingDir\migrate_vdisks-$FileCount.txt" | Out-File $WorkingDir\log.txt -Encoding Ascii -Append
CMD.EXE /C "ssh user@$svc -i PathToKey -m $WorkingDir\migrate_vdisks-$FileCount.txt"
Delete_vDisks
} else {
Write-Host "All migrations are complete. Exiting."
Send-MailMessage -From "someone@somewhere.com" -To "someone@somewhere.com" -Subject "Something" -Body "Some Stuff" -Attachments "somelog" -SmtpServer somemailserver
Set-Location C:\
Remove-Item -Path $WorkingDir -Recurse
exit
}
}
function Delete_vDisks {
$SessionCopies = @()
$AllvDiskCopies = CMD.EXE /C "ssh user@$svc -i PathToKey lsvdisksyncprogress -delim ," | ConvertFrom-Csv
if ($AllvDiskCopies -eq $null) {
$date = Get-Date
Write-Host "Deleting vDisk copies. Executing file $WorkingDir\Remove_vDiskCopies-$FileCount.txt"
Write-Output "$date - Executing file $WorkingDir\Remove_vDiskCopies-$FileCount.txt" | Out-File $WorkingDir\log.txt -Encoding ascii -Append
CMD.EXE /C "ssh user@$svc -i PathToKey -m $WorkingDir\Remove_vDiskCopies-$FileCount.txt"
$FileCount++
Execute_AddVdisk-Continue
} else {
foreach ($vDiskCopy in $AllvDiskCopies.vdisk_name) {
$SessionCopies += $vDiskNames | Where-Object {$_ -like $vDiskCopy}
}
if ($SessionCopies -eq $null) {
$date = Get-Date
Write-Host "Deleting vDisk copies. Executing file $WorkingDir\Remove_vDiskCopies-$FileCount.txt"
Write-Output "$date - Executing file $WorkingDir\Remove_vDiskCopies-$FileCount.txt" | Out-File $WorkingDir\log.txt -Encoding Ascii -Append
CMD.EXE /C "ssh user@$svc -i PathToKey -m $WorkingDir\Remove_vDiskCopies-$FileCount.txt"
$FileCount++
Execute_AddVdisk-Continue
} else {
$date = Get-Date
$SessionCopiesCount = $SessionCopies.Count
Write-Host "$SessionCopiesCount copies are still being synched. Sleeping for 60 seconds and checking again..."
Write-Output "$date - $SessionCopiesCount copies are still being synched. Sleeping for 60 seconds and checking again..." | Out-File $WorkingDir\log.txt -Encoding ascii -Append
Start-Sleep -Seconds 60
Delete_vDisks
}
}
}
这是日志的截断版本:
04/05/2017 10:24:01 - Executing file C:\scripts\SVC_Data_Migrate\05Apr2017-1021\migrate_vdisks-1.txt 04/05/2017 10:24:08 - 9 copies are still being synched. Sleeping for 60 seconds and checking again... 04/05/2017 10:34:16 - 2 copies are still being synched. Sleeping for 60 seconds and checking again... 04/05/2017 10:35:17 - Executing file C:\scripts\SVC_Data_Migrate\05Apr2017-1021\Remove_vDiskCopies-1.txt 04/05/2017 10:35:21 - Executing file C:\scripts\SVC_Data_Migrate\05Apr2017-1021\migrate_vdisks-2.txt 04/05/2017 10:35:27 - 9 copies are still being synched. Sleeping for 60 seconds and checking again... 04/05/2017 11:43:26 - 1 copies are still being synched. Sleeping for 60 seconds and checking again... 04/05/2017 11:44:27 - Executing file C:\scripts\SVC_Data_Migrate\05Apr2017-1021\Remove_vDiskCopies-2.txt 04/05/2017 11:44:31 - Executing file C:\scripts\SVC_Data_Migrate\05Apr2017-1021\migrate_vdisks-3.txt 04/05/2017 11:44:40 - 9 copies are still being synched. Sleeping for 60 seconds and checking again... 04/05/2017 11:58:52 - 9 copies are still being synched. Sleeping for 60 seconds and checking again...
在这种特殊情况下,我有10' migrate_vdisks-x.txt'文件。从日志中,您可以看到if
/ else
语句正在运行。它等待直到找不到任何活动的迁移,删除旧的LUN,然后找到下一个“migrate_vdisks-x.txt”#39;文件存在,执行迁移,并等待它们完成等等。
所有这些说法,这个特定的运行在上午11:58:52停止报告。日志中没有更多条目,脚本永远不会完成。此脚本作为任务运行,任务的状态为“正在运行”。我首先想到可能ssh.exe
(只是plink
重命名)被挂起或类似的东西,但当我在服务器上搜索正在运行的进程时,我看不到ssh
个进程。 / p>
我不知道if / else循环如何在几次迭代中起作用,然后只是停滞不动而没有做任何事情。任何想法/建议总是受到赞赏。
此外,而不是:
if ($AllvDiskCopies -eq $null)
我也尝试过计数,因为它是一个数组,我最终得到了相同的结果。
if ($AllvDiskCopies.Count -eq 0)