How to find number of snapshots for a vm using powershell - powershell

I am new to PowerShell scripting. I am trying to get number of snapshots have been created for a VM. I am able to get snapshots information of the VM's using the below command.
get-vm Test_sub | Get-Snapshot
It is giving complete information but is there any way I can count the number of snapshots?
Thanks,
Sasikumar.

If Get-Snapshot returns one output for each snapshot then use Measure-Object (alias Measure) to see the count e.g.:
get-vm Test_sub | Get-Snapshot | Measure
If you need the value in a script then:
$count = (get-vm Test_sub | Get-Snapshot).length

Related

Merge Powershell Output - Get-VM, Get-VHD

I would like to export some Server Statistics from a Failover Cluster System.
My plan is to show with the Get-VHD command all VHD used on the VMs on my host.
So I was Trying to use:
Get-VM | select-object VMID |get-vhd |ft
This gives me a list the
"ComputerName, Path, VHDFormat,VHDType,FileSize,MinimumSize, LogicalSectorSize, PhysicalSectorSize"
Sadly the ComputerName is not the VMName but just the Name of the Host.
Now when I run the Get-VM command I get the Name and here it is actually the VMName.
Is there a nice way to get the real VMnames in the Output of Get-VHD?
I am fairly new to Powershell and I could not find a solution that worked... Most of the "sniplets" found here did not work at all or did not return the VMname...
Thank you for your suggestions =)
Sorry, I think my Title is not that well described, I was not sure how this funktion is called.
Untested, but you could use a ForEach-Object loop and capture the Name property from the Get-VM cmdlet. Then go on with Get-VHD and combine the output:
Get-VM | ForEach-Object {
$name = $_.Name
$_ | Get-VHD | Select-Object #{Name = 'Name'; Expression = {$name}}, *
} | Format-Table -AutoSize

Remove Multiple Snapshots at once in PowerCLI

I am trying to delete old snapshots after patching using PowerCLI. The code i am using now is:
Get-VM | Get-Snapshot | Remove-Snapshot -confirm$false
It works great...but it only removes one at a time, and i would like it to do 2-3 at a time. Is this possible?
Thanks in advance!
This code will remove multiple snapshots from all virtual machines:
Get-VM | Get-Snapshot | % { Remove-Snapshot $_ -Confirm:$false }
I would recommend selecting a single virtual machine and testing first:
$VM = Get-VM -Name 'My Virtual Machine'
$VM | Get-Snapshot | % { Remove-Snapshot $_ -Confirm:$false }
Tested to work on PowerCLI 6.5.
I would recommend taking a look at the 'RunAsync' parameter. This will create the task and then move onto the next task without waiting for the prior task to complete.
Example:
Get-VM | Get-Snapshot | Remove-Snapshot -RunAsync -Confirm:$false

Get the size to a snapshot via Powershell in Hyper-V

Good Evening, I am developing a C# too, that is supposed to help users to control VMs on a server.
Now I have created the functions to create and erase Snapshots of these VMs and want to create a function to list all Snapshots of a VM including a view selected Snapshot parameter. With:
Invoke-Command -ComputerName ServerName -ScriptBlock { Get-VMSnapshot -VMName VMName |Select-Object}
I am able to get nearly all information that are requested but the total size of the Snapshot itself.
However I could get the size with this command:
Invoke-Command -ComputerName ServerName -ScriptBlock {Get-VM -VMName VMName | gci -r -Filter *.avhd*| select #{Name="MB";Expression={$_.Length / 1Mb}}, *}
Not to get the two results together I compared the creation time of these two with a tolerance of up to 10 seconds and took the closest one. (Primitive I know), but now it becomes even more complicated.
If I now use a Snapshot to set the VM to this state, the time of the File creation changes and so my previous method stops working completely.
Why I want to know is, is there a better way to compare these two results or even better, to get the Snapshot size without such primitive comparisons?
Thanks in advance for the help.
Below one-liners should suffice your need.
1)
Get-VM | Sort Name | Get-Snapshot | Where { $_.Name.Length -gt 0 } | Select VM,Name,Created,SizeMB
2)
Get-VM | Format-Table Name, #{Label="NumSnapshots";Expression={(Get-Snapshot -VM $_ | Measure-Object).Count}}, #{Label="TotalSnapShotSizeMB";Expression={(Get-Snapshot -VM $_ | Measure-Object -Sum SizeMB).Sum}}
Hope it helps.

Powershell syntax help needed

I have a Powershell command that for automation purposes in our monitoring system, I'm trying to write in one line.
Basically, we're checking multiple Hyper-V virtual machines to make sure they are replicating properly. The way we're doing this is checking the last replication date/time and comparing it to the current date/time. Here's the one-liner I have for checking one machine.
(new-timespan -start(get-vm termserv002| Measure-VMReplication | select -expand lastreplicationtime)).totalminutes
This works perfectly. However, we'd like to check ALL of the VMs on this Hyper-V box. I can do this without running the new-timespan by running this command -
get-vm | where {$_.replicationstate -ne "Disabled"} | Measure-VMReplication | select vmname, lastreplicationtime
This gives me every VM on the box and its last replication time.
Ultimately, what I'd like for output is the VMName and LastReplicationTime in minutes from current date/time. If I can do this on one line, homerun!
Any suggestions? No matter how I try to meld the two together, I just can't seem to get the syntax right (Or it's not possible.)
I can't test it but give this a try:
get-vm | where {$_.replicationstate -ne "Disabled"} | Measure-VMReplication |
select vmname, #{n="LastReplicationTime";e={ (new-timespan -start ($_.lastreplicationtime) -end (get-date)).TotalMinutes }}

Powershell remove-snapshot cmdlet

I'm working on a small script that will get all the snapshots in a VM and remove all snapshots in the VM except for the 6 newest snapshots, based off the description.
Right now my code looks like this:
get-snapshot -vm "test" | sort -property description | remove-snapshot ?
I am using get-date to make the description for each VM be the date on which it was created, and want to remove all but the 6 newest snapshots. What am I missing with my script to accomplish this task?
I was thinking of using the -getchildren however I'm unable to figure out how to get it to where it would delete snapshots 7 and on.
A snapshot has a property called Created so you can sort on this property and skip the first 6. Test this in a test environment and remove the WhatIf switch to delete the snapshots.
Get-Snapshot -VM test |
Sort-Object Created |
Select-Object -Skip 6 |
Remove-Snapshot -Confirm:$false -WhatIf