Schedule startup and shutdown of your virtual machines using Azure Automation

Written by ppolyzos

Software engineer based in Athens, Greece


    1. ppolyzos

      You can specify the VMs you want to start or stop using a comma separated string array, like ‘AzureVm1,AzureVM2’ and then perform a loop for each VmName.
      So, you can do something like:

  1. Eli Rios

    How do you properly replace the parameters for the variables?
    Do I have to change [string]$VmName, with the name of my server like this [AzureVM-Srv1]$VmName or without brackets? or AzureVM-Srv1 = $VmName?

    1. ppolyzos

      If I understand correctly your question, if you want to define your server name in VmName, you only to update VmName with AzureVM-Srv1 as shown in Step4.
      What is inside the brackets is to add additional information about the variable, like string or validation like [ValidateSet(“Startup”, “Shutdown”)]. You can have a look also here to learn more about Powershell parameters.

    2. Andre

      hi Eli, you set the parameters when you create the schedule task. You dont need to change anything in the code.
      You can set parameters in test panel also.

  2. moin

    Grate jobs thanks from bottom of my heart , As well i if you help me with few other parameters like i want to run same with multiple selected vm, where and how to add parameters.

  3. moin

    Thanks for the quick response. as i am not an expert in powershell, Could you please tell me where and how to make changes in current script as current script work for only one vm i want to use same for multiple vms i should have option to specifie or selcet the vm’s to start and stop.

    1. ppolyzos

      I understand now what you want to achieve. Have list a list of VM names and with a single script start them or shut them down.
      You can use a string array parameter, like: param([string[]] $VmName......) and then iterate on the $VmNames. Have a look as well here

      And for VMName you can pass multiple string comma separated like: Vm1,Vm2,Vm3....

      Be aware that I haven’t tested the script above but it should work or it may require some minor changes.

      1. moin

        Thank you so much for the help .after making some changes it is working now…..i have one more question Sir. Suppose i have to automate vm creation at particular time. everyday vm should be created automatically using run book. is it possible ?

          1. Tyron

            The individual VM shutdown as per the original is working like a charm, I have a few machines i would like to group into one runbook, the above.

            When I add the above i get the string cant, I believe it is missing the $name value somewhere, thanks again.

  4. angel

    Is it any possible to make a PowerShell script to run automatic shutdown per resouce group in azure?
    I am basically trying to make an auto shutdown to the entire resource group only if its on start mode.
    If they are already offline so the script shouldn’t do anything.
    And I want to manually start each one, so basically its only a shutdown script for the entire subscription I am searching.

      1. Adir

        Hey, first of thanks a lot for the quick reply .
        I am kind of new to powershell, is there any way you can write me a sample and I’ll fix it to my needs?
        If it’s not too much of a trouble.
        Appreciate any help, thanks again

        1. ppolyzos

          For more info on how to collect details about your VMs I would suggest to try this tutorial. It has also a Try It link at the top right corner to get you up to speed quickly.

          Now in regards to what you have to do, I would suggest to read this article, Simultaneously Start|Stop all Azure Resource Manager Virtual Machines in a Resource Group, and it will will help you a lot.

          Another way to do this is by creating multiple jobs and have something a Job to start/stop all your VMs:

Leave a Comment

Your email address will not be published. Required fields are marked *