I have create on script if azure vm state is deallocated the vm should be Trun on. Please review the code<\/p>\n
if ($vm.PowerState -eq “deallocated”)
\n{
\n$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue
\ntry
\n{
\n$StartRtn = $vm | Start-AzureRmVM -ErrorAction Continue
\n$flag=true
\n}
\ncatch
\n{<\/p>\n
Write-Output ($VM.Name + \" failed to start\")<\/p>\n
Write-Error ($VM.Name + \" failed to start. Error was:\") -ErrorAction Continue<\/p>\n
Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue<\/p>\n
}<\/p>","upvoteCount":4,"answerCount":7,"datePublished":"2020-06-12T16:52:01.000Z","author":{"@type":"Person","name":"pktech","url":"https://community.spiceworks.com/u/pktech"},"acceptedAnswer":{"@type":"Answer","text":"
you need resouregroupname to start the VM<\/p>\n
$vms=Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState\nforeach($vm in $vms){\nIF($vm.PowerState -eq 'VM deallocated'){\nTry{\n$ErrorActionPreference='stop'\nStart-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name\n}\ncatch{\nWrite-Warning \"$_\"\n}\n}\n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-06-14T05:16:50.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/5","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}},"suggestedAnswer":[{"@type":"Answer","text":"I have create on script if azure vm state is deallocated the vm should be Trun on. Please review the code<\/p>\n
if ($vm.PowerState -eq “deallocated”)
\n{
\n$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue
\ntry
\n{
\n$StartRtn = $vm | Start-AzureRmVM -ErrorAction Continue
\n$flag=true
\n}
\ncatch
\n{<\/p>\n
<\/a>The VM failed to start, so send notice<\/h1>\nWrite-Output ($VM.Name + \" failed to start\")<\/p>\n
Write-Error ($VM.Name + \" failed to start. Error was:\") -ErrorAction Continue<\/p>\n
Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue<\/p>\n
}<\/p>","upvoteCount":4,"datePublished":"2020-06-12T16:52:01.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/1","author":{"@type":"Person","name":"pktech","url":"https://community.spiceworks.com/u/pktech"}},{"@type":"Answer","text":"
Hi and welcome to the Community!<\/p>\n
In the future, I would kindly ask you to use the “Insert code<\/strong>” button in the editor when posting code. It looks much prettier and easier to read.<\/p>\nif ($vm.PowerState -eq \"deallocated\") \n{ \n$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue \ntry\n{\n$StartRtn = $vm | Start-AzureRmVM -ErrorAction Continue \n$flag=true\n}\ncatch\n{\n# The VM failed to start, so send notice \nWrite-Output ($VM.Name + \" failed to start\") \nWrite-Error ($VM.Name + \" failed to start. Error was:\") -ErrorAction Continue \nWrite-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue \n} \n<\/code><\/pre>\nThe first issue I can see is that at least one “}<\/strong>” is missing.<\/p>","upvoteCount":1,"datePublished":"2020-06-12T18:13:09.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/2","author":{"@type":"Person","name":"supaplex-starwind","url":"https://community.spiceworks.com/u/supaplex-starwind"}},{"@type":"Answer","text":"At /tmp/home/.code.tio.ps1:2 char:1\n+ {\n+ ~\nMissing closing '}' in statement block or type definition.\n+ CategoryInfo : ParserError: (:) [], ParseException\n+ FullyQualifiedErrorId : MissingEndCurlyBrace\n \n\nReal time: 0.838 s\nUser time: 0.736 s\nSys. time: 0.106 s\nCPU share: 100.55 %\nExit code: 1\n<\/code><\/pre>\nis the return I received from running a powershell emulator on your code.<\/p>","upvoteCount":0,"datePublished":"2020-06-12T21:57:57.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/3","author":{"@type":"Person","name":"The-Sith-Admin","url":"https://community.spiceworks.com/u/The-Sith-Admin"}},{"@type":"Answer","text":"
This code is run without any error but azure vm not start\n<\/code><\/pre>\nif ($vmname.PowerState -eq \"vmname deallocated\") \n{ \n$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue\n$flag=true \n\ttry\n\t{\n\t\t$StartRtn = $vmname | Start-AzureRmVM -ErrorAction Continue \n\t\t$flag=true\n\t}\n\n catch\n {\n # The VM failed to start, so send notice \n Write-Output ($VM.Name + \" failed to start\") \n Write-Error ($VM.Name + \" failed to start. Error was:\") -ErrorAction Continue \n Write-Error (ConvertTo-Json $StartRtn.Error) -ErrorAction Continue\n } \n}\n<\/code><\/pre>","upvoteCount":0,"datePublished":"2020-06-13T04:16:38.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/4","author":{"@type":"Person","name":"pktech","url":"https://community.spiceworks.com/u/pktech"}},{"@type":"Answer","text":"Hi Jitesh,<\/p>\n
Thanks for support.<\/p>\n
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.<\/p>","upvoteCount":0,"datePublished":"2020-06-14T06:05:24.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/6","author":{"@type":"Person","name":"pktech","url":"https://community.spiceworks.com/u/pktech"}},{"@type":"Answer","text":"
you can keep dellaocated vm names and resourgroupnames in csv like<\/p>\n
ResourceGroupName,name\nRG1,VM1\nRG2,Vm2\n<\/code><\/pre>\n$vms=Import-Csv c:\\filename.csv\n#Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState\nforeach($vm in $vms){\nTry{\n$ErrorActionPreference='stop'\nStart-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name\n}\ncatch{\nWrite-Warning \"$_\"\n}\n}\n\n<\/code><\/pre>\nto export csv file<\/p>\n
Get-AzureRmVM -Status | Select ResourceGroupName, Name, PowerState|export-csv c:\\filename.csv -nti\n<\/code><\/pre>\n^ keep the vm’s you want to select<\/p>","upvoteCount":0,"datePublished":"2020-06-14T06:23:58.000Z","url":"https://community.spiceworks.com/t/unable-to-start-the-vm/765907/7","author":{"@type":"Person","name":"jitensh","url":"https://community.spiceworks.com/u/jitensh"}}]}}