简体   繁体   中英

How do I restart a windows service on an Azure VM remotely from my office desktop using powershell?

I have a few Windows Server 2019 Core VMs in Azure all running the same windows service.

I would like to run a powershell script from my computer that loops through all the VMs and restarts the service on each.

I have a PS script on each VM which does this so how do I invoke them remotely? Is this possible without using Cloud Shell or Automation, with just a few lines of code? I have Azure powershell tools installed on my computer too.

Please provide answers with a working example.

Thank you

For your requirement, I think the simplest way is to use the Azure PowerShell command to execute the PowerShell script inside the VM, the example command here:

Invoke-AzVMRunCommand -ResourceGroupName 'rgname' -VMName 'vmname' -CommandId 'RunPowerShellScript' -ScriptPath 'sample.ps1' -Parameter @{param1 = "var1"; param2 = "var2"}

To execute the script in each VM, you need to make a loop with PowerShell yourself. By the way, you can also use the Custom Extension for Windows .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM