snapshotCleanup.ps1 4.1 KB
Newer Older
DaFyre's avatar
DaFyre committed
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
<#
.SYNOPSIS
This command is meant to add clean up snapshots that are generated on a Daily basis by default.  Parameters can be used to change the scope of the searches.

.DESCRIPTION

Date        Updated By  Details
05-21-2020      BW      Modified to show whether we are listing or removing snapshots.
11-07-2019      BW      Updated script to track number of snapshots to delete per VM.
10-30-2019      BW      Added Logging mechanism with start/stop-transcript
10-29-2019      BW      Initial release.  Parameters set to clean Any daily snaphots older than 8 days.

#>


Param(
    [Parameter(Mandatory = $false) ] [string] $vcServer="",
    [Parameter(Mandatory = $false) ] [string[]] $vmName="",
    [Parameter(Mandatory = $false) ] [string] $cleanupMatch="Daily Snapshot",
    [Parameter(Mandatory = $false) ] [string] $keepDate=(get-Date).adddays(-8),

    [Parameter(Mandatory = $false) ] [pscredential] $vcCred,
    [Parameter(Mandatory = $false) ] [switch] $cleanAll,
    [Parameter(Mandatory = $false) ] [bool] $doConfirm=$false, #has to be a [bool] because of -doConfirm in the remove-snapshot bits.
    [Parameter(Mandatory = $false) ] [switch] $liveRun,
    [Parameter(Mandatory = $false) ] [string] $logFile

)


#the default progress bar is useless for this.  Disable it!
$origProgressBar = $ProgressPreference
$ProgressPreference = "SilentlyContinue"
if ($liveRun -eq $false) {$action="Listing"} else {$action = "Cleaning"}
#vmware initialization

$hasVI=get-command connect-viserver* -ErrorAction SilentlyContinue

if ([string]::IsNullOrEmpty($hasVI) -eq $true) {
    try {
        import-module VMware.PowerCLI
    }
    catch {
        write-host -foregroundcolor "Yellow" "You MUST have the VMware PowerShell Modules installed for this to work."
        write-host -foregroundcolor "yellow" "Open PowerShell as an administrator and then "
        write-host " "
        write-host -foregroundcolor "white" "install-module vmware.powercli"
        write-host " "
        write-host -foregroundcolor "Yellow" "And then try this script again."

        exit -2048;
    }
}
 

if ($global:DefaultVIServers.count -lt 1) { 

    if ([string]::IsNullOrEmpty($vcServer) -eq $true ) {
        write-host -foreground Yellow "vCenter Server Connection"
        $vcServer = read-host "vCenter Server: "
    
    } 

    if ($vcCred -eq $null ) {
    
        write-host -foreground Yellow "Please enter your VCenter Admin Credentials..."
        $username = read-host "Username: "
        $password = read-host "Password: " -AsSecureString
        $VCCred = New-Object System.Management.Automation.PSCredential -ArgumentList $username, $password

    }

    try {
         connect-viserver -server $vcServer -Credential $vcCred 
    } catch {
        write-host "There was an error connecting to $vcServer!"
        error[0]
        exit -3072
    }

}

if ($cleanAll -eq $true) {
    write-host "Loading All Powered On VMs..."
    $vmName=(get-vm | where {$_.PowerState -eq 'PoweredOn'} | Select Name).Name
    
    write-host "Found $($vmName.count) machines to clean..."
}

$logFile="snapshotCleanup_$(get-date -format "MMddyyyy_HHmmss").log"

if ($liveRun -eq $true) {start-transcript -Path $logFIle -Append }
foreach ($vm in $vmName) {
    write-host "$action snapshots for $vm"
    if ($liveRun -eq $true) {
        $objVM=get-vm $vm
        $objSnapshots=$objVM | get-snapshot|where {$_.Name -like "*$cleanupMatch*" -and $_.created -lt $keepDate}
        $pass=0
         #| remove-snapshot -confirm:$doConfirm 
         $snapCount=$objSnapshots.count
         foreach ($snap in $objSnapshots) {
             $pass++
             write-host -nonewline "Cleaning snapshot $pass of $snapCount`r" 
             $snap|remove-snapshot -confirm:$doConfirm
         }
    } else {
        get-vm $vm | get-snapshot | where { $_.Name -like "*$cleanupMatch*" -and $_.created -lt $keepDate } | Select Created,Name|ft

    }
    write-host "Done $action snapshots for $vm"
    write-host " "
}
if ($liveRun -eq $true) { Stop-Transcript }

$ProgressPreference=$origProgressBar