VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
1 2 Previous Next 20 Replies Last post: Sep 25, 2009 9:38 AM by RichardCarter   Branched to a new thread.

Extending the snaphost powershell script. How to add snapshot size? posted: Sep 11, 2008 1:36 AM

Click to view Bist's profile Novice 9 posts since
Feb 27, 2006
Hi guys,

Stupid question maybe. I am using this script to list the open snapshots and mail them to our team. Question I got now is the following. Is it possible to add the size of the snapshot in the report? How should I do that?

#Setup email client
+ $SmtpClient = New-Object system.net.mail.smtpClient+
+ $MailMessage = New-Object system.net.mail.mailmessage+
+ $SmtpClient.host = "smtpserver"+
+ $MailMessage.from = "sdfsdfsdf"+
+ $MailMessage.To.add("sdfsdfsdfsdfs")+
+ $MailMessage.IsBodyHtml = 1+
+ $MailMessage.Subject = "Current Vmware Snapshots"+

#Get snapshots from all servers
+ Connect-VIServer virtualcenterserver+
+ $Snaps = @()+
+ get-vm | get-snapshot | %{+
+ $Snap = {} | Select VM,Name,Created,Description+
+ $Snap.VM = $_.vm.name+
+ $Snap.Name = $_.name+
+ $Snap.Created = $_.created+
+ $Snap.Description = $_.description+
+ $Snaps = $Snap
+ }+
+ $MailMessage.body = $Snaps | Sort VM | Select VM,Name,Created,Description | ConvertTo-Html+
+ $SmtpClient.Send($MailMessage)+

Another question is how do I call the powershell script including the VI toolkit. Now I initialize the VI toolkit each time by adding the following in the script

Add-PSSnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1

Click to view LucD's profile Champion 2,429 posts since
Oct 31, 2005
With the following function you get the filesize of the snapshot for a specific guest.

function Get-SNapshotSize($name)
{
  $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
  $searchSpec.details = New-Object VMware.Vim.FileQueryFlags
  $searchSpec.details.fileSize = $TRUE
  $searchSpec.query += New-Object VMware.Vim.VmSnapshotFileQuery
 
  $size = 0
  Get-VM -Name $name | Get-View  | % {
           foreach ($s in $_.Layout.Snapshot){
              foreach ($f in $s.SnapshotFile){
                if ($f -match ".vmsn$"){
                  $ds = Get-View ((Get-View $_.Datastore[0]).Browser)
				  $task = Get-View ($ds.SearchDatastoreSubFolders_Task($f.Substring(0,$f.IndexOf("/")),$searchSpec))
                  while($task.Info.State -eq "running"){}
                  foreach ($r in $task.Info.Result){
                    foreach ($tf in $r.File){
                      $size += $tf.FileSize
					}
                  }
	            }
               }
             }
           }
 
return $size
}
 
Get-SNapshotSize(<guest-name>)
 

You can add the load of the VITK snapin in your PowerShell profile.
Something like this


Add-PSSnapin VMware.VimAutomation.Core
 


I attached the script since the forum SW sometimes changes the script text

That profile is located in %windir%\system32\windowspowershell\v1.0 as file profile.ps1.
Attachments:
Click to view LucD's profile Champion 2,429 posts since
Oct 31, 2005
The function I provided returns the snapshot size for a specific guest.
You can add a new property ($Snap.Size) which you populate with the snapshot size by calling the function.

This is a sample implementation where I merged your script with my function
function Get-SNapshotSize($name)
{
  $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
  $searchSpec.details = New-Object VMware.Vim.FileQueryFlags
  $searchSpec.details.fileSize = $TRUE
  $searchSpec.query += New-Object VMware.Vim.VmSnapshotFileQuery
 
  $size = 0
  Get-VM -Name $name | Get-View  | % {
           foreach ($s in $_.Layout.Snapshot){
              foreach ($f in $s.SnapshotFile){
                if ($f -match ".vmsn$"){
                  $ds = Get-View ((Get-View $_.Datastore[0]).Browser)
				  $task = Get-View ($ds.SearchDatastoreSubFolders_Task($f.Substring(0,$f.IndexOf("/")),$searchSpec))
                  while($task.Info.State -eq "running"){}
                  foreach ($r in $task.Info.Result){
                    foreach ($tf in $r.File){
                      $size += $tf.FileSize
					}
                  }
	            }
               }
             }
           }
 
return $size
}

#Setup email client
$SmtpClient = New-Object system.net.mail.smtpClient
$MailMessage = New-Object system.net.mail.mailmessage
$SmtpClient.host = "<smtp-server-name>"
$MailMessage.from = "<from-email-address>"
$MailMessage.To.add("<to-email-address>")
$MailMessage.IsBodyHtml = 1
$MailMessage.Subject = "Current Vmware Snapshots"

#Get snapshots from all servers
Connect-VIServer <VC-server-name>
$Snaps = @()
get-vm | get-snapshot | %{
  $Snap = {} | Select VM,Name,Created,Description, Size
  $Snap.VM = $_.vm.name
  $Snap.Name = $_.name
  $Snap.Created = $_.created
  $Snap.Description = $_.description
  $Snap.Size = Get-SNapshotSize($_.vm.name)
  $Snaps += $Snap
}

$MailMessage.body = $Snaps | Sort VM | Select VM,Name,Created,Description,Size | ConvertTo-Html
$SmtpClient.Send($MailMessage)


I hope this clarifies it.
Attachments:
Click to view Smitty Smith's profile Novice 21 posts since
Jul 25, 2008

I have tried the provided script to get the snapshot size and found a couple of unexplained issues with this one I hope you can help me with. The first issue is that you pull any files with the extension .vmsn. As I understand it, this file stores the running state of the VM (Snapshot state file) which is a few megabites larger than the memory allocated to the VM and does not grow. The 000001.vmdk file is the snapshot delta file which is the file that actually grows over time after a snapshot has been taken and hence the file that should be monitored or better yet a sum of the .vmdk and the vmsn for total snapshot size would be preferable.

The second issue is that the script will return a snapshot count factor for size by adding all the .vmsn files together for each snapshot instance then add all those together which gives an incorrect answer. I.E. if 4 snapshots have been taken and 2gb of memory has been allocated to the VM then each snapshot .vmsn file will be about 2.1Gb in size. When the script enumerates the first snapshot, it then adds all four .vmsn files together so that script returns 2.1Gb times four or 8.4Gb. It then goes to snapshot two and does the same thing. The total return value comes out to be 8.4 times 4 or 33.2Gb when in reality each .vmsn is only 2.1Gb and the total for all four snapshots is 8.4Gb. This delta gets larger with more snapshots taken.

What I am looking for is a way to add the .vmsn to the associated delta .vmdk and spit out a report that has the name of the VM, the name of the snapshot, date the snapshot was created and the size of that snapshot. I have the first three with no issues but associating the total size of that snapshot to its name is proving problematic as I don't know file info gathering yet. I am hoping you can help with these issues.

Click to view c_shanklin's profile Master 752 posts since
Dec 3, 2007
Hi Smitty, I've been wanting to do this for a while, so I've added a cmdlet to the VI Toolkit Extensions that will do this.

It's just very similarly to Get-Snapshot, just run
Get-VM | Get-TkeSnapshotExtended | select Name, VM, SizeMB


The SizeMB property accounts for the size of the deltas as well as the persisted RAM, etc. I'm not sure if it works against VMs with more than one disk. If anyone has any problems with it, let me know.
Click to view LucD's profile Champion 2,429 posts since
Oct 31, 2005
Well done Carter, very useful cmdlet.

Since you have been busy with the SearchDatastoreSubFolders_Task method a question on the side.
Did you notice that the filesizes returned by this method do not correspond exactly with what you see when you do a simple "ls -l" in the VMFS folder ?
Any clue where this discrepancy could come from ?
Click to view Smitty Smith's profile Novice 21 posts since
Jul 25, 2008

