You can get an alert when a domain is about to expire (in 30 days) by using the WhoisXMLAPI.
Note: Also available in our github repository
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 |
# Define the API key at the beginning of the script for easy maintenance $apiKey = "API_FROM_WHOISXMLAPI" # List of domains to check $domains = @("domain1.com", "domain2.com") # Set a flag to track if any domain is about to expire $aboutToExpire = $false foreach ($domain in $domains) { $whoisApiUrl = "https://www.whoisxmlapi.com/whoisserver/WhoisService?apiKey=$apiKey&domainName=$domain&outputFormat=JSON" # Use Invoke-RestMethod to query the WHOIS API $response = Invoke-RestMethod -Uri $whoisApiUrl # Check for API error response if ($response.ErrorMessage) { Write-Output "ERROR: API Error for domain $domain : $($response.ErrorMessage.msg)" $aboutToExpire = $true continue } # Check if the API response contains the expiration date if ($response.WhoisRecord -and $response.WhoisRecord.expiresDate) { $expirationDate = $response.WhoisRecord.expiresDate $expirationDateTime = [datetime]::Parse($expirationDate) $daysUntilExpiration = ($expirationDateTime - (Get-Date)).Days # Check if the domain is less than 30 days from expiration if ($daysUntilExpiration -lt 30) { Write-Output "ERROR: Domain $domain is about to expire in $daysUntilExpiration days." $aboutToExpire = $true } else { Write-Output "OK: Domain $domain expires in $daysUntilExpiration days." } } else { Write-Output "Warning: No expiration information available for domain $domain (Domain is not fully supported by whoisxmlapi)" $noInfo = $true } } # Exit with error 1 if any domain is about to expire if ($aboutToExpire) { exit 1 } if ($noInfo) { exit 998 } |
The script editor window should look similar to this:
System Health Package Example
Package should look like this:
Event Log Alert Package
If everything was correctly configured an alert will be triggered when the domain expires sooner than 30 days. This can also be modified by changing the line if ($daysUntilExpiration -lt 30) {
replacing 30 with the number of days before the expiration.
When running PowerShell scripts, also ensure that the proper execution policy is set.