Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Fantasillion/26c68b381f5537f6260b30c9b5bccc01 to your computer and use it in GitHub Desktop.
Save Fantasillion/26c68b381f5537f6260b30c9b5bccc01 to your computer and use it in GitHub Desktop.
PRTG Close All Open Tickets Bulk Script
# Ensuring TLS/SSL connection to our host
# WARNING: TrustAllCertsPolicy trusts all SSL certificates, including invalid ones.
# Use this only if connecting to a server with a self-signed or untrusted certificate.
# For servers with valid SSL certificates from a trusted CA, consider removing this section.
#add-type @"
# using System.Net;
# using System.Security.Cryptography.X509Certificates;
# public class TrustAllCertsPolicy : ICertificatePolicy {
# public bool CheckValidationResult(
# ServicePoint srvPoint, X509Certificate certificate,
# WebRequest request, int certificateProblem) {
# return true;
# }
# }
#"@
#[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
# HTTP return codes for better readability of the script's output
$http_code = @{ 200 = "OK"; 302 = "Found"; 400 = "Bad Request"; 401 = "Unauthorized"}
# Retrieve stored credentials for PRTG from Windows Credential Manager
$credentials = Get-StoredCredential -Target "REPLACE WITH NAME OF CREDENTIALS"
# Define the base URL for the PRTG server
$prtgServerUrl = "https://url_of_prtg_server"
# Check if the credentials exist
if (-not $credentials) {
Write-Error "Failed to retrieve PRTG credentials from Windows Credential Manager. Ensure they are stored correctly."
return
}
$username = $credentials.UserName
$password = $credentials.GetNetworkCredential().Password
# Construct the URL to fetch open tickets
$openTicketsUrl = "$prtgServerUrl/api/table.csv?content=tickets&columns=datetime,parentid,status&filter_status=1&username=$username&password=$password"
# Try to get the list of today's open tickets
try {
$AllOpenTickets = Invoke-WebRequest -Uri $openTicketsUrl | ConvertFrom-Csv
} catch {
Write-Error "Error fetching open tickets - $_.Exception.Message"
return
}
# Check if tickets are returned and count them
if ($AllOpenTickets -is [System.Array]) {
$openTicketCount = $AllOpenTickets.Count
} elseif ($AllOpenTickets) {
$openTicketCount = 1
} else {
$openTicketCount = 0
}
Write-Host "Found $openTicketCount open tickets."
# Initialize a counter for successfully closed tickets
$successfulCloseCount = 0
# Iterate through each open ticket and close it
$AllOpenTickets | ForEach-Object {
$t_id = $_."Ticket ID"
try {
$res = Invoke-WebRequest -Uri "$prtgServerUrl/api/closeticket.htm?id=$($t_id)&content=BulkScriptClosed&username=$username&password=$password"
Write-Host "Closing ticket $($t_id) - $($http_code.($res.StatusCode))"
# Increment the counter if the ticket is closed successfully
if ($res.StatusCode -eq 200) {
$successfulCloseCount++
}
} catch {
Write-Error "Error closing ticket $($t_id) - $_.Exception.Message"
}
}
# Output the number of tickets successfully closed
Write-Host "Successfully closed $successfulCloseCount out of $openTicketCount tickets."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment