Saritasa.Prtg.psm1

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
116
117
118
119
120
121
122
123
$prtgUrl  = ''
$userName = ''
$password = ''
$sensors = @{}

<#
.SYNOPSIS
 
.EXAMPLE
Initialize-Prtg prtg.local admin Qwerty123 @{ Server1 = 10123; Server2 = 10124; }
#>

function Initialize-Prtg
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $PrtgUrl,
        [Parameter(Mandatory = $true)]
        [System.Management.Automation.PSCredential]
        [System.Management.Automation.Credential()]
        $Credential,
        [Parameter(Mandatory = $true)]
        [hashtable] $Sensors
    )

    Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

    $script:prtgUrl = $PrtgUrl
    $script:userName = $Credential.UserName
    $script:password = $Credential.GetNetworkCredential().Password
    $script:sensors = $Sensors
}

<#
.SYNOPSIS
Retrieves the sensor information by server key.
 
.PARAMETER Server
The server key.
 
.EXAMPLE
Initialize-Prtg prtg.local admin Qwerty123 @{ Server1 = 10123; Server2 = 10124; }
Get-PrtgSensorId Server1
#>

function Get-PrtgSensorId
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $Server
    )

    Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

    $sensorId = $sensors[$Server]
    if (!$sensorId)
    {
        throw "PRTG sensor for $Server is not registered."
    }

    $sensorId
}

<#
.SYNOPSIS
Starts the PRTG sesor.
 
.PARAMETER Server
The server key associated with a sensor.
 
.EXAMPLE
Initialize-Prtg prtg.local admin Qwerty123 @{ Server1 = 10123; Server2 = 10124; }
Start-PrtgSensor Server1
#>

function Start-PrtgSensor
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $Server
    )

    Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

    Write-Information 'Starting PRTG sensor...'
    $sensorId = Get-PrtgSensorId($Server)
    Update-SslCheckProcedure
    $status = (Invoke-WebRequest -UseBasicParsing "$prtgUrl/api/pause.htm?id=$sensorId&action=1&pausemsg=Resumed by deployment script.&username=$username&password=$password").StatusDescription
    Write-Information "$status`n`n"
}

<#
.SYNOPSIS
Stops the PRTG sensor.
 
.PARAMETER Server
The server key associated with a sensor.
 
.EXAMPLE
Initialize-Prtg prtg.local admin Qwerty123 @{ Server1 = 10123; Server2 = 10124; }
Stop-PrtgSensor Server1
#>

function Stop-PrtgSensor
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true)]
        [string] $Server
    )

    Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState

    Write-Information 'Stopping PRTG sensor...'
    $sensorId = Get-PrtgSensorId($Server)
    Update-SslCheckProcedure
    $status = (Invoke-WebRequest -UseBasicParsing "$prtgUrl/api/pause.htm?id=$sensorId&action=0&pausemsg=Paused by deployment script.&username=$username&password=$password").StatusDescription
    Write-Information "$status`n`n"
}