Ying Li at myITforum.com

PowerShell & System Center

PowerShell Script to Take Snapshot on Multiple VM Machines

In my previous Blog, I introduced some basics about VMWare Infrastructure Toolkit. Now let's put it into action!

Let's say you have a test MOSS or SMS virtual environment, you have a couple of servers and you configured everything and test everything. Before you make the next big change, you may want to take a snapshot of your machines so you can have some to fall back on if something goes bad!

It couldn't easier with VI Toolkit!

$Now = Get-Date

#get all your moss servers
$VMs = get-vm |where{$_.name -like "xyzmoss*"}
foreach ($vm in $VMs)

#Take snapshots for each VM Box
{new-snapshot -vm $vm -name $Now -confirm}

Posted: Aug 18 2008, 11:26 PM by yli628 | with 4 comment(s)
Filed under:

Comments

halr9000 said:

Couple of suggestions:

1. Get-VM's name parameter supports wildcards!

2. New-Snapshot accepts VM objects on the pipeline (read the help for this cmdlet, you'll see where it says this).

That means your code can be simplified to:

get-vm xyzmoss* | new-snapshot -name (get-date) -confirm

:)

This also has the benefit of moving the get-date to inside the loop, thus allowing for the date to change as each snapshot is created.

-hal

Author of upcoming book, Managing VMware Infrastructure with PowerShell: TFM (sapienpress.com/vmware.asp)

# August 19, 2008 8:57 AM

yli628 said:

Hal,

Thanks for your comment and tip about wildcard.

I do know that new-snapshot accepts vm object pipeline, same like shutdown-vmguest and others. Actually that's what I tried at begining and it indeed works.

The original intension of this post is something like this:

$date = get-date

$VMs = get-vm |where{$_.name -like "nytwsccm*"}

foreach ($vm in $VMs)

{

$Task1 = Shutdown-VMGuest -vm $vm -confirm

Wait-task -Task $Task1

$Task2 = new-snapshot -vm $vm -name $date -confirm

Wait-task -Task $Task2

Restart-VMGuest -vm $vm -confirm

}

That is to shutdown the vm box, take snapshot and restart them.

But I had hard time to get the wait-task part to work, I am using the latest MS internal build. so I scratch them altogether.

Do you have any luck to get wait-task to work, which version of PowerShell you use.

Also can you pipe them all? shutdown, snampshot, restart...

Thanks,

Ying

# August 19, 2008 11:10 AM

halr9000 said:

Oh, ok.  Yeah, if you want to do multiple statements, putting them in a foreach or foreach-object loop is the way to go.  The problem you are running into (and this came up recently on the VITK forums), is that Shutdown-VMGuest returns a Guest object rather than a task object.  And it doesn't wait for the power to be turned off at all, instead it returns immediately.  In theory, the new-snapshot command should still work, but of course if your OS is in the middle of a shutdown that's not going to be a good snapshot.

I suggest using a while loop and check the powerstate with get-vm

# August 20, 2008 7:53 PM

yli628 said:

Thanks, that's good to know.

# August 20, 2008 8:30 PM