This is 100% referenced

I have done some little change so it suits me to do some easy schedule for our test environment. 

I.E. Start the test environment at 8:00AM daily and shutthem down at 18:00 daily on weekdays.

Stop the test environment at any other time.


BTW, the sharepoint 2013 template on Azure is amazing, you can easily spin it up within one hour, by simply click click click.

To create a identical environment yourself will easily take three days, and most of the time you will need to wait for downloading and installation updates. Anyway.


1. Create an Azure RunBook, type workflow RunBook.

2. Here is the code:

    [string] $action,
    [string] $ResourceGroupName
$connectionName = "AzureRunAsConnection"
Write-Output ("before connection")
    # Get the connection "AzureRunAsConnection "
    $servicePrincipalConnection=Get-AutomationConnection -Name $connectionName         
    "Logging in to Azure..."
    Add-AzureRmAccount `
        -ServicePrincipal `
        -TenantId $servicePrincipalConnection.TenantId `
        -ApplicationId $servicePrincipalConnection.ApplicationId `
        -CertificateThumbprint $servicePrincipalConnection.CertificateThumbprint 
        Write-Output ("Connected")
catch {
    if (!$servicePrincipalConnection)
        $ErrorMessage = "Connection $connectionName not found."
        throw $ErrorMessage
    } else{
        Write-Error -Message $_.Exception
        throw $_.Exception
  $VMs = Get-AzureRMVM -ResourceGroupName $ResourceGroupName
#Print out up to 10 of those VMs
     if(!$VMs) {
        Write-Output "No VMs were found in your subscription."

     } else {

		Foreach ($VM in $VMs) {
            write-output $VM.Name
    $VMDetail = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -Status
                                      foreach ($VMStatus in $VMDetail.Statuses)
                                          if($VMStatus.Code.CompareTo("PowerState/deallocated") -eq 0)
                                              $VMStatusDetail = $VMStatus.DisplayStatus
    if ($action -eq "start"){
         write-output "try to start" $
         start-AzureRmVM  -ResourceGroupName $ResourceGroupName -Name $VM.Name -ErrorAction SilentlyContinue 
	if($action -eq "stop"){ 
        write-output "try to stop" $
        Stop-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VM.Name -force -ErrorAction SilentlyContinue}




3. Test it, type in the action as start or stop, then type in the resource group name, it shall show the results.

4. Schedule it as required. 



About Lei

I am an IT specialist with over 10 year experience - years on Automation, on-Premise or Azure.

I am happy to develop however never want be a full time developer. Only do what I have to do. If it has to be PowerShell,HTML, PHP, CSS, C#, VBS or JS, front end or backend, so be it, doesn't matter!

Spent years with Windows, SCCM, SharePoint, SQL and Exchange servers. For last several years, I have been actively working under On Premise > Azure environment.


Current Certificates:
    Microsoft® Certified-
  • -Enterprise Administrator
  • -Database Administrator
  • -SharePoint Administrator
  • -Administering and Deploying SCCM 2012
Red Hat Certified Technician
ITIL V3 Foundation - Practitioner

Working on Azure Certificates now and hopefully they can stop upgrading their questions one day! GIVE ME A BREAK!

Contact Lei

Name *
Email *
Comments *

Traffic since 10/08/2016

This week13
This month2086

Visitor Info

  • IP:
  • Browser: Unknown
  • Browser Version:
  • Operating System: Unknown

Who Is Online