
Prerequisites:
- Azure Subscription
- Azure Resource Group
- DevOps Orginisation
- Azure Service Principal
- Virtual Machine
- Setting up Service Principal
1. Open Azure Active Directory 2. Open App Registrations

Enter a name and click Register

Click Certificates & Secrets then New client secret.

Enter a Description and select how long the secret will be valid for.

Copy the secret value as we will need this later.

Now we will add the Service Principal to be able to access the Subscription.

Select Contributor or Virtual Machine Operator then search for the Service Principal name.

You will now see the Service Principal’s permissions.

DevOps Setup
Within the DevOps project, select Project Settings then open Service Connections.

Select Azure Resource Manager

Select Service Principal (Manual). You can choose Service Principal (Automatic) but it will auto create a Service Principal that wont match our naming standards.

Now fill out the details, these details can be found from the Subscription Overview/Service Principal Overview and we will also need the value saved earlier from the Service Principal Secret.
Subscription ID - Found from Subscription Overview. Subscription Name - Name of Subscription. Service Principal ID - Application (Client) ID found in the Service Principal Overview. Service Principal Key - The value from the secret we created earlier. Tenant ID - Can be found in the home screen of Azure AD or found in the Service Principal Overview.

Click Verify and it should be successful, if not you can scroll up and it will show you the error reason.

DevOps Pipelines
Select Pipelines > Create Pipeline then select Use the classic editor.

Select a Repository to store the pipeline.

Click Empty Job

Select the + next to Agent Job 1 then search for Azure CLI then click Add

Under the connection you should see the Service Connection we just set up

Fill out the details as shown in the image and select Shell then Inline Script. Use the script below and modify the Resource Group name and the individual VM names. In the script below we start the Database VM first then wait for 360 seconds before starting the other VMs. You can add as many VMs as you need and you can have multiple sleep commands set if needed.
set -xe az vm start --resource-group rg-cyberscot-vm --name dbscyberscotdev01 && \ sleep 360s && \ az vm start --resource-group rg-cyberscot-vm --name vmcyberscotdev01 && \ az vm start --resource-group rg-cyberscot-vm --name vmcyberscotdev02

We now set the schedule that the VMs should start at. (Example is every weekday at 1am)

Ensure that ‘Only schedule builds if the source or pipeline has changed’ is unselected.

Select the Agent Specification, Give the pipeline a suitable name then save.

The pipeline will now run on the selected schedule and start the VMs.
You can now combine Azures Auto-Shutdown feature with your Auto-Start pipeline for seamless start/stop of VMs to reduce costs!
