1
votes

I am new to Powershell, and to be specific entirely new to Azure Powershell. I need to create a powershell script to switch off all the VM's found on a subscription. I guess this needs to be done with the help of management certificate. But not sure where to start. I just done some simple lines of code to list all VM's as shown below:

Add-AzureAccount -Environment "AzureCloud"
$subscription = "my-sub-scri-ption"
Select-AzureSubscription -Current "SubName"
Set-AzureSubscription -SubscriptionName "SubName" 
Get-AzureVM -ServiceName "VM1"

The output recieved is "Get-AzureVM : Value cannot be null. Parameter name: subscriptionId".

Can someone please help me in this regard?

**EDIT:**

The powershell script which I am using is given below:

Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM | Stop-AzureVM -Force

Though while running it shows script execution is successful, I can see still that vm is powered on. Please note that the serviceName and vmName is same in my case. Anything wrong here in my code?

**Re-Edit** Executed code:

Add-AzureAccount -Environment "AzureCloud"
Set-AzureSubscription -SubscriptionName "My Subs"
$serviceName = "Service01"
$vmName = "Service01"
Get-AzureVM

Error for the above code:

Get-AzureVM : Value cannot be null.
Parameter name: subscriptionId
At D:\TC_PS\Untitled1.ps1:5 char:1
+ Get-AzureVM
+ ~~~~~~~~~~~
+ CategoryInfo          : CloseError: (:) [Get-AzureVM], ArgumentNullException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand
1
Could you please print the Get-AzureVM result, because the posted code seems to be well formedMichael Denny
@Michael: Please see re-edit!!serverstackqns
that error happens when the selected subscription is not valid. Now I would run Get-AzureSubscription | Remove-AzureSubscription this will delete all your subscriptions (I think you run Set-AzureSubscription before add the accout), then just run Add-AzureAccount, after you login check your subscriptions just running Get-AzureSubscription you don't need to run Set-AzureSubscription (that is used to change the subscription, for example to set the current storage name), after you check the correct SubscriptionName just run Select-AzureSubscription "MySubscriptionName" then Get-AzureVMMichael Denny

1 Answers

4
votes

You can start from here: Getting Started with Windows Azure PowerShell

Then you can simply run Get-AzureVM that will return all the virtual machines for every cloud service.

To stop a VM: Stop-AzureVM -ServiceName xxx -Name vm-test-01

To stop all the VMs in the subscription simply run: Get-AzureVM | Stop-AzureVM -Force

The -Force switch is necessary to stop the last virtual machine of the cloud service, otherwise the system would throw an error when trying to stop the last VM in the service, to avoid drop all the resources of your published app.

If you don't specify the -StayProvisioned switch, the virtual machine will be deallocated. This is the default behavior of Stop-AzureVM.