#Parameters param( [Parameter(Mandatory=$true)] [string] $ResourceGroupName, [Parameter(Mandatory=$true)] [string] $VMName ) #Azure Authentication try { $AzureConnection = (Connect-AzAccount -Identity).context } catch { Write-Output "There is no system-assigned user identity. Aborting." exit } #Get status of Azure VM $vm = Get-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName -Status $powerState = $vm.Statuses | Where-Object { $_.Code -like "PowerState/*" } | Select-Object -ExpandProperty DisplayStatus #Start Azure VM if ($powerState -eq "VM stopped" -or $powerState -eq "VM deallocated") { Write-Output "Starting VM '$VMName'..." Start-AzVM -ResourceGroupName $ResourceGroupName -Name $VMName } else { Write-Output "VM '$VMName' is already running or in transition. No action taken." }