Search code examples
powershellazureazure-automation

Powershell for stopping and starting Azure VMs


I have created a "Visual Studio Enterprise 2017 (latest release) on Windows Server (x64)" Virtual machine. I would like to start and stop this machine via Powershell.

#Login
Add-AzureAccount

#Enterprize subscription. Id can be found by seraching for subsription in the portal
Select-AzureSubscription -SubscriptionId xxxxxx-xxxe-xxxx5-8xxxx-e2xxxxxxxx1c

#Should list VMs but returns nothing
Get-AzureVM 

#Asks for ServiceName that I cannot find 
Start-AzureVM -Name NigelsPcWUS        

How can I find the ServiceName that corresponds to my VM? Or is there a better way to do this?


Solution

  • My guess is that you create the virtual machine using the Azure Resource Management (ARM) model. The PowerShell script above use the older, Azure Service Management (ASM) model.

    The above script need a few modifications to use the ARM model:

    #Login using the ARM model
    Login-AzureRmAccount
    
    #Select your subscription if you got multiple
    Select-AzureRmSubscription -SubscriptionId xxxxxx-xxxe-xxxx5-8xxxx-e2xxxxxxxx1c
    
    #Get a list of exisiting VMs
    Get-AzureRmVM
    
    #Start a vm named 'linux' inside resource group 'lab-rg'
    Start-AzureRmVM -Name linux -ResourceGroupName lab-rg
    

    If you don't have AzureRM PowerShell commands installed and are running Windows 10, you can easily install them by running

    Install-Module AzureRM