-
Notifications
You must be signed in to change notification settings - Fork 0
/
WindowsBackup.ps1
60 lines (50 loc) · 1.93 KB
/
WindowsBackup.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
54
55
56
57
58
59
60
# PRTG Sensor for Windows Backup
# see https://github.com/halliba/PRTGWindowsBackupSensor
$ErrorActionPreference = "Stop";
$hostname = $args[0];
$domain = $args[1];
$username = $args[2];
$password = $args[3];
#create credentials
$secPassword = $password | ConvertTo-SecureString -AsPlainText -Force;
$userDomainName = $domain + '\' + $username;
$creds = New-Object -TypeName System.Management.Automation.PSCredential ($userDomainName, $secPassword);
#add hostname to trusted hosts to connect without ssl
Set-Item WSMan:\localhost\Client\TrustedHosts -Value $hostname -Concatenate -Force;
#get backup summary from host
$backupSummary = Invoke-Command -ComputerName $hostname -Credential $creds -ScriptBlock {
Add-PSSnapin windows.ServerBackup -ErrorAction SilentlyContinue; #required for Windows Server 2008 / 2008 R2
Get-WBSummary;
};
#calculate hours since last backup
$lastBackup = (Get-Date) - ($backupSummary.LastSuccessfulBackupTime | Measure-Object -Maximum | Select-Object -ExpandProperty Maximum);
if($backupSummary.DetailedMessage -eq '' -and $backupSummary.LastBackupResultHR -eq 0) {
$text = 'Last Backup OK';
} elseif($backupSummary.DetailedMessage -eq '') {
$text = 'Unkown Backup Error';
} else {
$text = $backupSummary.DetailedMessage;
}
Write-Host "<prtg>
<result>
<channel>Backup Status</channel>
<value>$($backupSummary.LastBackupResultHR)</value>
<limitMaxError>1</limitMaxError>
<limitMinError>-1</limitMinError>
<limitMode>1</limitMode>
</result>
<result>
<channel>Hours Since Last Backup</channel>
<value>$([math]::Floor($lastBackup.TotalHours))</value>
<unit>TimeHours</unit>
<limitMaxWarning>26</limitMaxWarning>
<limitMaxError>50</limitMaxError>
<limitMode>1</limitMode>
</result>
<result>
<channel>Number of Versions</channel>
<value>$($backupSummary.NumberOfVersions)</value>
<limitMinError>0</limitMinError>
</result>
<Text>$($text)</Text>
</prtg>";