Best Practices


Start of a PowerShell Tooklit Part 2

by Steve  | April 30th, 2009

This post is the second installment of my blog titled “Start of a PowerShell Toolkit.” This time I am going to present two scripts, createsnapshot.ps1 and removesnapshot.ps1, which are geared towards working with snapshots.  I figured the name of the scripts would be pretty self explanatory as to what the scripts’ actual functions are. These scripts can really come in handy for a down and dirty way to create snapshots as well as remove them from a virtual machine.

First, let’s take a look at the create snapshot script createsnapshot.psi:

#PowerShell script for a VM to create a Snapshot
#Stephen Beaver
#Tripwire
#April 21 2009
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
Add-PSSnapIn VMware.VimAutomation.Core
$VMachineName = $Args[0]
$SnapName = $Args[1]
if (($VMachineName -eq $null)-or ($SnapName -eq $null)){
Write-Host
Write-Host “Please specify a Virtual Machine and or Snapshot name eg….”
Write-Host “powershell.exe createsnapshot.ps1 VM1 Snapname”
Write-Host
Write-Host
exit
}

Connect-VIServer
# This next line will create the Snapshot without a prompt before the action
get-vm $VMMachine | New-Snapshot -Name $SnapName -Confirm:$false
# Comment the line above and use the line below to be prompted
#get-vm $VMMachine | New-Snapshot -Name $SnapName -Confirm:$true

Notice that there is an option to get prompted, or not, when performing the action. The same setting is in the next script to remove snapshots removesnapshot.ps1:

#PowerShell script for a VM to remove a Snapshot
#Stephen Beaver
#Tripwire
#April 21 2009
#FORCE TO LOAD VMWARE POWERSHELL PLUGIN
Add-PSSnapIn VMware.VimAutomation.Core
$VMachineName = $Args[0]
$SnapName = $Args[1]
if (($VMachineName -eq $null)-or ($SnapName -eq $null)){
Write-Host
Write-Host “Please specify a Virtual Machine and or Snapshot name eg….”
Write-Host “powershell.exe removesnapshot.ps1 VM1 Snapname”
Write-Host
Write-Host
exit
}

Connect-VIServer
# This next line will delete or remove the Snapshot without a prompt before the action is taken
get-vm $VMachineName | get-snapshot -Name $SnapName | remove-Snapshot -Confirm:$false
# Comment the line above and use the line below to be prompted
#get-vm $VMMachine | get-snapshot -Name $SnapName | remove-Snapshot -Confirm:$true

As you can see there is really still not too much to the scripts and they present a quick and dirty way to get a job done.

Until next time happy scripting!


Tags , , ,

This entry was posted on Thursday, April 30th, 2009 at 12:32 pm and is filed under Virtual Tech. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Comments

Hey Steve,

I read this blog post using Google Reader and the color of the code snippets makes it difficult to read against a white background.

Cheers,

Dave

  

Add a Comment

x

Subscribe to The Virtual Black Hole RSS Feed Email Notification

Enter your email address:

Delivered by FeedBurner