My boss wants me to write a script that will do the following:
Shutdown the VM gracefully, Change the memory and CPU, start the VM, log in, and then reboot the VM after all the hardware is seen about 5 minutes.
As I am very new to scripting for VMware, I am not sure how to do this or even where to put the script if it can be written.
Thanks for any help you can offer.
Mike
It'll work something like this:
$vmlist = "vm1", "vm2", "vm3" # etc. foreach ( $name in $vmlist ) { $vm = get-vm $name $vm | shutdown-vmguest -confirm:$false start-sleep 60 # wait for shutdown if ( Get-VM $name | where-object { $_.PowerState -ne "PoweredOff" } ) { start-sleep 60 # wait longer } $vm | set-vm -MemoryMB 2048 # change vm settings here $vm | start-vm
To schedule this, place these cmds in a .ps1 file and invoke it using Windows task scheduler. There are a few items you have to add to the file or to the task, search the forum for this topic, it has come up before.
[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org
Using VI APIs (for which VI Toolkit provides easy-to-use C# and PowerShell interface), you can perform the following operations:
1. Shutdown the VM
2. Change the memory and CPU
3. Start the VM
4. Reboot the VM
However, after powering on the VM, you cannot log in and check if the boot up has been completed and all the hardware can be seen in the VM. VI API cannot access this information from the guest OS.
To start with the script, you can download VI Toolkit for Windows.
This toolkit provides cmdlets, sample scripts and function library that will help you get started.
How do I get the script to run at a scheduled time? Should the script be written in C or powershell?
It'll work something like this:
$vmlist = "vm1", "vm2", "vm3" # etc. foreach ( $name in $vmlist ) { $vm = get-vm $name $vm | shutdown-vmguest -confirm:$false start-sleep 60 # wait for shutdown if ( Get-VM $name | where-object { $_.PowerState -ne "PoweredOff" } ) { start-sleep 60 # wait longer } $vm | set-vm -MemoryMB 2048 # change vm settings here $vm | start-vm
To schedule this, place these cmds in a .ps1 file and invoke it using Windows task scheduler. There are a few items you have to add to the file or to the task, search the forum for this topic, it has come up before.
[PowerShell MVP|https://mvp.support.microsoft.com/profile=5547F213-A069-45F8-B5D1-17E5BD3F362F], VI Toolkit forum moderator
Author of the upcoming book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org