OP5 plugin example in powershell

If you want to monitor your windows server uptime in OP5. Run from NSClient++ on all hosts you want to monitor uptime on.

param
(
	[parameter(Mandatory = $true)]
	[int]$w,
	[parameter(Mandatory = $true)]
	[int]$c
)
function Get-Uptime
{
	$os = Get-WmiObject win32_operatingsystem
	$uptime = (Get-Date) - ($os.ConvertToDateTime($os.lastbootuptime))
	return $Uptime
}
$os = Get-CimInstance Win32_OperatingSystem | Select-Object  Caption | ForEach{ $_.Caption }
$uptime = Get-Uptime

if ( $uptime.Days -gt $c)
{
	Write-Host Write-Host "CRITICAL Uptime:" $Uptime.Days "days"
	exit 2

}
if ($uptime.Days -gt $w)
{
	Write-Host Write-Host "WARNING Uptime:" $Uptime.Days "days"
	exit 1

}
if ($uptime.Days -lt $w)
{
	Write-Host "OK" $Uptime.Days "days uptime"
	Write-Host "OS: $os"
	exit 0
}

Create a new host via OP5 API with powershell

If you need to create a new host with OP5 api you can use something like this.
Template, hostgroups and contactgroups are not mandatory and can be removed from hash table.

 

function new-op5host
(
	[parameter(Mandatory = $true)]
	[string]$hostname,
	[parameter(Mandatory = $true)]
	[string]$alias
)
{
	$username = 'api_user$Default'
	$password = "password"
	
	$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username, $password)))
	
	$hash = @{
		file_id = "etc/hosts.cfg";
		host_name = "$hostname";
		address = "$hostname.domain.com";
		alias = "$alias";
		max_check_attempts = "3";
		notification_interval = "5";
		notification_options = "d", "r";
		notification_period = "24x7";
		template = "Win_hosts";
		hostgroups = "Windows servers";
		contact_groups = "Windowsservrar"
	}
	$jsonData = $hash | convertto-json
	
	$result = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri "https://op5.domain.com/api/config/host" -Body $jsonData -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
	
	#Save
	$result = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri "https://op5.domain.com/api/config/change" -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }
		
}