Download Attune CE

Shutdown Single or Multiple Azure Virtual Machines

Exported on 07-Oct-2021 21:54:47

Using Attune to shutdown Azure Virtual Machine

This Blueprint is used for shutting down single or multiple Azure Virtual Machines.

An Azure Virtual Machines is the Azure infrastructure as a service (IaaS) used to deploy virtual machines, it's commonly shorted to VMs with nearly any VM server workload that you want.

They provide on-demand and scalable computing resources with usage-based pricing.

Pre-Blueprint Attune setup
  1. On the Inputs tab, create a Windows Node for the host you wish to run this Blueprint.
  2. On the Inputs tab, create a Windows Credentials to connect to the host you wish to run this Blueprint.
  3. On the Inputs tab, create a Text value to store the values below:
    • AzureUserName: This is the Username of the Azure Administrator (DataType: String).
    • AzurePassword: This is the Password of the Azure Administrator (DataType: String).
    • AzureVmNames: This holds an array of VM(s) name(s) in Azure (DataType: Array).

AzureVmNames Syntax:

@('VMOne', 'VMTwo', 'VMThree')


Blueprint Steps
  1. Check and Install the Azure AzPowerShell Module
  2. Shuts down the Azure Virtual Machine(s)

Parameters

Name Type Script Reference Default Value Comment
Attune Node Windows Server attuneNode This is an Attune Node
Attune Node Credential Windows OS Credential attuneNodeCredential This is an Attune Node Credential
AzurePassword Text azurepassword PassW0rd@101 This is the Azure Administrator's Password
AzureUserName Text azureusername admin@contoso.com This is Azure Administrator's Username
AzureVMNames Text azurevmnames @('VMOne', 'VMTwo', 'VMThree') This is a list of VMs on Azure we are working with

1 - Install Azure Az PowerShell Module

This step installs the Azure Az PowerShell Module

The Blueprint first gets the Execution Policy of the current PowerShell session.

Then, checks if the Execution Policy is set to Unrestricted.

If it's not, it then sets the Execution Policy to Unrestricted for the current session.

Next, it checks if the Az PowerShell module is installed.

If it's not installed, it then goes ahead to install the module.

The connection details have changed from the last step.

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credential}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region for ExecutionPolicy
# Get Execution Policy of the current process
$Script:ProcessEP = Get-ExecutionPolicy -Scope Process

#Get the value of the Execution Policy and save it in the Variable
$Script:ValueProcessEP = ($Script:ProcessEP).value__

# Check if the Execution Policy of the process is set to Unrestricted
if ($Script:ValueProcessEP -eq 0) {

    # Write the message
    Write-Output "Execution Policy is already set to Unrestricted for the Process"
# Check if the Execution Policy of the process is already set
}else{

    # Set the ExecutionPolicy of the Process to Unrestricted
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force -Confirm:$false

    # Checks if the Execution Policy has been set
    if ((Get-ExecutionPolicy -Scope Process).value__ -eq 0) {

        # Write the message
        Write-Output "Execution Policy is now set to Unrestricted for the Process"
    }
}
#EndRegion for ExecutionPolicy 


#Region Check if Az Module is installed 
#Region if module is installed, update module if version is not up to Version "4.1.13.0"
if($null -ne (Get-InstalledModule -Name Az -ErrorVariable +ErrorAzV -ErrorAction SilentlyContinue)) {

    # Get the  Az module installed and save it in a variable
    $Script:GetAzModule = Get-InstalledModule -Name Az -ErrorVariable +ErrorAzV -ErrorAction SilentlyContinue

    # Writes a message to the screen
    Write-Output "Az PowerShell Module exists ... checking ..."

    # Gets the build number for the  Az Module 
    $Script:AzModuleBuild = ($Script:GetAzModule).Version

    # Checks the build number to meet requirements 
    if($Script:AzModuleBuild -like "*6.3.0*") {

        # Saves and converts Module version name to a variable
        $Script:OutVersion = ((($Script:GetAzModule).Version)).tostring()

        # Writes a message to the screen
        Write-Output "Az Module Version $Script:OutVersion meets the minimum requirement."

    # Check if the build version is on 13
    }else{

        # Writes a message to the screen
        Write-Output "Updating the Az PowerShell Module..."

        # Uppdates the  AzPowerShell Module to the latest version
        Update-Module -Name Az -Confirm:$false -Force 

        # Writes a message to the screen
        Write-Output "Az PowerShell Module is updated :)"
    }
#EndRegion if the module is installed, update module if the version is not up to Version "4.1.13.0"
#Region If the module is not installed, install it 
}else{

    # Writes a message to the screen
    Write-Output "Az PowerShell Module is not installed"
    
    # Writes a message to the screen
    Write-Output "Az PowerShell Module is installing..."

    # Install Az Powershell Module 
    Install-Module -Name Az -MaximumVersion "6.3.0" -Scope "CurrentUser" -AllowClobber:$true -Confirm:$false -Force

    # Writes a message to the screen
    Write-Output "Az PowerShell Module is installed :)"
}
#EndRegion If the module is not installed, install it

