# PRTG-WordPress
#
# Collect version information from an installed WordPress instance and compare it with most recent version
#
# 20210910 frank@carius.de initial version
# 20210921 Version 2 with other way to parse versions.
# 20220415 Adding wpurl to output
[CmdletBinding()]
param (
[string]$wpurl = "https://www.netatwork.de", # zu testende öffentliche Wordpress Webseite
[long]$maxage = 7, # maximales Alter einer neueren Version zum Warn-Status
[ValidateSet("","simple","xml")][string]$prtg ="" # Der Code enthält eine AutoDetectFunktion, wenn der Scriptpfad auf EXEXML endet
)
if ($PSScriptRoot.EndsWith("EXEXML")) {
Write-Verbose "PRTG EXEXML-Mode enabled"
[string]$prtg="xml"
}
elseif ($PSScriptRoot.EndsWith("EXE")) {
Write-Verbose "PRTG Simple-Mode enabled"
[string]$prtg="simple"
}
else {
Set-PSDEBUG -strict # enable strict variable checking. Not usable with PRTG
}
$error.clear()
[string]$publicversion="unknown"
[datetime]$releasedate = get-date "1.1.1900"
[long]$releaseage=1
[string]$installedversion="unknown"
[long]$updateavailable=-1
[string]$finalstatus = "NoStatus"
[int]$finalstatuscode = -1
Write-Verbose "GET installed Version at $($wpurl)"
$htmlanswer = Invoke-WebRequest -Uri $wpurl -Method GET -UseBasicParsing
$generatorvalid = $htmlanswer.Content -match ""
if ($generatorvalid) {
Write-Verbose "GET Get newest public version Age"
$installedversion = $($matches[1])
$historyrest = Invoke-RestMethod https://wordpress.org/news/feed/
$currentversion = $historyrest `
| Where-Object { ($_.category."#cdata-section" -contains "releases") `
-and ($_.category."#cdata-section" -notcontains "development")} `
| Select-Object -first 1
$releasedate = (get-date $currentversion.pubDate)
$releaseage=[int]((get-date) - $releasedate).totaldays
Write-Verbose " Release Date : $($currentversion.pubDate)"
Write-Verbose " Age of Public Version : $($releaseage)"
Write-Verbose "GET Get newest public version Build"
$wpversioncheck=Invoke-RestMethod http://api.wordpress.org/core/version-check/1.7/
$publicversion = $wpversioncheck.offers.current
Write-Verbose " Latest Public Version : $($publicversion)"
Write-Verbose " Installed WP Version : $($installedversion)"
if ($installedversion -eq $publicversion) {
Write-Verbose "OK WP Installation is uptodate"
$updateavailable = 0
$finalstatus = "OK UpToDate"
$finalstatuscode = 0 # OK
}
else {
Write-Verbose " WP Installation outdated"
$updateavailable = 1
if ($releaseage -lt $maxage) {
Write-Warning "WARN WPInstallation should be updated"
$finalstatus = "WARN Update Pending from $($installedversion) to $($publicversion)"
$finalstatuscode = 1 # Warning
}
else {
Write-Warning " ALERT Please update Wordpress Installation"
$finalstatus = "ALERT Update Required from $($installedversion) to $($publicversion)"
$finalstatuscode = 2 # Critical
}
}
}
else {
Write-Verbose "No WPVersion found"
$finalstatus = "ERROR NoWPVersionFound"
$updateavailable = 3
$finalstatuscode = "3" # Protocol/Unknown
}
if ($prtg -eq "xml") {
Write-Verbose "Generating PRTG XML"
$prtgresult="`r`n"
$prtgresult+=" `r`n"
$prtgresult+=" ReleaseAge`r`n"
$prtgresult+=" Days`r`n"
$prtgresult+=" 0`r`n"
$prtgresult+=" absolut`r`n"
$prtgresult+=" $($releaseage)`r`n"
$prtgresult+=" `r`n"
$prtgresult+=" `r`n"
$prtgresult+=" UpdateAvailable`r`n"
$prtgresult+=" `r`n"
$prtgresult+=" 0`r`n"
$prtgresult+=" absolut`r`n"
$prtgresult+=" $($updateavailable)`r`n"
$prtgresult+=" $($updateavailable)`r`n"
$prtgresult+=" `r`n"
$prtgresult+=" $($Finalstatus) for URL $($wpurl)`r`n"
$prtgresult+=" $(if ($finalstatuscode -eq 0) {"0"} else {"1"})`r`n"
$prtgresult+=""
$prtgresult
}
elseif ($prtg -eq "simple") {
Write-Verbose "Generating PRTG Simple"
"$($updateavailable):$($Finalstatus)"
}
else {
[pscustomobject][ordered]@{
wpurl = $wpurl
LatestPublicVersion = $publicversion
LatestReleaseDate = $releasedate
Releaseage = $releaseage
InstalledWPVersion = $installedversion
updateavailable = $updateavailable
Finalstatus = $finalstatus
finalstatuscode = $finalstatuscode
}
}
Write-Verbose "END WP-Versioncheck Exitcode $($finalstatuscode)"
exit $finalstatuscode