VMware Cloud Community
Mahiee
Enthusiast
Enthusiast

Restart multiple VMs through Script

Dear All,

Often I have a requirement to restart multiple VMs, so I usually do it manually through vCenter.

But if it is more than 20 or 30 or so, it would be lot of time consumption, therefore can anyone please suggest me with the script which helps me to automate it.

The list of servers are not same for every time, so i'm looking if there is any option to store my servers in notepad and that will be called through script, post completion also there must be option to get the result of servers reboot status in output file.

Thank you very much for your suggestions.

Rachis.

Tags (1)
5 Replies
LucD
Leadership
Leadership

You could do something like this

The .txt file should contain a VM name per line.

Note that the 2nd Get-VM could be executed separately, some time after you start the VMs.

$vmNames = Get-Content -Path vmnames.txt

Get-VM -Name $vmNames | Start-VM -Confirm:$false

Get-VM -Name $vmNames | Select Name,PowerState |

Out-File -FilePath vmState.txt


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Mahiee
Enthusiast
Enthusiast

Thank you so much for your response, but seems the below command is help to power on the server if that is in powered down state, it is not helping me to restart.

Can you please suggest me the command for restart the servers.

$vmNames = Get-Content -Path vmnames.txt

Get-VM -Name $vmNames | Start-VM -Confirm:$false


And in the below command powerstate result is not coming, can you please help me with the command for getting power state and also the last reboot time.


Thanks again.


Rachis.

Reply
0 Kudos
LucD
Leadership
Leadership

Ok, two option there, a hard of a guest OS based restart

$vmNames = Get-Content -Path vmnames.txt

Get-VM -Name $vmNames | ReStart-VM -Confirm:$false

$vmNames = Get-Content -Path vmnames.txt

Get-VM -Name $vmNames | Restart-VMGuest -Confirm:$false

Not sure why you don't see a PowerState.

Does this show the PowerState?

Get-VM | Select Name,PowerState


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Mahiee
Enthusiast
Enthusiast

Thank you very much, surprising I got below warnings, can you please confirm if they are safe?

WARNING: The 'Description' property of VirtualMachine type is deprecated. Use the 'Notes' property instead.
WARNING: The 'HardDisks' property of VirtualMachine type is deprecated. Use 'Get-HardDisk' cmdlet instead.
WARNING: The 'NetworkAdapters' property of VirtualMachine type is deprecated. Use 'Get-NetworkAdapter' cmdlet instead.
WARNING: The 'UsbDevices' property of VirtualMachine type is deprecated. Use 'Get-UsbDevice' cmdlet instead.
WARNING: The 'CDDrives' property of VirtualMachine type is deprecated. Use 'Get-CDDrive' cmdlet instead.
WARNING: The 'FloppyDrives' property of VirtualMachine type is deprecated. Use 'Get-FloppyDrive' cmdlet instead.
WARNING: The 'Host' property of VirtualMachine type is deprecated. Use the 'VMHost' property instead.
WARNING: The 'HostId' property of VirtualMachine type is deprecated. Use the 'VMHostId' property instead.
WARNING: PowerCLI scripts should not use the 'Client' property. The property will be removed in a future release.

Reply
0 Kudos
LucD
Leadership
Leadership

For now you can safely ignore these warnings.

They are generic, and only have meaning when you actually are using the properties mentioned.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos