VMware Cloud Community
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Count the number of snapshots per VM

Is there a way (preferably a one liner) to get a list of VM's and number of snapshots per VM like the below...

Name Number

Test 2

Test1 7

Test 2 0

Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
hugopeeters
Hot Shot
Hot Shot
Jump to solution

It's easy when you use the script. You can do whatever you want with the output object $myCol:

$myCol | Where {$_.NumSnapshots -gt 0} | Sort-Object VM | Format-Table -AutoSize

Let me think about a oneliner...

It slows the script down, but should work:

Get-VM | Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}

Message was edited by: hugopeeters

Added slow oneliner

View solution in original post

20 Replies
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Mini-script off the top of my head:


$VC = Connect-VIServer $VCServerName;
$vms = Get-VM;
$myCol = @();
ForEach ($vm in $vms);{$snapshots = Get-Snapshot -VM $vm;
$myObj = "" | Select-Object VM, NumSnapshots;
$myObj.VM = $vm.name;
$myObj.NumSnapshots = ($snapshots | measure-object).count;
$myCol += $myObj;}$myCol | Sort-Object VM;
 

Message was edited by: hugopeeters

Fixed code

LucD
Leadership
Leadership
Jump to solution

A quick and dirty one-liner

Get-VM | %{$vm = $_.Name ; $_ | Get-Snapshot | Measure-Object | %{write-host $vm $_.Count}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

alanrenouf
VMware Employee
VMware Employee
Jump to solution

A quick and dirty one-liner

</div>

Get-VM | %{$vm = $_.Name ; $_ | Get-Snapshot | Measure-Object | %{write-host $vm $_.Count}}

 

Just what I like... Quick and Dirty :smileysilly:, brilliant thanks.

>

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

A bit less dirty:

Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_).Length}}

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

I dont get numbers next to that one or your other one Hugopeeters

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's because the SnapshotImpl object doesn't have a Length property.

It will only work if an array of SnapshotImpl objects is returned (in other words more than 1 snapshot).

And an array has a Length property.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Oh, sorry about that. I don't have any snapshots here to test it with. I should have used:

| Measure-Object ).Count

New code:

Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}} 

Oh, and the forum screwed up my mini-script I posted earlier... Smiley Sad

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

That works, Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Anywhere I can put a where NumSnapshots -lt 0 in there somewhere ?

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

It's easy when you use the script. You can do whatever you want with the output object $myCol:

$myCol | Where {$_.NumSnapshots -gt 0} | Sort-Object VM | Format-Table -AutoSize

Let me think about a oneliner...

It slows the script down, but should work:

Get-VM | Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}

Message was edited by: hugopeeters

Added slow oneliner

LucD
Leadership
Leadership
Jump to solution

For my quick-and-dirty onliner

Get-VM | %{$vm = $_.Name ; $_ | Get-Snapshot | Measure-Object | %{if($_.Count){write-host $vm $_.Count}}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

One-liner looks and works great, just what wanted. Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

No problem.

This is fun! I'm considering rewriting all my scripts into massive oneliners Smiley Wink

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

I can see these one-liners getting pretty long :smileygrin:

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

I'll ask my boss for a widescreen monitor :smileylaugh:

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Is there any way I can get these as a property so I can use them in a array ?

Sorry, im sure Im going about this the wrong way but still learning.

kinda...

Get-VM | Where{(Get-SnapShot -VM $_ | Measure-Object).Count -gt 0} | Format-Table Name, @{Label="Value";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}} | foreach-object {

$categories += $_.Name

$values += $_.Value * 1

}

Which obviosuly doesnt work !

Thanks

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos
hugopeeters
Hot Shot
Hot Shot
Jump to solution

Alanrenouf, use the script I posted earlier. If you dot-source the script, you can use the $myCol variable and manipulate the properties.

The forum software messed it up a bit, but it is also on my website: http://www.peetersonline.nl/index.php/vmware/powershell-oneliner-4/

2008/8/6 alanrenouf <communities-emailer@vmware.com<mailto:communities-emailer@vmware.com>>

Hugo Peeters,

A new message was posted in the thread "Count the number of snapshots per VM":

http://communities.vmware.com/message/1013705

Author : alanrenouf

Email : ajw.renouf@googlemail.com<mailto:ajw.renouf@googlemail.com>

Profile : http://communities.vmware.com/people/alanrenouf

Message:

0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

Thanks, Will do, I think I will need to adjust my master script a little Smiley Happy

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
0 Kudos