[英]How to reboot a azure vm from powershell script and continue the remaining steps
I have an azure VM .我有一个天蓝色的 VM。 Am writing a powershell script which executes two .bat files after that it has to reboot the azure VM after that it has to continue to execute remaining .bat files.我正在编写一个执行两个 .bat 文件的 powershell 脚本,然后它必须重新启动 azure VM,然后它必须继续执行剩余的 .bat 文件。
Here's the below script这是下面的脚本
Step1: set-location -path C:/temp
步骤 1: set-location -path C:/temp
Step2: start-process cmd.exe /c "abc.bat" -wait
Step2: start-process cmd.exe /c "abc.bat" -wait
Step3: start-process cmd.exe /c "123.bat" -wait
Step3: start-process cmd.exe /c "123.bat" -wait
Step4: restart-computer -Force
Step4: restart-computer -Force
Step5: start-process cmd.exe /c "prod.bat" -wait
Step5: start-process cmd.exe /c "prod.bat" -wait
Step6: start-process cmd.exe /c "dev.bat" -wait
Step6: start-process cmd.exe /c "dev.bat" -wait
Am executing this script remotely by giving below command通过给出以下命令远程执行此脚本
PS C:\> Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath '/Users/username/sample.ps1'
After running the above command its executing fine up to "step 4" .After that its not continuing with the remaining steps运行上述命令后,它执行到“第 4 步”为止。之后它不会继续执行剩余的步骤
Can you please help me on this ?你能帮我解决这个问题吗?
There are two approaches that I can think of and detailing them below :我可以想到两种方法,并在下面详细介绍它们:
Using the registry key RUNONCE - which can be used to specify commands that the system will execute one time and then delete.使用注册表项RUNONCE - 可用于指定系统将执行一次然后删除的命令。
More information available here . 此处提供更多信息。
There will be slight modification in when running it remotely.远程运行时会稍作修改。
When you use the Invoke-AzVMRunCommand
, the initial command will have to copy the 2 script files to a temporary location in the remote VM and kick start the first script (this contains steps 1-3)当您使用Invoke-AzVMRunCommand
,初始命令必须将 2 个脚本文件复制到远程 VM 中的临时位置并启动第一个脚本(这包含步骤 1-3)
And before step 4 - you will create a registry key entry under RUNONCE :在第 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"
Or Alternatively you can directly add the .bat files under the RunOnce keys.或者,您可以直接在 RunOnce 键下添加 .bat 文件。
The file gets under RUNONCE keys gets executed after login and gets deleted after the execution. RUNONCE 键下的文件在登录后执行并在执行后删除。
You could refer to the below article which creates workflow and also creates ScheduledTask to resume the suspended workflow.您可以参考下面的文章,它创建了工作流并创建了ScheduledTask来恢复暂停的工作流。
https://devblogs.microsoft.com/scripting/powershell-workflows-restarting-the-computer/#automatic-restart-of-workflow https://devblogs.microsoft.com/scripting/powershell-workflows-restarting-the-computer/#automatic-restart-of-workflow
workflow test-restart {
#STEPS 1-3
Restart-Computer -Wait
#STEPS 5 - 6
}
For scheduling :对于调度:
$act = New-ScheduledTaskAction -Execute $pstart -Argument $actionscript
$trig = New-ScheduledTaskTrigger -AtLogOn
NOTE : It is not mandatory to use workflows like in the example.注意:使用示例中的工作流不是强制性的。 You can write your own logic to resume at the point where you had left once it has been re-triggered - by persisting the step data.您可以编写自己的逻辑,以便在重新触发后从您离开的点恢复 - 通过保留步骤数据。
The key to this approach is to create a scheduled task which gets triggered to continue the script automatically after a restart.这种方法的关键是创建一个计划任务,该任务被触发以在重新启动后自动继续脚本。
Even in this case, it may be necessary to copy the scripts locally to the machine.即使在这种情况下,也可能需要将脚本本地复制到机器上。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.