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
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
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
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
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.
>
Thanks
A bit less dirty:
Get-VM | Format-Table Name, @{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_).Length}}
I dont get numbers next to that one or your other one Hugopeeters
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
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...
That works, Thanks
Anywhere I can put a where NumSnapshots -lt 0 in there somewhere ?
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
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
One-liner looks and works great, just what wanted. Thanks
No problem.
This is fun! I'm considering rewriting all my scripts into massive oneliners
I can see these one-liners getting pretty long :smileygrin:
I'll ask my boss for a widescreen monitor :smileylaugh:
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
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:
Thanks, Will do, I think I will need to adjust my master script a little