Stop-Start-AzureVM (Scheduled VM Shutdown/Startup)

This PowerShell Workflow runbook connects to Azure using an Automation Credential and Starts/Stops a VM/a list of VMs/All VMs in a Subscription in-parallel.

 
 
 
 
 
5 Star
(9)
70,510 times
Add to favorites
Windows Azure
8/21/2018
E-mail Twitter del.icio.us Digg Facebook
  • Careful,
    1 Posts | Last post February 05, 2020
    • I strongly suggest people test this on test environment first, even though I changed the "All" to "specificVM" it still went ahead and shutdown "All" VMs
  • Parallel execution
    1 Posts | Last post December 19, 2019
    • Many thanks for this script.
      One thing which I faced was when you have lots of VMs this script tries to start/stop them in parallel in one time and gets stuck. 
      The way how to solve this issue it to and restrictions for parallel processes with 
      -ThrottleLimit option.
      
      Example: 
      foreach -parallel -ThrottleLimit 20 ($AzureVM in $AzureVMsToHandle) 
              { 
                  Get-AzureRmVM | ? {$_.Name -eq $AzureVM} | Stop-AzureRmVM -Force 
              }    
  • Hello
    1 Posts | Last post December 04, 2019
    • I have download the Powershell script. Are you able to highlight the value that need to be changed in the script using an example e.g. SubscriptionID?
      
      Kind regards
      
  • Script conversion
    1 Posts | Last post August 27, 2019
    • How to read/convert the script in the form of normal Powershell script as I can see its in PowerShell workflow mode. Would you be kind to share the guidelines in terms of normal PS mode instead of PS workflow?
  • error while testing the script please chcek error below and help to resolve it
    3 Posts | Last post August 25, 2019
    • Suspended
      Run Login-AzureRmAccount to login.
      Add-AzureRmAccount : Cannot bind argument to parameter 'Credential' because it is null.
      At Stop-Start-AzureVM:17 char:17
      + 
          + CategoryInfo          : InvalidData: (:) [Add-AzureRmAccount], ParameterBindingValidationException
          + FullyQualifiedErrorId : 
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand
       
      Select-AzureRmSubscription : Run Login-AzureRmAccount to login.
      At Stop-Start-AzureVM:18 char:18
      + 
          + CategoryInfo          : InvalidOperation: (:) [Set-AzureRmContext], PSInvalidOperationException
          + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand
       
      Add-AzureRmAccount : Cannot bind argument to parameter 'Credential' because it is null.
      At Stop-Start-AzureVM:17 char:17
      + 
          + CategoryInfo          : InvalidData: (:) [Add-AzureRmAccount], ParameterBindingValidationException
          + FullyQualifiedErrorId : 
      ParameterArgumentValidationErrorNullNotAllowed,Microsoft.Azure.Commands.Profile.AddAzureRMAccountCommand
       
      Select-AzureRmSubscription : Run Login-AzureRmAccount to login.
      At Stop-Start-AzureVM:18 char:18
      + 
          + CategoryInfo          : InvalidOperation: (:) [Set-AzureRmContext], PSInvalidOperationException
          + FullyQualifiedErrorId : InvalidOperation,Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand
       
    • The authentication to the runbook happens with the "AzureCredential" which can be added under Automation Account -> shared Resources -> Credentials
      
      
    • Hi which Credential you use?
  • many vm
    2 Posts | Last post August 25, 2019
    • what are the changes to make to the script to apply this one to some targeted VMs
    • No need change, while you run a script in parameter AZUREVMLIST you need type only the name of VM that you start/
  • does not find subscription id - although same is used for automation account and vm
    1 Posts | Last post August 23, 2019
    • Completed
      
      
      PSComputerName        : localhost
      PSSourceJobInstanceId : d62b82c7-d868-44fe-bd7b-27c337481b87
      Environments          : {AzureCloud, AzureChinaCloud, AzureUSGovernment}
      Context               : Microsoft.Azure.Commands.Profile.Models.PSAzureContext
      
      
      
      Select-AzureRmSubscription : Provided subscription xxxx-7887-4beb-83fe-013a7e9ea0xx does not exist
      At Stop-Start-AzureVM:18 char:18
      + 
          + CategoryInfo          : CloseError: (:) [Set-AzureRmContext], ArgumentException
          + FullyQualifiedErrorId : Microsoft.Azure.Commands.Profile.SetAzureRMContextCommand
       
      Get-AzureRmVM : No tenant found in the context.  Please ensure that the credentials you provided are authorized to 
      access an Azure subscription, then run Login-AzureRMAccount to login.
      At Stop-Start-AzureVM:27 char:27
      + 
          + CategoryInfo          : CloseError: (:) [Get-AzureRmVM], ArgumentException
          + FullyQualifiedErrorId : Microsoft.Azure.Commands.Compute.GetAzureVMCommand
       
      Stopping VMs
  • Error:Item has already been added
    1 Posts | Last post June 04, 2019
    • Hey guys,
      
      The runbook appears to work seamlessly but sometimes one of the VMs in my VMList does not start as scheduled. I found this in the Error section. At the moment, it looks alien to me. Would you be able to advise?
      
      Job Stream details
      Microsoft.PowerShell.Utility\Write-Error : Item has already been added. Key in dictionary: 'Microsoft.WindowsAzure.Commands.Common.CmdletInfoHandler' Key being added: 'Microsoft.WindowsAzure.Commands.Common.CmdletInfoHandler' At Stop-Start-AzureVM:75 char:75 + + CategoryInfo : NotSpecified: (:) [Write-Error], CmdletInvocationException + FullyQualifiedErrorId : System.Management.Automation.CmdletInvocationException,Microsoft.PowerShell.Commands.WriteErrorCommand 
  • Stop command
    2 Posts | Last post January 04, 2019
    • Every time I run the Stop Command it runs indefinitely and the Resource says Deallocating all the time, the only thing I am able to do is to stop the runbook and after a couple of minutes the VM finishes deallocating to Deallocated, any help?
    • Hi AlexPavlou, 
      
      Have you tried adding some optional parameters as -Force? https://docs.microsoft.com/en-us/powershell/module/servicemanagement/azure/stop-azurevm?view=azuresmps-4.0.0#optional-parameters