Skip to content

Commit 3b20b13

Browse files
committed
update
fix for empty vm names
1 parent db1fd47 commit 3b20b13

File tree

2 files changed

+229
-244
lines changed

2 files changed

+229
-244
lines changed

PRTG-VMware-Snapshot.ps1

Lines changed: 79 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -105,20 +105,19 @@ param(
105105
[string]$IncludeSnapName = '',
106106
[int]$WarningHours = 24,
107107
[int]$ErrorHours = 48,
108-
[int]$WarningSize = 10, #in GB
108+
[int]$WarningSize = 10, #in GB
109109
[int]$ErrorSize = 20 #in GB
110110
)
111111

112112
#Catch all unhandled Errors
113-
trap{
114-
if($connected)
115-
{
113+
trap {
114+
if ($connected) {
116115
$null = Disconnect-VIServer -Server $ViServer -Confirm:$false -ErrorAction SilentlyContinue
117-
}
116+
}
118117
$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("#", "")
122121
Write-Output "<prtg>"
123122
Write-Output "<error>1</error>"
124123
Write-Output "<text>$Output</text>"
@@ -131,29 +130,25 @@ trap{
131130
#If Powershell is running the 32-bit version on a 64-bit machine, we
132131
#need to force powershell to run in 64-bit mode .
133132
#############################################################################
134-
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64")
135-
{
136-
if ($myInvocation.Line)
137-
{
133+
if ($env:PROCESSOR_ARCHITEW6432 -eq "AMD64") {
134+
if ($myInvocation.Line) {
138135
[string]$output = &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile $myInvocation.Line
139-
}
140-
else
141-
{
136+
}
137+
else {
142138
[string]$output = &"$env:WINDIR\sysnative\windowspowershell\v1.0\powershell.exe" -NonInteractive -NoProfile -file "$($myInvocation.InvocationName)" $args
143-
}
139+
}
144140

145141
#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+
}
149145

150-
catch
151-
{
152-
}
146+
catch {
147+
}
153148

154149
Write-Output $output
155150
exit
156-
}
151+
}
157152

158153
#############################################################################
159154
#End
@@ -170,7 +165,8 @@ $ErrorActionPreference = "Stop"
170165
# Import VMware PowerCLI module
171166
try {
172167
Import-Module "VMware.VimAutomation.Core" -ErrorAction Stop
173-
} catch {
168+
}
169+
catch {
174170
Write-Output "<prtg>"
175171
Write-Output " <error>1</error>"
176172
Write-Output " <text>Error Loading VMware Powershell Module ($($_.Exception.Message))</text>"
@@ -180,55 +176,53 @@ try {
180176

181177
# Parameter empty = 999
182178
# 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)) {
184180
$WarningHours = 999
185-
}
186-
if(($ErrorHours -eq "") -and ($ErrorHours -ne 0)){
181+
}
182+
if (($ErrorHours -eq "") -and ($ErrorHours -ne 0)) {
187183
$ErrorHours = 999
188-
}
189-
if(($WarningSize -eq "") -and ($WarningSize -ne 0)){
184+
}
185+
if (($WarningSize -eq "") -and ($WarningSize -ne 0)) {
190186
$WarningSize = 999
191-
}
192-
if(($ErrorSize -eq "") -and ($ErrorSize -ne 0)){
187+
}
188+
if (($ErrorSize -eq "") -and ($ErrorSize -ne 0)) {
193189
$ErrorSize = 999
194-
}
190+
}
195191

196192
# PowerCLI Configuration Settings
197-
try
198-
{
193+
try {
199194
#Ignore certificate warnings
200195
Set-PowerCLIConfiguration -InvalidCertificateAction ignore -Scope User -Confirm:$false | Out-Null
201196

202197
#Disable CEIP
203198
Set-PowerCLIConfiguration -ParticipateInCeip $false -Scope User -Confirm:$false | Out-Null
204-
}
199+
}
205200

206-
catch
207-
{
201+
catch {
208202
Write-Output "Error in Set-PowerCLIConfiguration but we will ignore it." #Error when another Script is currently accessing it.
209-
}
203+
}
210204

211205
# Connect to vCenter
212206
try {
213207
$null = Connect-VIServer -Server $ViServer -User $User -Password $Password
214208

215209
$connected = $true
216-
}
210+
}
217211

218-
catch
219-
{
212+
catch {
220213
Write-Output "<prtg>"
221214
Write-Output "<error>1</error>"
222215
Write-Output "<text>Could not connect to vCenter server $ViServer. Error: $($_.Exception.Message)</text>"
223216
Write-Output "</prtg>"
224217
Exit
225-
}
218+
}
226219

227220
# Get a list of all VMs
228221
try {
229222
$VMs = Get-VM
230223

231-
} catch {
224+
}
225+
catch {
232226
Write-Output "<prtg>"
233227
Write-Output "<error>1</error>"
234228
Write-Output "<text>Could not Get-VM. Error: $($_.Exception.Message)</text>"
@@ -243,77 +237,76 @@ $IncludeVMNameScript = ''
243237

244238
#VM Name
245239
if ($ExcludeVMName -ne "") {
246-
$VMs = $VMs | Where-Object {$_.Name -notmatch $ExcludeVMName}
240+
$VMs = $VMs | Where-Object { $_.Name -notmatch $ExcludeVMName }
247241
}
248242

249243
if ($ExcludeVMNameScript -ne "") {
250-
$VMs = $VMs | Where-Object {$_.Name -notmatch $ExcludeVMNameScript}
244+
$VMs = $VMs | Where-Object { $_.Name -notmatch $ExcludeVMNameScript }
251245
}
252246

253247
if ($IncludeVMName -ne "") {
254-
$VMs = $VMs | Where-Object {$_.Name -match $IncludeVMName}
248+
$VMs = $VMs | Where-Object { $_.Name -match $IncludeVMName }
255249
}
256250

257251
if ($IncludeVMNameScript -ne "") {
258-
$VMs = $VMs | Where-Object {$_.Name -match $IncludeVMNameScript}
252+
$VMs = $VMs | Where-Object { $_.Name -match $IncludeVMNameScript }
259253
}
260254

261255
#VM Folder
262256
if ($ExcludeFolder -ne "") {
263-
$VMs = $VMs | Where-Object {$_.Folder.Name -notmatch $ExcludeFolder}
257+
$VMs = $VMs | Where-Object { $_.Folder.Name -notmatch $ExcludeFolder }
264258
}
265259

266260
if ($IncludeFolder -ne "") {
267-
$VMs = $VMs | Where-Object {$_.Folder.Name -match $IncludeFolder}
261+
$VMs = $VMs | Where-Object { $_.Folder.Name -match $IncludeFolder }
268262
}
269263

270264
#VM Resource
271265
if ($ExcludeRessource -ne "") {
272-
$VMs = $VMs | Where-Object {$_.ResourcePool.Name -notmatch $ExcludeRessource}
266+
$VMs = $VMs | Where-Object { $_.ResourcePool.Name -notmatch $ExcludeRessource }
273267
}
274268

275269
if ($IncludeRessource -ne "") {
276-
$VMs = $VMs | Where-Object {$_.ResourcePool.Name -match $IncludeRessource}
270+
$VMs = $VMs | Where-Object { $_.ResourcePool.Name -match $IncludeRessource }
277271
}
278272

279273
#VM Host
280274
if ($ExcludeVMHost -ne "") {
281-
$VMs = $VMs | Where-Object {$_.VMHost.Name -notmatch $ExcludeVMHost}
275+
$VMs = $VMs | Where-Object { $_.VMHost.Name -notmatch $ExcludeVMHost }
282276
}
283277

284278
if ($IncludeVMHost -ne "") {
285-
$VMs = $VMs | Where-Object {$_.VMHost.Name -match $IncludeVMHost}
279+
$VMs = $VMs | Where-Object { $_.VMHost.Name -match $IncludeVMHost }
286280
}
287281
#End Region VM Filter
288282

289283
# Get Snapshots from every VM
290284
$AllSnaps = New-Object -TypeName "System.Collections.ArrayList"
291285
foreach ($VM in $VMs) {
292286
$Snaps = Get-Snapshot -VM $VM -ErrorAction SilentlyContinue
293-
foreach($Snap in $Snaps)
294-
{
287+
foreach ($Snap in $Snaps) {
295288
# Save VM names for later use
296289
$null = $AllSnaps.Add($Snap)
297-
}
290+
}
298291
}
299292

300293
# Snapshot filter (include/exclude)
301294
# Snapshot Name
302295
if ($ExcludeSnapName -ne "") {
303-
$AllSnaps = $AllSnaps | Where-Object {$_.Name -notmatch $ExcludeSnapName}
296+
$AllSnaps = $AllSnaps | Where-Object { $_.Name -notmatch $ExcludeSnapName }
304297
}
305298

306299
if ($IncludeSnapName -ne "") {
307-
$AllSnaps = $AllSnaps | Where-Object {$_.Name -match $IncludeSnapName}
300+
$AllSnaps = $AllSnaps | Where-Object { $_.Name -match $IncludeSnapName }
308301
}
309302

310303
# Snapshot Description
311304
if ($ExcludeSnapDescription -ne "") {
312-
$AllSnaps = $AllSnaps | Where-Object {$_.Description -notmatch $ExcludeSnapDescription}
305+
$AllSnaps = $AllSnaps | Where-Object { $_.Description -notmatch $ExcludeSnapDescription }
313306
}
314307

315308
if ($IncludeSnapDescription -ne "") {
316-
$AllSnaps = $AllSnaps | Where-Object {$_.Description -match $IncludeSnapDescription}
309+
$AllSnaps = $AllSnaps | Where-Object { $_.Description -match $IncludeSnapDescription }
317310
}
318311

319312

@@ -324,39 +317,40 @@ $MaxAge = 0
324317
$MaxSizeMB = 0
325318

326319

327-
foreach ($Snap in $AllSnaps){
320+
foreach ($Snap in $AllSnaps) {
328321
$date = $snap.created -as [DateTime]
329322
$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+
333331
#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) {
337334
$MaxSizeMB = $TempSize
338-
}
335+
}
339336

340337
#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) {
344340
$MaxAge = $TempAge
345-
}
341+
}
346342

347343
#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))) {
350345
$ErrorVMs += "VM=$($name) Created=$($dateoutput) Size=$($size)GB; "
351-
$ErrorCount +=1
352-
}
346+
$ErrorCount += 1
347+
}
353348

354349
#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+
}
360354
}
361355

362356

@@ -369,13 +363,13 @@ $connected = $false
369363
$xmlOutput = '<prtg>'
370364
if ($ErrorCount -ge 1) {
371365
$xmlOutput = $xmlOutput + "<text>Error - $($ErrorVMs)</text>"
372-
}
366+
}
373367

374-
if (($WarningCount -ge 1) -and ($ErrorCount -eq 0)){
368+
if (($WarningCount -ge 1) -and ($ErrorCount -eq 0)) {
375369
$xmlOutput = $xmlOutput + "<text>Warning - $($WarningVMs)</text>"
376-
}
370+
}
377371

378-
if(($WarningCount -eq 0) -and ($ErrorCount -eq 0)) {
372+
if (($WarningCount -eq 0) -and ($ErrorCount -eq 0)) {
379373
$xmlOutput = $xmlOutput + "<text>No Snapshots older $($WarningHours) hours or greater $($WarningSize)GB</text>"
380374
}
381375

0 commit comments

Comments
 (0)