jessem
Enthusiast
Enthusiast

Easy one but hard for me - Power off/on a list of VMs

Jump to solution

I know the commands to power off and on a VM, but if I have a list of VMs in a csv format, and I want to power them off (not gracefully), and then power them back on, what's the easiest way for this to get done?  Or even if I want to reset a list of VMs, how?

0 Kudos
1 Solution

Accepted Solutions
jessem
Enthusiast
Enthusiast

I figured it out but changed some things.  This works fine now.

Power Off

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

$file = "c:\temp\vmlist.txt"

$vms = Get-Content $file

Get-VM $vms | where-object {$_.PowerState -eq "PoweredOn"} | Shutdown-VMGuest -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

Power On

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

$file = "c:\temp\vmlist.txt"

$vms = Get-Content $file

Get-VM $vms | where-object {$_.PowerState -eq "PoweredOff"} | Start-VM -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

View solution in original post

0 Kudos
12 Replies
Anjani_Kumar
Commander
Commander

Power ON Script : 

$file = "c:\TEMP\vmlist.txt"

$vms = Get-Content $file

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

Get-VM $vms | where {$_.status -eq "powered off"} | Start-VM -Runasync -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

Power OFF Script :

$file = "c:\TEMP\vmlist.txt"

$vms = Get-Content $file

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

Get-VM $vms | where {$_.status -eq "powered on"} | Stop-VM -Runasync -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

Please consider marking this answer "correct" or "helpful" if you found it useful. Anjani Kumar | VMware vExpert 2014-2015-2016 | Infrastructure Specialist Twitter : @anjaniyadav85 Website : http://www.Vmwareminds.com
jessem
Enthusiast
Enthusiast

Thanks for your response.

However, when I ran this script, it prompted my for my credentials, I entered them and clicked ok, then the script stops.  Nothing is executed.  It just output that I am connect to port 443 on my vcenter.

My text file I am pointing to just has a few VM names listed.

0 Kudos
Anjani_Kumar
Commander
Commander

save it to same location  as per script and point it there. else it will not work.

i use the same script for bulk power on and off the vm.

Please consider marking this answer "correct" or "helpful" if you found it useful. Anjani Kumar | VMware vExpert 2014-2015-2016 | Infrastructure Specialist Twitter : @anjaniyadav85 Website : http://www.Vmwareminds.com
0 Kudos
jessem
Enthusiast
Enthusiast

Still confused.

I have the vmlist.txt in my C:\temp.

I have the script called, poweroffvm.ps1 in my C:\temp.

I launch power cli and execute poweroffvm.ps1.

It prompts for my vc name, and I enter it.

It prompts for my credentials which I enter.

Then it logs in outputs:

---------------

Name               Port     User

vcentername     443     mydomain\userid

But nothing happens after that.  My VM doesn't get powered off.

0 Kudos
Anjani_Kumar
Commander
Commander

Modify the second last line for my power off screen and see if it works

Get-VM $vms | where {$_.status -eq "powered on"} | Shutdown-VMGuest -Runasync -Confirm:$false | Out-Null

Please consider marking this answer "correct" or "helpful" if you found it useful. Anjani Kumar | VMware vExpert 2014-2015-2016 | Infrastructure Specialist Twitter : @anjaniyadav85 Website : http://www.Vmwareminds.com
0 Kudos
jessem
Enthusiast
Enthusiast

Same result.

Just says I'm connected to the vcenter and then I get a prompt:

PowerCLI C:\temp>

and the VM is still powered on.

Like I said, all I have in the list is the name of one VM.

0 Kudos
Anjani_Kumar
Commander
Commander

Try this one. Modified it. see if this work..

Power OFF Script :

$file = "c:\TEMP\vmlist.txt"

$vms = Get-Content $file

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

Get-VM $vms | where {$_.powerstate -eq ‘PoweredOn’} | Shutdown-VMGuest -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

Please consider marking this answer "correct" or "helpful" if you found it useful. Anjani Kumar | VMware vExpert 2014-2015-2016 | Infrastructure Specialist Twitter : @anjaniyadav85 Website : http://www.Vmwareminds.com
0 Kudos
jessem
Enthusiast
Enthusiast

Negative, Got red errors when i ran it:

error.JPG

0 Kudos

It looks like you might have some invalid characters in the script after -eq that might of came from copying from the website.  Copy the command into notepad  first then paste back in your IDE that you are using.

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
jessem
Enthusiast
Enthusiast

I figured it out but changed some things.  This works fine now.

Power Off

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

$file = "c:\temp\vmlist.txt"

$vms = Get-Content $file

Get-VM $vms | where-object {$_.PowerState -eq "PoweredOn"} | Shutdown-VMGuest -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

Power On

$vc = Read-Host "Enter the VC name"

$cred = Get-Credential

Connect-VIServer $vc -Credential $cred

$file = "c:\temp\vmlist.txt"

$vms = Get-Content $file

Get-VM $vms | where-object {$_.PowerState -eq "PoweredOff"} | Start-VM -Confirm:$false | Out-Null

Disconnect-VIServer -Confirm:$false

0 Kudos

I was also going to say I believe you need quotations and not single quote. Was that part of the problem?

http://www.twitter.com/markdjones82 | http://nutzandbolts.wordpress.com
0 Kudos
jessem
Enthusiast
Enthusiast

It was a few things.

Connecting to vcenter and Creds first.

Then adding the where object statement, followed by double quotes.

0 Kudos