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.

	[parameter(Mandatory = $true)]
	[parameter(Mandatory = $true)]
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)]
	[parameter(Mandatory = $true)]
	$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) }
	$result = Invoke-RestMethod -Method POST -ContentType "application/json" -Uri "https://op5.domain.com/api/config/change" -Headers @{ Authorization = ("Basic {0}" -f $base64AuthInfo) }

Change wallpaper on windows 7

I needed a script to change desktop wallpaper for all users using the default one with an outdated company logo on. I came up with this startup script as a solution. Set $filesize to the size of the old wallpaper to only change wallpapers with same filesize. Use as logon script.

$filesize = 100593
$bg = Get-Item ("$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\TranscodedWallpaper.jpg)
if ($bg.length -eq $filesize)
Copy-item "\\fileserver\UsrLogon\wallpaper\img0.jpg" "$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\img0.jpg" -Force
Set-ItemProperty -path 'HKCU:\Control Panel\Desktop\' -name wallpaper -value "$env:USERPROFILE\appdata\Roaming\Microsoft\Windows\Themes\img0.jpg"
rundll32.exe user32.dll, UpdatePerUserSystemParameters