Questions? Feedback? powered by Olark live chat software
Gezintiyi Atla

How to optimize costs by setting expired date for Azure Virtual Machine by PowerShell

One Code tarafından
Son güncelleştirme tarihi: 11.10.2016
Katkıda Bulunanlar:
GitHub üzerinde düzenleyin

Introduction

This script demonstrates how to set expired date for Azure Virtual Machine in Dev Labs by PowerShell.

Related topics

Scenarios

Practically, developers often create Azure Virtual Machines in Dev Labs for demo or development purpose. However, those “test” VM instances are also charged. Since nobody remembers to delete them after development, company shall then pay for the instances. Thus, it must be wonderful if those “test” VM instances can be automatically deleted.

Luckily, Microsoft Azure have released new features about expired date of virtual machine on October 6, 2016. IT pro can make the use of this new feature to optimize costs. And this sample also supplies an OOB script to set expired date for your existed VM instance in Dev Labs.

Prerequisites

Script

  • Open the script file SetExpiredDateAzureVM.ps1
  • Edit the parameter “-VMName” and “-LabName” and “-ExpiredUTCDate”
    ps1 Set-AzureVirtualMachineExpiredDate -VMName "vmname" -LabName "labname" -ExpiredUTCDate "2016-10-10"

Notes: the date is in UTC format

  • Then run the script in PowerShell console
  • After the script finishes its job, you will see similar outputs at below

Here are some code snippets for your reference. ```ps1 Function Set-AzureVirtualMachineExpiredDate { [CmdletBinding()] Param ( [Parameter(Mandatory=$true)][String]$VMName, [Parameter(Mandatory=$true)][String]$LabName, [Parameter(Mandatory=$true)][DateTime]$ExpiredUTCDate )

# get vm info 
$targetVMInfo = Get-AzureRmResource | Where { $_.Name -eq "$LabName/$VMName" -and $_.ResourceType -eq 'Microsoft.DevTestLab/labs/virtualMachines' } 

# if not find, throw exception 
If ($targetVMInfo -eq $null) { 
    Throw "No VM naming $VMName" 
} 

# get vm properties 
$vmInfoWithProperties = Get-AzureRmResource -ResourceId $targetVMInfo.ResourceId -ExpandProperties 
$vmProperties = $vmInfoWithProperties.Properties 

# set expired date 
$vmProperties | Add-Member -MemberType NoteProperty -Name expirationDate -Value $ExpiredUTCDate 
Set-AzureRmResource -ResourceId $targetVMInfo.ResourceId -Properties $vmProperties -Force 

Write-Host "Successfully to set VM "$LabName/$VMName" to expire on UTC $ExpiredUTCDate" 

}

1. login azure

Login-AzureRmAccount

2. fill target vm name, lab name and expired date

Set-AzureVirtualMachineExpiredDate -VMName "lab-autoexpired" -LabName "eric-lab" -ExpiredUTCDate "2016-10-10" ```

Additional Resources