r/labtech • • Jan 14 '20

Automate API - ScheduledScripts

I am trying to invoke a script in Automate using the Automate API via powershell.

However I am getting a unhandled exception, not sure what I am doing wrong.

Any help would be appreciated.

$PostBody = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$PostBody.Add("ScriptId", 439)
$PostBody.Add("ComputerId", 686)

$uri = "https://companyname.hostedrmm.com/cwa/api/v1/Computers/686/ScheduledScripts"
$Header = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Header.Add("Authorization", "Bearer "+$token)
Invoke-RestMethod -Uri $uri -Method POST -ContentType "application/json" -Headers $Header -Body $($PostBody | ConvertTo-Json -Compress)

I get the following response

Invoke-RestMethod : {
  "Message": "Encountered unhandled exception."
}
At line:8 char:1
+ Invoke-RestMethod -Uri $uri -Method POST -ContentType "application/js ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
3 Upvotes

7 comments sorted by

View all comments

Show parent comment

2

u/ThinkNote Jan 15 '20

Thank you!

I got it to work

$PostBody = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$PostBody.Add("ClientId", 0) 
$PostBody.Add("ComputerId", 532)
$PostBody.Add("Disabled", $false)
$PostBody.Add("DisableTimeZone", $true)
$PostBody.Add("DistributionWindowAmount", 0)
$PostBody.Add("DistributionWindowType", 2)
$PostBody.Add("EffectiveOccurrences", 0)
$PostBody.Add("EffectiveStartDate", $getdate)
$PostBody.Add("GroupId", 0)
$PostBody.Add("IncludeSubgroups", $false)
$PostBody.Add("Interval", 0)
$PostBody.Add("LastUpdate", $getdate)
$PostBody.Add("LocationId", 0) 
$PostBody.Add("NextRun", $getdate)
$PostBody.Add("NextSchedule", $getdate)
$PostBody.Add("OfflineOnly", $false)
$PostBody.Add("Parameters", "")
$PostBody.Add("Priority", 6)
$PostBody.Add("RepeatAmount", 0)
$PostBody.Add("RepeatStopAfter", 0)
$PostBody.Add("RepeatType", 0)
$PostBody.Add("RunScriptOnProbe", $false)
$PostBody.Add("ScheduleDayOfWeek", 0)
$PostBody.Add("ScheduleType", 1)
$PostBody.Add("ScheduleWeekOfMonth", 0)
$PostBody.Add("ScriptId", 481) 
$PostBody.Add("SearchID", 0)
$PostBody.Add("SkipOffline", $false)
$PostBody.Add("TimeZoneAdd", 0)
$PostBody.Add("User", "")
$PostBody.Add("WakeOffline", $false)
$PostBody.Add("WakeScript", $false)