none
Error in simple script = need help! RRS feed

  • Question

  • 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

    • Moved by YASWANTHM-MSFTModerator Friday, June 22, 2018 7:46 PM Better suited here(Moved from Azure Mangement portal to Azure CLI)
    Friday, June 22, 2018 1:10 PM

Answers

  • 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.

    Friday, June 22, 2018 8:12 PM
    Moderator
  • 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.

    Saturday, June 23, 2018 8:07 PM
    Moderator

All replies

  • 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.

    Friday, June 22, 2018 8:12 PM
    Moderator
  • Works brilliantly - thank you for explaining.

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

    Saturday, June 23, 2018 10:36 AM
  • 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.

    Saturday, June 23, 2018 8:07 PM
    Moderator
  • Thank you -  most useful.

    J

    Monday, June 25, 2018 12:14 PM
  • Glad to know it helped.
    Saturday, June 30, 2018 12:57 PM
    Moderator