This works quite well with your help to load it. Thanks. I ran a test and here is my results.

One VM with one snapshot

.vmsn - 2102.374
delta.vmdk - 100.352
total Snapshot - 2202.726
script report - 2151.099

One VM with four snapshots

1st Snaphot
.vmsn - 2102.374
delta.vmdk - 2425.856
total actual - 4528.230
script report - 4422.100

2nd Snaphot
.vmsn - 2102.374
delta.vmdk - 3195.904
total actual - 5298.282
script report - 5174.104

3rd Snaphot
.vmsn - 2102.374
delta.vmdk - 181.248
total actual - 2283.622
script report - 2230.100

4th Snaphot
.vmsn - 2102.374
delta.vmdk - 1754.112
total actual - 3856.486
script report - 3766.100

As you can see with the .vmsn, 2Gb memory has been allocated to these VMs but I agree with LucD that there is a little delta between the cmdlet and actual size. Not much delta and this could be due to the difference in how the VC Datastore GUI reports and how the API reports size. I have noticed that the greater the delta file size, the greater the delta between the total size and the script size and that the script always reports lower numbers than the GUI. Still, I can live with this due to not wanting any snapshot over 18 hours old in the environment if any at all. This will help weed these out and submit reports to the big boys. Thanks.

Click to view c_shanklin's profile Master 752 posts since
Dec 3, 2007
It's better than nothing I guess :/

When I get a chance I'll see if I can find any reason for the difference.
Click to view Smitty Smith's profile Novice 21 posts since
Jul 25, 2008
Interesting update. I just ran snapshot size using 1) putty in and ls -l, 2) VC datastore gui, 3) VC 4.0 snapshot view, 4) cmdlet, 5) LucD API and 6) FastSCP. All the snapshot size methods used report different snapshot sizes with VC 4.0 reporting the smallest and putty reporting the largest. None match. The question now becomes which is the real size?
Click to view Smitty Smith's profile Novice 21 posts since
Jul 25, 2008
Also of note is that on the VC 4.0 the snapshot portion of the cmdlet returns blank. Specifically the VMware.Vim.VirtualMachineFileLayout.snapshot is blank on the VC 4.0 connected servers. Haven't found any other descrepancies but then have not yet gotten into the VC 4.0 with scripts yet.
Click to view c_shanklin's profile Master 752 posts since
Dec 3, 2007
Smitty Smith wrote:
Also of note is that on the VC 4.0 the snapshot portion of the cmdlet returns blank. Specifically the VMware.Vim.VirtualMachineFileLayout.snapshot is blank on the VC 4.0 connected servers. Haven't found any other descrepancies but then have not yet gotten into the VC 4.0 with scripts yet.

I looked into that and VI 4 replaces the layout field with one called layoutex.

Hopefully there won't be too many "quirks" like that.
Click to view Smitty Smith's profile Novice 21 posts since
Jul 25, 2008
Hopefully not, but did run into this once the extention module was updated with layoutex. $task = $datastoreBrowser.SearchDatastoreSubFolders_Task("$name /$subpath", $spec) returns with an "Exception calling "SearchDatastoreSubFolders_Task" with "2" argument(s): "Not initialized: boolean fileOwner" At C:\Data\PowerShell\PowerShell Scripts\Snapshots\viToolkitExtensions.psm1:762 char:59" error again when accessing VC 4.0 servers. The extension works fine with VC 2.5.
Click to view c_shanklin's profile Master 752 posts since
Dec 3, 2007
Ok, good to know :/

I'll have to get a 4.0 system up and stable one of these days. Too bad I can't run it under Workstation.
Click to view LucD's profile Champion 2,429 posts since
Oct 31, 2005
Does this mean that running ESX 3.x under Workstation is a supported feature ? ;-)

Btw you can install ESX 4 under WS 6.5 but you can't start guests in ESX 4 in that setup.
Sounds familiar ?

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities