[英]Powershell script to list last reboot of vm's under a Azure Subscription
[英]How to reboot a azure vm from powershell script and continue the remaining steps
我有一个天蓝色的 VM。 我正在编写一个执行两个 .bat 文件的 powershell 脚本,然后它必须重新启动 azure VM,然后它必须继续执行剩余的 .bat 文件。
这是下面的脚本
步骤 1: set-location -path C:/temp
Step2: start-process cmd.exe /c "abc.bat" -wait
Step3: start-process cmd.exe /c "123.bat" -wait
Step4: restart-computer -Force
Step5: start-process cmd.exe /c "prod.bat" -wait
Step6: start-process cmd.exe /c "dev.bat" -wait
通过给出以下命令远程执行此脚本
PS C:\> Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath '/Users/username/sample.ps1'
运行上述命令后,它执行到“第 4 步”为止。之后它不会继续执行剩余的步骤
你能帮我解决这个问题吗?
我可以想到两种方法,并在下面详细介绍它们:
使用注册表项RUNONCE - 可用于指定系统将执行一次然后删除的命令。
此处提供更多信息。
远程运行时会稍作修改。
当您使用Invoke-AzVMRunCommand
,初始命令必须将 2 个脚本文件复制到远程 VM 中的临时位置并启动第一个脚本(这包含步骤 1-3)
在第 4 步之前 - 您将在 RUNONCE 下创建一个注册表项:
HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\RunOnce
set-location HKLM:\Software\Microsoft\Windows\CurrentVersion\RunOnce
new-itemproperty . MyKey -propertytype String -value "Powershell c:\temp\script2.ps1"
或者,您可以直接在 RunOnce 键下添加 .bat 文件。
RUNONCE 键下的文件在登录后执行并在执行后删除。
您可以参考下面的文章,它创建了工作流并创建了ScheduledTask来恢复暂停的工作流。
workflow test-restart {
#STEPS 1-3
Restart-Computer -Wait
#STEPS 5 - 6
}
对于调度:
$act = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
$trig = New-ScheduledTaskTrigger -AtLogOn
注意:使用示例中的工作流不是强制性的。 您可以编写自己的逻辑,以便在重新触发后从您离开的点恢复 - 通过保留步骤数据。
这种方法的关键是创建一个计划任务,该任务被触发以在重新启动后自动继续脚本。
即使在这种情况下,也可能需要将脚本本地复制到机器上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.