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?
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
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
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.
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.
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.
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
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.
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
Negative, Got red errors when i ran it:
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.
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
I was also going to say I believe you need quotations and not single quote. Was that part of the problem?
It was a few things.
Connecting to vcenter and Creds first.
Then adding the where object statement, followed by double quotes.