Today, we’ll see how we can leverage Azure Automation to stop/start virtual machines in Azure. We’ll begin with how to start virtual machine first and same steps can be applied to stop virtual machines. You’ll only need to change few commands in the runbook associated.
Login into Azure Resource Manager with your credentials. Click on Azure Automation account and then click on the runbooks section highlighted:

This will open up a new blade containing attached runbooks for this account. By default, you can see the 4 runbooks auto-created by Microsoft.

As you can see, this blade contains two buttons to add runbooks:
- Add a runbook: You can add either a new runbook or import an existing runbook
- Browse gallery: Use this option to use existing runbooks loaded on Azure site by Microsoft and community members.
At the top of the this blade, click Browse Gallery button. We can use built-in runbooks created by Microsoft for this simple task. Here’s what the gallery would look like:

For the purpose of this blog, we’ll use ‘Start Azure classic VMs’ highlighted above. Click on the runbook and in the new blade opened, click import:

After this, we’ll need to edit the runbook we just imported. for this, just click on the runbook and then click edit:

Now, if you edit code for associated PowerShell workflow, you can see that this workflow requires 3 optional parameters:
- AzureCredentialAssetName: This will be used to fetch the resources using Azure credentials
- AzureSubscriptionIdAssetName: This will be used to associate the subscription id with the azure account so as to get resources associated.
- ServiceName: This will be used to get VMs related to a specific cloud service only.
Out of these, though first two are optional, you need to have them as they will be used to get resources from Azure. Without these default values will be used. For now, just go ahead and publish the runbook. This will make runbook available for use.
Now, go to Azure Automation account blade again, then click on the assets. This will open up a new blade containing various tiles. You need to update values for AzureCredentialAssetName in the credentials section and AzureSubscriptionIdAssetName in the variables section:

Once you do this, go to Azure runbook -> Start-AzureVMs runbook -> Start. This will open up a blade showing real time information for the job status. You can see whether the job succeeded/failed and associated logs, if any.

For stopping Azure VMs, you can import associated runbook and then follow steps as mentioned above.