For one of my recent projects I would need to update IP Symcon variables from a PowerShell script.
I found the following approach handy and adopted it:
JSON-RPC Web Request from within PowerShell
It’s been a while since I worked on this script but as far as I remember, I struggled with the “payloadJsonRPCVersion”. I had to provide the version in the JSON payload.
JSON_RPC.ps1 contains the two functions you need. In the next step I will dot-source this script file into my script from which I want to make the remote procedure call.
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 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
#region JSON RPC Functions # JSON RPC approach based on https://forum.opsi.org/wiki/doku.php?id=userspace:json-rpc_web-request Function New-JsonPayload() { [CmdletBinding()] Param( [Parameter()] [string]$payloadJsonRPCVersion = '2.0', [Parameter(Mandatory)] [string]$payloadMethod, [Parameter(Mandatory, ValueFromPipeline)] [array]$payloadParams ) Begin { Write-Verbose "Starting to create payload object and convertig to JSON" } Process { try { $jsonPayload = (New-Object -TypeName PSObject | Add-Member -PassThru NoteProperty jsonrpc $payloadJsonRPCVersion | Add-Member -PassThru NoteProperty method $payloadMethod | Add-Member -PassThru NoteProperty params $payloadParams | Add-Member -PassThru NoteProperty id '1') | ConvertTo-Json -Depth 3 # Content } catch { "Error was $_" $line = $_.InvocationInfo.ScriptLineNumber "Error was in Line $line" } } End { Write-Verbose "JSON Payload created" $jsonPayload } } Function New-JsonRpc() { [CmdletBinding()] Param( [Parameter(Mandatory)] [string]$url, [Parameter(Mandatory)] [string]$authUser, [Parameter(Mandatory)] [string]$authPass, [Parameter(Mandatory)] [string]$method, [Parameter(Mandatory)] [array]$params ) Write-Verbose "Setting credentials" $cred = New-Object -TypeName System.Net.NetworkCredential -ArgumentList $authUser, $authPass Write-Verbose "Creating payload" $payload = New-JsonPayload -payloadMethod $method -payloadParams $params $bytes = [System.Text.Encoding]::ascii.GetBytes($payload) $web = [System.Net.HttpWebRequest]::Create($url) [System.Net.ServicePointManager]::ServerCertificateValidationCallback = { $true } $web.Method = 'POST' $web.ContentLength = $bytes.Length $web.ContentType = 'application/json' $web.Credentials = $cred Write-Verbose "Getting request stream and sending payload" $stream = $web.GetRequestStream() $stream.Write($bytes,0,$bytes.Length) $stream.close() Write-Verbose "Getting the streamreader object" $reader = New-Object -TypeName System.IO.Streamreader -ArgumentList $web.GetResponse().GetResponseStream() return $reader.ReadToEnd()| ConvertFrom-Json } #endregion JSON RPC Functions |
In the script from which you want to call the remote procedure, first dot-source the JSON_RPC.ps1 like this:
1 |
. $PSScriptRoot\JSON_RPC.ps1 |
Next you need to specify the url to you IP Symcon WebFront including the ‘/api/’ to address the JSON payload and the credentials. You may also declare the method(s) you want to call as well as the Symcon variable IDs and their new values in an array. In my case I just used the ‘SetValue’ method but for the sake of clarity I added it to the PowerShell variables, too:
1 2 3 4 5 6 7 |
#region IP Symcon config $urlJSON = 'http://192.168.0.20:82/api/' $methodSetValue = 'SetValue' $authUser = 'yourUser' $authPass = 'yourPw****' $paramsLastBackup = @(53411, $datetimeNow) #first array element is the IP Symcon variable ID, second is the value #endregion IP Symcon config |
You can now finally make your JSON RPC like this:
1 2 3 4 5 |
$requestLastBackup = New-JsonRpc -url $urlJSON -authUser $authUser -authPass $authPass -method $methodSetValue -params $paramsLastBackup if ($requestLastBackup.result -eq $true) { Write-Host -Object 'Successfully executed JSON RPC' } |
I think there is still some space for improvement in these script snippets but it serves my desired purpose – for now 🙂
Based on the script variable names you may guess what I need this for. Probably this will be another post 😉
Bye
No Comment
You can post first response comment.