VMware Cloud Community
itc0mputerguru
Contributor
Contributor
Jump to solution

Is this kind of script impossiable?

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

0 Kudos
1 Solution

Accepted Solutions
halr9000
Commander
Commander
Jump to solution

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

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000

View solution in original post

0 Kudos
3 Replies
njain
Expert
Expert
Jump to solution

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.

http://www.vmware.com/support/developer/windowstoolkit/wintk15/windowstoolkit15-200901-releasenotes....

This toolkit provides cmdlets, sample scripts and function library that will help you get started.

itc0mputerguru
Contributor
Contributor
Jump to solution

How do I get the script to run at a scheduled time? Should the script be written in C or powershell?

0 Kudos
halr9000
Commander
Commander
Jump to solution

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

My signature used to be pretty, but then the forum software broked it. vExpert. Microsoft MVP (Windows PowerShell). Author, Podcaster, Speaker. I'm @halr9000
0 Kudos