Create json-rpc web-request from within a powershell script to query the json-rpc api

$urlJSON = 'https://xxx.xxx.xxx.xxx:4447/rpc'
 
$authUser = "[username]"
$authPass = "[password]"
 
$method = "[method]"
$params = @("[param1]","[param2]")
 
Function CallJSON($url,$object,$authUser,$authPass,$method,$params) {
    $cred = New-Object System.Net.NetworkCredential -ArgumentList $authUser,$authPass
 
    $bytes = [System.Text.Encoding]::ascii.GetBytes($object)
    $web = [System.Net.WebRequest]::Create($url)
 
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
 
    $web.Method = "POST"
    $web.ContentLength = $bytes.Length
    $web.ContentType = "application/json"
    $web.Credentials = $cred
    $stream = $web.GetRequestStream()
    $stream.Write($bytes,0,$bytes.Length)
    $stream.close()
    $reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
    return $reader.ReadToEnd()| ConvertFrom-Json
    $reader.Close()
}
 
$data = (New-Object PSObject |
    Add-Member -PassThru NoteProperty method $method |
    Add-Member -PassThru NoteProperty params $params |
    Add-Member -PassThru NoteProperty id '1') | ConvertTo-Json -depth 3
 
$request = CallJSON $urlJSON $data $authUser $authPass $method $params

example: automatically assign a meta-package of default software to every new client registered to OPSI

$urlJSON = 'https://your-opsi-server-url:4447/rpc'
 
$authUser = "adminuser"
$authPass = "TopSecretPassword"
 
$method = "setProductActionRequestWithDependencies"
$params = @("hwaudit","servername.domain.tld","setup")
 
Function CallJSON($url,$object,$authUser,$authPass,$method,$params) {
    $cred = New-Object System.Net.NetworkCredential -ArgumentList $authUser,$authPass
 
    $bytes = [System.Text.Encoding]::ascii.GetBytes($object)
    $web = [System.Net.WebRequest]::Create($url)
 
    [System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
 
    $web.Method = "POST"
    $web.ContentLength = $bytes.Length
    $web.ContentType = "application/json"
    $web.Credentials = $cred
    $stream = $web.GetRequestStream()
    $stream.Write($bytes,0,$bytes.Length)
    $stream.close()
    $reader = New-Object System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream()
    return $reader.ReadToEnd()| ConvertFrom-Json
    $reader.Close()
}
 
$data = (New-Object PSObject |
    Add-Member -PassThru NoteProperty method $method |
    Add-Member -PassThru NoteProperty params $params |
    Add-Member -PassThru NoteProperty id '1') | ConvertTo-Json -depth 3
 
$request = CallJSON $urlJSON $data $authUser $authPass $method $params