@@ -105,20 +105,19 @@ param(
105
105
[string ]$IncludeSnapName = ' ' ,
106
106
[int ]$WarningHours = 24 ,
107
107
[int ]$ErrorHours = 48 ,
108
- [int ]$WarningSize = 10 , # in GB
108
+ [int ]$WarningSize = 10 , # in GB
109
109
[int ]$ErrorSize = 20 # in GB
110
110
)
111
111
112
112
# Catch all unhandled Errors
113
- trap {
114
- if ($connected )
115
- {
113
+ trap {
114
+ if ($connected ) {
116
115
$null = Disconnect-VIServer - Server $ViServer - Confirm:$false - ErrorAction SilentlyContinue
117
- }
116
+ }
118
117
$Output = " line:$ ( $_.InvocationInfo.ScriptLineNumber.ToString ()) char:$ ( $_.InvocationInfo.OffsetInLine.ToString ()) --- message: $ ( $_.Exception.Message.ToString ()) --- line: $ ( $_.InvocationInfo.Line.ToString ()) "
119
- $Output = $Output.Replace (" <" , " " )
120
- $Output = $Output.Replace (" >" , " " )
121
- $Output = $Output.Replace (" #" , " " )
118
+ $Output = $Output.Replace (" <" , " " )
119
+ $Output = $Output.Replace (" >" , " " )
120
+ $Output = $Output.Replace (" #" , " " )
122
121
Write-Output " <prtg>"
123
122
Write-Output " <error>1</error>"
124
123
Write-Output " <text>$Output </text>"
@@ -131,29 +130,25 @@ trap{
131
130
# If Powershell is running the 32-bit version on a 64-bit machine, we
132
131
# need to force powershell to run in 64-bit mode .
133
132
# ############################################################################
134
- if ($env: PROCESSOR_ARCHITEW6432 -eq " AMD64" )
135
- {
136
- if ($myInvocation.Line )
137
- {
133
+ if ($env: PROCESSOR_ARCHITEW6432 -eq " AMD64" ) {
134
+ if ($myInvocation.Line ) {
138
135
[string ]$output = & " $env: WINDIR \sysnative\windowspowershell\v1.0\powershell.exe" - NonInteractive - NoProfile $myInvocation.Line
139
- }
140
- else
141
- {
136
+ }
137
+ else {
142
138
[string ]$output = & " $env: WINDIR \sysnative\windowspowershell\v1.0\powershell.exe" - NonInteractive - NoProfile - file " $ ( $myInvocation.InvocationName ) " $args
143
- }
139
+ }
144
140
145
141
# Remove any text after </prtg>
146
- try {
147
- $output = $output.Substring (0 , $output.LastIndexOf (" </prtg>" )+ 7 )
148
- }
142
+ try {
143
+ $output = $output.Substring (0 , $output.LastIndexOf (" </prtg>" ) + 7 )
144
+ }
149
145
150
- catch
151
- {
152
- }
146
+ catch {
147
+ }
153
148
154
149
Write-Output $output
155
150
exit
156
- }
151
+ }
157
152
158
153
# ############################################################################
159
154
# End
@@ -170,7 +165,8 @@ $ErrorActionPreference = "Stop"
170
165
# Import VMware PowerCLI module
171
166
try {
172
167
Import-Module " VMware.VimAutomation.Core" - ErrorAction Stop
173
- } catch {
168
+ }
169
+ catch {
174
170
Write-Output " <prtg>"
175
171
Write-Output " <error>1</error>"
176
172
Write-Output " <text>Error Loading VMware Powershell Module ($ ( $_.Exception.Message ) )</text>"
@@ -180,55 +176,53 @@ try {
180
176
181
177
# Parameter empty = 999
182
178
# if you dont need size or hours just leave it empty
183
- if (($WarningHours -eq " " ) -and ($WarningHours -ne 0 )){
179
+ if (($WarningHours -eq " " ) -and ($WarningHours -ne 0 )) {
184
180
$WarningHours = 999
185
- }
186
- if (($ErrorHours -eq " " ) -and ($ErrorHours -ne 0 )){
181
+ }
182
+ if (($ErrorHours -eq " " ) -and ($ErrorHours -ne 0 )) {
187
183
$ErrorHours = 999
188
- }
189
- if (($WarningSize -eq " " ) -and ($WarningSize -ne 0 )){
184
+ }
185
+ if (($WarningSize -eq " " ) -and ($WarningSize -ne 0 )) {
190
186
$WarningSize = 999
191
- }
192
- if (($ErrorSize -eq " " ) -and ($ErrorSize -ne 0 )){
187
+ }
188
+ if (($ErrorSize -eq " " ) -and ($ErrorSize -ne 0 )) {
193
189
$ErrorSize = 999
194
- }
190
+ }
195
191
196
192
# PowerCLI Configuration Settings
197
- try
198
- {
193
+ try {
199
194
# Ignore certificate warnings
200
195
Set-PowerCLIConfiguration - InvalidCertificateAction ignore - Scope User - Confirm:$false | Out-Null
201
196
202
197
# Disable CEIP
203
198
Set-PowerCLIConfiguration - ParticipateInCeip $false - Scope User - Confirm:$false | Out-Null
204
- }
199
+ }
205
200
206
- catch
207
- {
201
+ catch {
208
202
Write-Output " Error in Set-PowerCLIConfiguration but we will ignore it." # Error when another Script is currently accessing it.
209
- }
203
+ }
210
204
211
205
# Connect to vCenter
212
206
try {
213
207
$null = Connect-VIServer - Server $ViServer - User $User - Password $Password
214
208
215
209
$connected = $true
216
- }
210
+ }
217
211
218
- catch
219
- {
212
+ catch {
220
213
Write-Output " <prtg>"
221
214
Write-Output " <error>1</error>"
222
215
Write-Output " <text>Could not connect to vCenter server $ViServer . Error: $ ( $_.Exception.Message ) </text>"
223
216
Write-Output " </prtg>"
224
217
Exit
225
- }
218
+ }
226
219
227
220
# Get a list of all VMs
228
221
try {
229
222
$VMs = Get-VM
230
223
231
- } catch {
224
+ }
225
+ catch {
232
226
Write-Output " <prtg>"
233
227
Write-Output " <error>1</error>"
234
228
Write-Output " <text>Could not Get-VM. Error: $ ( $_.Exception.Message ) </text>"
@@ -243,77 +237,76 @@ $IncludeVMNameScript = ''
243
237
244
238
# VM Name
245
239
if ($ExcludeVMName -ne " " ) {
246
- $VMs = $VMs | Where-Object {$_.Name -notmatch $ExcludeVMName }
240
+ $VMs = $VMs | Where-Object { $_.Name -notmatch $ExcludeVMName }
247
241
}
248
242
249
243
if ($ExcludeVMNameScript -ne " " ) {
250
- $VMs = $VMs | Where-Object {$_.Name -notmatch $ExcludeVMNameScript }
244
+ $VMs = $VMs | Where-Object { $_.Name -notmatch $ExcludeVMNameScript }
251
245
}
252
246
253
247
if ($IncludeVMName -ne " " ) {
254
- $VMs = $VMs | Where-Object {$_.Name -match $IncludeVMName }
248
+ $VMs = $VMs | Where-Object { $_.Name -match $IncludeVMName }
255
249
}
256
250
257
251
if ($IncludeVMNameScript -ne " " ) {
258
- $VMs = $VMs | Where-Object {$_.Name -match $IncludeVMNameScript }
252
+ $VMs = $VMs | Where-Object { $_.Name -match $IncludeVMNameScript }
259
253
}
260
254
261
255
# VM Folder
262
256
if ($ExcludeFolder -ne " " ) {
263
- $VMs = $VMs | Where-Object {$_.Folder.Name -notmatch $ExcludeFolder }
257
+ $VMs = $VMs | Where-Object { $_.Folder.Name -notmatch $ExcludeFolder }
264
258
}
265
259
266
260
if ($IncludeFolder -ne " " ) {
267
- $VMs = $VMs | Where-Object {$_.Folder.Name -match $IncludeFolder }
261
+ $VMs = $VMs | Where-Object { $_.Folder.Name -match $IncludeFolder }
268
262
}
269
263
270
264
# VM Resource
271
265
if ($ExcludeRessource -ne " " ) {
272
- $VMs = $VMs | Where-Object {$_.ResourcePool.Name -notmatch $ExcludeRessource }
266
+ $VMs = $VMs | Where-Object { $_.ResourcePool.Name -notmatch $ExcludeRessource }
273
267
}
274
268
275
269
if ($IncludeRessource -ne " " ) {
276
- $VMs = $VMs | Where-Object {$_.ResourcePool.Name -match $IncludeRessource }
270
+ $VMs = $VMs | Where-Object { $_.ResourcePool.Name -match $IncludeRessource }
277
271
}
278
272
279
273
# VM Host
280
274
if ($ExcludeVMHost -ne " " ) {
281
- $VMs = $VMs | Where-Object {$_.VMHost.Name -notmatch $ExcludeVMHost }
275
+ $VMs = $VMs | Where-Object { $_.VMHost.Name -notmatch $ExcludeVMHost }
282
276
}
283
277
284
278
if ($IncludeVMHost -ne " " ) {
285
- $VMs = $VMs | Where-Object {$_.VMHost.Name -match $IncludeVMHost }
279
+ $VMs = $VMs | Where-Object { $_.VMHost.Name -match $IncludeVMHost }
286
280
}
287
281
# End Region VM Filter
288
282
289
283
# Get Snapshots from every VM
290
284
$AllSnaps = New-Object - TypeName " System.Collections.ArrayList"
291
285
foreach ($VM in $VMs ) {
292
286
$Snaps = Get-Snapshot - VM $VM - ErrorAction SilentlyContinue
293
- foreach ($Snap in $Snaps )
294
- {
287
+ foreach ($Snap in $Snaps ) {
295
288
# Save VM names for later use
296
289
$null = $AllSnaps.Add ($Snap )
297
- }
290
+ }
298
291
}
299
292
300
293
# Snapshot filter (include/exclude)
301
294
# Snapshot Name
302
295
if ($ExcludeSnapName -ne " " ) {
303
- $AllSnaps = $AllSnaps | Where-Object {$_.Name -notmatch $ExcludeSnapName }
296
+ $AllSnaps = $AllSnaps | Where-Object { $_.Name -notmatch $ExcludeSnapName }
304
297
}
305
298
306
299
if ($IncludeSnapName -ne " " ) {
307
- $AllSnaps = $AllSnaps | Where-Object {$_.Name -match $IncludeSnapName }
300
+ $AllSnaps = $AllSnaps | Where-Object { $_.Name -match $IncludeSnapName }
308
301
}
309
302
310
303
# Snapshot Description
311
304
if ($ExcludeSnapDescription -ne " " ) {
312
- $AllSnaps = $AllSnaps | Where-Object {$_.Description -notmatch $ExcludeSnapDescription }
305
+ $AllSnaps = $AllSnaps | Where-Object { $_.Description -notmatch $ExcludeSnapDescription }
313
306
}
314
307
315
308
if ($IncludeSnapDescription -ne " " ) {
316
- $AllSnaps = $AllSnaps | Where-Object {$_.Description -match $IncludeSnapDescription }
309
+ $AllSnaps = $AllSnaps | Where-Object { $_.Description -match $IncludeSnapDescription }
317
310
}
318
311
319
312
@@ -324,39 +317,40 @@ $MaxAge = 0
324
317
$MaxSizeMB = 0
325
318
326
319
327
- foreach ($Snap in $AllSnaps ){
320
+ foreach ($Snap in $AllSnaps ) {
328
321
$date = $snap.created -as [DateTime ]
329
322
$dateoutput = (Get-Date - Date $date - Format " dd.MM.yy-HH:mm" ).ToString()
330
- $size = [math ]::Round(($Snap.SizeGB ), 2 )
331
- $name = ($Snap.VM ).ToString()
332
-
323
+ $size = [math ]::Round(($Snap.SizeGB ), 2 )
324
+ if ($null -eq $Snap.VM ) {
325
+ $name = " None_$ ( $Snap.VMId ) "
326
+ }
327
+ else {
328
+ $name = ($Snap.VM ).ToString()
329
+ }
330
+
333
331
# Max Snap Size
334
- $TempSize = [math ]::Round(($Snap.SizeMB * 1048576 ), 0 )
335
- if ($TempSize -gt $MaxSizeMB )
336
- {
332
+ $TempSize = [math ]::Round(($Snap.SizeMB * 1048576 ), 0 )
333
+ if ($TempSize -gt $MaxSizeMB ) {
337
334
$MaxSizeMB = $TempSize
338
- }
335
+ }
339
336
340
337
# Max Snap Age
341
- $TempAge = [math ]::Round((((Get-Date ) - $date ).TotalSeconds), 0 )
342
- if ($TempAge -gt $MaxAge )
343
- {
338
+ $TempAge = [math ]::Round((((Get-Date ) - $date ).TotalSeconds), 0 )
339
+ if ($TempAge -gt $MaxAge ) {
344
340
$MaxAge = $TempAge
345
- }
341
+ }
346
342
347
343
# Check Error Limit
348
- if (($Snap.SizeGB -ge $ErrorSize ) -or ($date -le (Get-Date ).AddHours(- $ErrorHours )))
349
- {
344
+ if (($Snap.SizeGB -ge $ErrorSize ) -or ($date -le (Get-Date ).AddHours(- $ErrorHours ))) {
350
345
$ErrorVMs += " VM=$ ( $name ) Created=$ ( $dateoutput ) Size=$ ( $size ) GB; "
351
- $ErrorCount += 1
352
- }
346
+ $ErrorCount += 1
347
+ }
353
348
354
349
# Check Warning Limit
355
- elseif (($Snap.SizeGB -ge $WarningSize ) -or ($date -le (Get-Date ).AddHours(- $WarningHours )))
356
- {
357
- $WarningVMs += " VM=$ ( $name ) Created=$ ( $dateoutput ) Size=$ ( $size ) GB; "
358
- $WarningCount += 1
359
- }
350
+ elseif (($Snap.SizeGB -ge $WarningSize ) -or ($date -le (Get-Date ).AddHours(- $WarningHours ))) {
351
+ $WarningVMs += " VM=$ ( $name ) Created=$ ( $dateoutput ) Size=$ ( $size ) GB; "
352
+ $WarningCount += 1
353
+ }
360
354
}
361
355
362
356
@@ -369,13 +363,13 @@ $connected = $false
369
363
$xmlOutput = ' <prtg>'
370
364
if ($ErrorCount -ge 1 ) {
371
365
$xmlOutput = $xmlOutput + " <text>Error - $ ( $ErrorVMs ) </text>"
372
- }
366
+ }
373
367
374
- if (($WarningCount -ge 1 ) -and ($ErrorCount -eq 0 )){
368
+ if (($WarningCount -ge 1 ) -and ($ErrorCount -eq 0 )) {
375
369
$xmlOutput = $xmlOutput + " <text>Warning - $ ( $WarningVMs ) </text>"
376
- }
370
+ }
377
371
378
- if (($WarningCount -eq 0 ) -and ($ErrorCount -eq 0 )) {
372
+ if (($WarningCount -eq 0 ) -and ($ErrorCount -eq 0 )) {
379
373
$xmlOutput = $xmlOutput + " <text>No Snapshots older $ ( $WarningHours ) hours or greater $ ( $WarningSize ) GB</text>"
380
374
}
381
375
0 commit comments