Script Repository

Stop Azure virtual machine

February 24, 2021

The script stops an Azure virtual machine.

Note: The script uses the $Context variable available on the server side only. This means that the script can be executed only by business rules, custom commands, and scheduled tasks. To add the script, use the Run a program or PowerShell script action.


  • $vmName - Specifies the virtual machine name.
  • $serviceName - Specifies the name of the Azure service that contains the virtual machine.
  • $stayProvisioned - Specifies whether to keep the virtual machine provisioned when it is stopped.
  • $publishSettingsFile - Specifies the full path to a *.publishsettings file with a certificate to connect to your Windows Azure account. You can obtain one using the Get-AzurePublishSettingsFile cmdlet.
Edit Remove
$scriptBlock = {
   Import-Module Azure

    $vmName = 'MYSERVER' # TODO: modify me
    $serviceName = 'MyService' # TODO: modify me
    $stayProvisioned = $True # TODO: modify me
    $publishSettingsFile = 'C:\Azure\MyAzureSettings.publishsettings' # TODO: modify me

    Import-AzurePublishSettingsFile -PublishSettingsFile $publishSettingsFile
    if ($stayProvisioned)
        Stop-AzureVM -ServiceName $serviceName -Name $vmName -StayProvisioned -Force
        Stop-AzureVM -ServiceName $serviceName -Name $vmName -Force

# Start Windows PowerShell as a separate process and run the script block
$waitTimeSeconds = 8 * 60 * 1000
$powershellPath = “C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe”
$process = Start-Process $powershellPath -NoNewWindow -ArgumentList ("-ExecutionPolicy Bypass -noninteractive -noprofile " + $scriptBlock) -PassThru
See Also:

Comments ( 0 )
No results found.
Leave a comment