0
votes

Need to shutdown specific VMs in my Azure subscription. Is there a way, I can make the ‘Get-AzureVM’ PowerShell cmdlet to read from an input file? We have too many VMs in our subscription. So specifying individual machine by name is tedious in our case. Please advice.

1
Get-AzureVM won't read a file, but you can extract what you need from your file and pipe it to the command. What have you tried?Cobster
Hi Cobster, So far, I tried Get-Content and its not working as expected. Get-Content C:\data\VMlist.txt | ForEach-Object {Start-AzureVM }Ram
Edit your question and add the code there instead of in the comments.Brendan Green
Why use a text file? I presume you want to shut down ALL VM's that are listed in a file. Can you just add the VM names to an array in a powershell script and just loop over that instead? If there's no real need for the external file, you can simplify things down.Brendan Green
Hi Brendan, I will do use array instead of file. Thanks for the suggestionRam

1 Answers

0
votes

I am assuming in the answer below that you are using ASM Azure portal, i.e. the older portal as you are using the ASM cmdlets.

If you want to start all VMs in your subscription then you can use:

Get-AzureVM | Start-AzureVM

If you want to start only specific VMs then you can use below script:

#Declaring the Dictionary object to store VM Names along with their Service names
$VMList = @{}

#Adding VMs to the VM List
#You can replace this section with reading from file and then populating your dictionary object
#Replace the VM Name and Service name with your environment data
$VMList.Add("VMName01", "ServiceName")
$VMList.Add("VMName02","ServiceName")

#Getting the VM object using Get-AzureVM and then starting the VMs
$VMList.Keys | % {Get-AzureVM -Name $_ -ServiceName $VMList.Item($_)} | Start-AzureVM