Quick Script to Switch Azure VM on/off

$ResourceGroupName = "$(yourpipelinevariable)"

$Vmname = "$(yourpipelinevariable)"

$VMstatus = Get-AzVm -ResourceGroupName $ResourceGroupName -name $Vmname -Status

if ($VMstatus.Statuses[1].code -eq "powerstate/deallocated") {

    $newVMstatus = Start-AzVM -ResourceGroupName $ResourceGroupName  -Name $Vmname
    Write-Host "VM Starting" -NoNewline

}elseif ($VMstatus.Statuses[0].code -eq "ProvisioningState/succeeded") {

    Write-Host "The" $Vmname  "is already switched on"

}