-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVMSnapshot.ps1
53 lines (39 loc) · 1.76 KB
/
VMSnapshot.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
param(
[string] $schedule = "daily"
)
If ( ! (Get-module VMware.VimAutomation.core )) {
get-module -name VMware* -ListAvailable | Import-Module
}
#Stop an error from occurring when a transcript is already stopped
$ErrorActionPreference = "SilentlyContinue"
Stop-Transcript | out-null
#Reset the error level before starting the transcript
$ErrorActionPreference = "Continue"
Start-Transcript -path C:\temp\VMSnapshot.log -append
#Get the Credentials
$creds = Get-VICredentialStoreItem -file C:\Users\Administrator\In.creds
#Connect to the server using the credentials file
Connect-VIServer -Server $creds.host -User $creds.User -Password $creds.Password
#Get all VMs having ScheduleSnapshot set to $schedule
$VMs = @(Get-VM | Get-Annotation -CustomAttribute "SnapshotSchedule" |
Where-Object { $_.Value -eq $schedule } | Sort-Object AnnotatedEntity)
ForEach ($VM in $VMs) {
$name = $VM.AnnotatedEntity.Name
$snapName = Get-Date -Format "yyyyMMdd-HHmm"
# Creating snapshot
Write-Host "Creating snapshot for VM $name as $snapName"
New-Snapshot -VM $VM.AnnotatedEntity -Name $snapName -Quiesce:$true -Confirm:$false
# Cleaning up by removing oldest snaps
$SNAPs = Get-Snapshot -VM $VM.AnnotatedEntity
$Retain = (Get-Annotation -Entity $VM.AnnotatedEntity -CustomAttribute "SnapshotRetain").Value -as [int]
if ($SNAPs.Length -gt $Retain) {
Write-Host "Removing oldest snapshots for VM $name"
Remove-Snapshot -Snapshot (
$SNAPs | Sort-Object -Descending Created |
Select-Object -Last ($SNAPs.Length - $Retain)
) -Confirm:$false
}
}
#Disconnect
Disconnect-VIServer -Force -Confirm:$false
Stop-Transcript