- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Shutdown/poweroff VM from a txt list
Hello,
I would like to ask if someone can share a powershell script where I can choose to shutdown guest and/or poweroff VM from a txt list, and get result output in a csv or txt file.
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
What would define if it should be a guest shutdown or a power off?
The presence of VMware Tools?
Something like this?
foreach($vmName in (Get-Content -Path .\vmnames.txt)){
$vm = Get-VM -Name $vmName
if($vm.Guest.State -eq "Running"){
Shutdown-VMGuest -VM $vm -Confirm:$false
}
else{
Stop-VM -VM $vm -Confirm:$false
}
}
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi LucD,
I think this is fine, I will test.
About your question relate vmtools, there are some VMs with 3rd party software, where different vendor will manage, in this case, script will do only poweroff right?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
If you are referring to VMware Tools that are displayed as "Guest Managed', they will also run the Shutdown-VMGuest cmdlet.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello.. can someone please help me out on the below error or post the script for the Shutdown Power Off VM from a text file so I can see where my mistakes may be.. I have the below, but the VM is not being found.. Thank you!!
ERROR
Get-VM : 3/23/2018 9:08:44 AM Get-VM VM with name ''testfailover'' was not found using the specified filter(s).
At C:\temp\new_test.ps1:3 char:11
+ $vm = Get-VM -Name $vmName
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (:) [Get-VM], VimException
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM
Stop-VM : Cannot validate argument on parameter 'VM'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\temp\new_test.ps1:7 char:21
+ Stop-VM -VM $vm -confirm:$false
+ ~~~
+ CategoryInfo : InvalidData: (:) [Stop-VM], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutomation.ViCore.Cmdlets.Commands.StopVM
SCRIPT
$vm = Get-VM -Name $vmName
if($vm.Guest.State -eq "Running"){
Shutdown-VMGuest -VM $vm -Confirm:$false}
else{
Stop-VM -VM $vm -confirm:$false
write-host "Powering Down VM..."
Count 20
write-host "Adjusting Compute..."
set-vm -numCPU 2 -memorygb 4 -confirm:$false
start-vm -confirm:$false
Count 120
write-host "Powering On Server..."
export "c:\temp\results.csv"}
}
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
It looks as if the VM with displayname 'testfailover' was not found.
Does Get-VM -Name testfailover return anything?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hi Luc.. Thanks for the reply. I was able to grab without an issue..
PowerCLI C:\temp> get-vm testfailover
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
testfailover PoweredOn 4 6.000
PowerCLI C:\temp> get-vm -name testfailover
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
testfailover PoweredOn 4 6.000
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
How did you construct your text file?
Does Get-Content -Path c:\temp\vm.txt return a line with testfailover on there?
As another test do a Get-VM -Name (Get-Content -Path c:\temp\vm.txt)
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Yes, see below...
PowerCLI C:\temp> get-vm -name testfailover
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
testfailover PoweredOn 4 6.000
PowerCLI C:\temp> get-content -path c:\temp\vm.txt
testfailover
PowerCLI C:\temp> Get-VM -Name (Get-Content -Path c:\temp\vm.txt)
Name PowerState Num CPUs MemoryGB
---- ---------- -------- --------
testfailover PoweredOn 4 6.000
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
That's strange.
Can you attach the script (.ps1) as you are running it?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Adjusted a couple lines and this is what comes up now.. Thanks Luc!
PowerCLI C:\temp> .\test_323.ps1
At C:\temp\test_323.ps1:3 char:28
+ foreach($vmName in $VMList)
+ ~
Missing statement body in foreach loop.
At C:\temp\test_323.ps1:17 char:5
+ }
+ ~
Unexpected token '}' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : MissingForeachStatement
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
You were missing the code-block for the ForEach.
Not sure what the purpose of the 'export' at the end is, but I removed it for now.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thank you Sir! Ill give it a try
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I wanted to export the results after they run, we will be running this late at night and would like to have an output file to share to the server owners...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Okay, the VM shutdown pretty quickly which is great. Doesnt look like the compute was adjusted nor did the VM did not power back on
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Ok, I updated your code to perform the Set-VM in any case.
Try this one
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Luc.. That seems to have worked. Didnt export but thats fine, I can tweak it..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Luc, any quick one liner help to point me in the right direction that will help export the results of the VM that was edited? We are not set on strategy, and the more verification the better for getting these 200 or so VM's right-sized automatically.
The export I have only exports the VM name.
After the VM's are powered back on, Id like to export the VM Name, Timestamp, and current Compute allocation. If it isnt too much re-writing that is..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Try the attached.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Thanks Luc! I was able to do what I needed, I get an error on -Name when it runs on the first VM only, but it still works..
I'm need try and incorporate a VMtools version and Status check, then a reboot where needed..
Is there a way to pass along or link the Guest.State with the following objects in the If/and statement below? Appreciate your help Luc!
Guest.ToolsVersion
Guest.ToolsStatus
Shutdown-VMGuest -VM $vm -Confirm:$false}