Nigel Findlater
Nigel Findlater

Reputation: 1724

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?

Upvotes: 3

Views: 8053

Answers (2)

AshokPeddakotla
AshokPeddakotla

Reputation: 1038

ServiceName specifies the name of the Azure service that contains the virtual machine to shut down.

Incase if you have deployed your VM through classic model, then you will get the ServiceName.

From the description, looks like you have created the VM through ARM model. I suggest you use Get-AzureRmVM cmdlet to list the VM's.

To start VM use the below PowerShell cmdlets.

Start-AzureRmVM -ResourceGroupName "YourResourceGroupName" -Name "YourVirtualMachineName"

To stop VM use the below PowerShell cmdlets.

Stop-AzureRmVM -ResourceGroupName "ResourceGroupName" -Name "VirtualMachineName"

Upvotes: 2

kim
kim

Reputation: 3421

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

Upvotes: 2

Related Questions