2 - Stop Azure VM

This step shuts down the Azure Virtual Machine(s)

The Blueprint first gets the Execution Policy of the current PowerShell session.

Then checks if the Execution Policy is set to Unrestricted.

If it's not, it then sets the Execution Policy to Unrestricted for the current session.

Next, the AzPowerShell module is imported to the current session.

Then the values below are set:

  1. UserName: This is the Username of the Azure Administrator corresponding to the AzureUserName set in the Inputs Tab.
  2. PasswordString: This is the Password of the Azure Administrator corresponding to the AzurePassword set in the Inputs Tab.
  3. AzureVmNames: This holds an array of VM(s) name(s) in Azure corresponding to the AzureVmNames set in the Inputs Tab.

Next, a connection to Azure is made.

Then it loops through the values of the Azure VM Names and shuts down the virtual machines.

Finally, the Azure PowerShell session is disconnected.

This step has the following parameters

Name Script Reference Default Value
AzureUserName {azureusername.value} admin@contoso.com
AzurePassword {azurepassword.value} PassW0rd@101
AzureVMNames {azurevmnames.value} @('VMOne', 'VMTwo', 'VMThree')

Login as user on node

  1. Connect via RDP
    mstsc /admin /v:Attune Node
  2. Login as user {Attune Node Credential}
  3. Then open a command prompt
This is a PowerShell Script make sure you run it with powershell.exe Click start menu, enter "powershell" in the search bar, then select the powersehll program
#Region for ExecutionPolicy
# Get Execution Policy of the current process
$Script:ProcessEP = Get-ExecutionPolicy -Scope Process

#Get the value of the Execution Policy and save it in the Variable
$Script:ValueProcessEP = ($Script:ProcessEP).value__

# Check if the Execution Policy of the process is set to Unrestricted
if ($Script:ValueProcessEP -eq 0) {

    # Write the message
    Write-Output "Execution Policy is already set to Unrestricted for the Process"
# Check if the Execution Policy of the process is already set
}else{

    # Set the ExecutionPolicy of the Process to Unrestricted
    Set-ExecutionPolicy -Scope Process -ExecutionPolicy Unrestricted -Force -Confirm:$false

    # Checks if the Execution Policy has been set
    if ((Get-ExecutionPolicy -Scope Process).value__ -eq 0) {

        # Write the message
        Write-Output "Execution Policy is now set to Unrestricted for the Process"
    }
}
#EndRegion for ExecutionPolicy 


#Region Stop Azure VM
# Import Module for Az PowerShell
Import-Module -Name Az

#Region assign variables
# Save accesskey to this Variable
$Script:UserName = "{azureusername.value}"

# Save secretkey to this variable
$Script:PasswordString = "{azurepassword.value}"

# List of VM names
$Script:AzureVmNames = {azurevmnames.value}
#EndRegion assign variables

#Region for Connection to Azure 
# Set the password and convert it to secure string to the variable
$Script:Password = ConvertTo-SecureString $Script:PasswordString -AsPlainText -Force

# set the credentials to the variable
$Script:UserCredential = New-Object System.Management.Automation.PSCredential ($Script:UserName,$Script:Password)

# Connect using set credentials to Azure
Connect-AzAccount -Credential $Script:UserCredential
#EndRegion for Connection to Azure 

# Loop through the hash table for the Names of the VM
foreach ($item in $Script:AzureVmNames) {

    # Get the Azure VM
    $Script:AzVm = Get-AzVM -VMName ($item)

    # Write the message
    Write-Output "The Azure VM '$($item)' in is stoping....."

    # Stop the Azure VM
    Stop-AzVM -ResourceGroupName $Script:AzVm.ResourceGroupName -Name $Script:AzVm.Name -NoWait -Confirm:$false -Force
}
#EndRegion stop Azure VM

#Region Disconnect the Azure session
Disconnect-AzAccount
#EndRegion Disconnect the Azure session