Error in simple script = need help!

Category: azure scripting and command line tools

Question

Palmer_001 on Fri, 22 Jun 2018 13:10:06


Hey all, I am creating a small script to power down all VMs in a test azure lab. I do know there are scripts in the gallery, yes, but I am trying to learn it myself. I have an error but not sure exactly how I am supposed to resolve it. Here is the snippet

 

$vmname='vmw01'

$vm Get-AzureVM|Where-Object{ $_.Name -eq$vmname}

Write-Host

$vm.OperationStatus $vm.PowerState $vm.Status 'OK StoppedVM'

And then I get this error:

Get-AzureVM : No default subscription has been designated. Use Select-AzureSubscription -Default <subscriptionName> to set the default

subscription.

At line:6 char:7

+ $vm = Get-AzureVM | Where-Object { $_.Name -eq $vmname }

+       ~~~~~~~~~~~

    + CategoryInfo          : CloseError: (:) [Get-AzureVM], ApplicationException

    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.IaaS.GetAzureVMCommand

Thing I don't understand is this. If I run Get-AzureRMSubscription just before the script, it resolves to the correct sub. Could someone provide me the line of code I need to add the subscription data to the script please?

Thank you

Replies

YASWANTHM-MSFT on Fri, 22 Jun 2018 20:12:23


The PowerShell script above which you have mentioned use the old azure portal, Azure Service Management (ASM) model.

The above script need a few modifications to use the ARM model(Azure Resource Manager):

#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 existing VMs

Get-AzureRmVM

# To start VM use the below PowerShell cmdlets.

Start-AzureRmVM -Name VMNAME -ResourceGroupName RESOURCEGROUPNAME

To stop VM use the below PowerShell cmdlets.

Start-AzureRmVM -Name VMNAME -ResourceGroupName RESOURCEGROUPNAME

If you don't have AzureRM PowerShell commands, suggest you to install using below command in PowerShell console

Install-Module AzureRM

For more information, suggest you to refer the below article to shutdown the virtual machines using powershell. See if this helps.

Ref:- https://docs.microsoft.com/en-us/powershell/module/azure/stop-azurevm?view=azuresmps-4.0.0

----------------------------------------------------------------------------------------------

If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click here.

Palmer_001 on Sat, 23 Jun 2018 10:36:53


Works brilliantly - thank you for explaining.

One question if I may. Is there an easy way to target ALL VMs in the resource group?

YASWANTHM-MSFT on Sat, 23 Jun 2018 20:07:02


You can stop all VMs in Specified Azure Resource Group using PowerShell script. This sample runbooks stops all of the virtual machines in the specified Azure Resource Group.

Ref:-https://gallery.technet.microsoft.com/scriptcenter/Stop-All-VMs-in-Specified-40c8531e

Also, suggest you to check the following document link for PowerShell samples to shut down the all VM’s in the resource group deployment model. See if this helps.

For more information, you may refer the following document which explains stopping VM’s using Azure Automation.

Disclaimer: This response contains a reference to a third-party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet. 

----------------------------------------------------------------------------------------------

If this answer was helpful, click “Mark as Answer” or Up-Vote. To provide additional feedback on your forum experience, click here.

Palmer_001 on Mon, 25 Jun 2018 12:14:13


Thank you -  most useful.

J

YASWANTHM-MSFT on Sat, 30 Jun 2018 12:57:40


Glad to know it helped.