I have create on script if azure vm state is deallocated the vm should be Trun on. Please review the code

if ($vm.PowerState -eq “deallocated”)
{
$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue
try
{
$StartRtn = $vm | Start-AzureRmVM -ErrorAction Continue
$flag=true
}
catch
{

The VM failed to start, so send notice

Write-Output ($VM.Name + " failed to start")

Write-Error ($VM.Name + " failed to start. Error was:") -ErrorAction Continue

Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue

}

4 Spice ups

Hi and welcome to the Community!

In the future, I would kindly ask you to use the “Insert code” button in the editor when posting code. It looks much prettier and easier to read.

if ($vm.PowerState -eq "deallocated") 
{ 
$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue 
try
{
$StartRtn = $vm | Start-AzureRmVM -ErrorAction Continue 
$flag=true
}
catch
{
# The VM failed to start, so send notice 
Write-Output ($VM.Name + " failed to start") 
Write-Error ($VM.Name + " failed to start. Error was:") -ErrorAction Continue 
Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue 
} 

The first issue I can see is that at least one “}” is missing.

1 Spice up
At /tmp/home/.code.tio.ps1:2 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingEndCurlyBrace
 

Real time: 0.838 s
User time: 0.736 s
Sys. time: 0.106 s
CPU share: 100.55 %
Exit code: 1

is the return I received from running a powershell emulator on your code.

This code is run without any error but azure vm not start
if ($vmname.PowerState -eq "vmname deallocated") 
{ 
$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue
$flag=true 
	try
	{
		$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue 
		$flag=true
	}

  catch
    {
    # The VM failed to start, so send notice 
    Write-Output ($VM.Name + " failed to start") 
    Write-Error ($VM.Name + " failed to start. Error was:") -ErrorAction Continue 
    Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue
    } 
}

you need resouregroupname to start the VM

$vms=Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState
foreach($vm in $vms){
IF($vm.PowerState -eq 'VM deallocated'){
Try{
$ErrorActionPreference='stop'
Start-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
catch{
Write-Warning "$_"
}
}
}

Hi Jitesh,

Thanks for support.

I have one more question, like we can pass ResourceGroupName and Name in CSV file. I don’t want to run on each VM and i want to run only selected VM.

you can keep dellaocated vm names and resourgroupnames in csv like

ResourceGroupName,name
RG1,VM1
RG2,Vm2
$vms=Import-Csv c:\filename.csv
#Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState
foreach($vm in $vms){
Try{
$ErrorActionPreference='stop'
Start-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name
}
catch{
Write-Warning "$_"
}
}

to export csv file

Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState|export-csv c:\filename.csv -nti

^ keep the vm’s you want to select