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
Related
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.
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
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 }}
I'm trying to find a way with PowerCLI to get a list of ISO's that are mounted in VM's and change the device type to "client device." Originally I thought that using the get-datastore would work to find the ISO's, however I was struggling to find ISO's, and ran into this line of code while doing some searching:
(Get-VM | Get-View | Where {$_.Runtime.ToolsInstallerMounted}) | % {$_.Name}
I tried this, however I didn't get a list of ISO's mounted. Was wondering if anyone here might know how to get a list of ISO's mounted on VM's and change the device type to client through scripting.
Try this, remove WhatIf to remove the ISO:
Get-VM |
Get-CDDRive |
Where-Object {$_.IsoPath} |
Set-CDDrive -NoMedia -Confirm:$false -WhatIf
I am using Powershell scripts to deploy the codebase on our remote servers.
One Major part of the script copies the current release to the server. Now I just need to keep the last two releases on the remote server and delete all others.
I Need to keep the latest two releases
Eg:
In the remote server, I have
//server001/Application/
Build_1_0_0_19
Build_1_0_0_18
Build_1_0_0_17
Build_1_0_0_16
I need to clear Builds _17 and _16 while deploying _19.
We can sort out the directories according to the time modified and the last two will come on top. Rest all are not required.
Can this be done through Powershell Scripts ?
P.S. The builds are not always in sequential order
You can do something like this:
#requires -version 2
Get-ChildItem //server001/Application/|
Sort-Object CreationTime -Descending|
Select-Object -Skip 2|
Remove-Item -Recurse -Confirm
Just remove the -Confirm switch once you are sure that it does what you want.
Here is a v1 compatible method:
$dirs = #(Get-ChildItem //server001/Application/)
$dirs|
Sort-Object CreationTime -Descending|
Select-Object -Last ($dirs.Count - 2)|
Remove-Item -Recurse -Confirm