-1

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

Andrew Jocelyn
  • 259
  • 3
  • 11
  • Can you accept the answer while it works for you or please let me know if you need more help to solve the problem? – Charles Xu Oct 21 '19 at 09:00

1 Answers1

1

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.

Charles Xu
  • 24,004
  • 1
  • 12
  • 27