Sorry to hear about your issues creating a change via Powershell. I’ve written quite a bit of Powershell for Freshservice, so hopefully this helps. Here is some basic working code:
#Tenant name, it adds .freshservice.com... to the url
$tenant = 'mytenant'#.freshservice.com
$apiKey = 'origagroanfg;afng40'
#Force TLS 1.2
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$AuthorizationToken = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $apiKey,$null)))
$Headers = @{"Authorization" = ("Basic {0}" -f $AuthorizationToken)}
$body = @{
description = "<div>Hi Team, <br/><br/> One of our email servers, Exchange Server (ES3) has been acting up. We tried rebooting it, but that didn’t help. We need to get it replaced ASAP. <br/><br/>Regards<br/> Rachel<br/> </div>"
subject = "Getting ES3 back up to speed"
email = "tom@outerspace.com"
priority = 1
status = 1
risk = 1
change_type = 1
impact = 1
planned_start_date = (Get-Date -Date "3/23/2019 4:00:00 PM" -Format "s")
planned_end_date = (Get-Date -Date "3/23/2019 4:30:00 PM" -Format "s")
}
$restParams = @{
Uri = ('https://{0}.freshservice.com/api/v2/changes' -f $tenant)
Method = 'POST'
Body = ($body | ConvertTo-Json)
ContentType = 'application/json'
Headers = $Headers
ErrorAction = 'Stop'
}
try {
$cc = Invoke-RestMethod @restParams
"Successfully create change control {0}" -f $cc.change.id
$cc
}
catch {
$_
}
If you are trying to create a change with departments, agent ids, etc., those values vary per tenant based on what you have created in the tenant. Recommend you create a change manually and then do a GET on that change control to see what the values are (e.g. api/v2/changes/4). The errors typically tell you what is missing or wrong in the payload.
For instance, I passed 4 and the API tells me the value has to be 1,2 or 3:
Invoke-RestMethod: C:\Users\rasim\Dropbox\GitHub\Powershell\Freshworks\Scripts\Create-ChangeControl.ps1:38:11
Line |
38 | $cc = Invoke-RestMethod @restParams
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"description":"Validation failed","errors":[{"field":"impact","message":"It should be one of these values: '1,2,3'","code":"missing_field"}]}
If it’s something like department, that is dynamically generated based on imports or creation on the tenant:
Invoke-RestMethod: C:\Users\rasim\Dropbox\GitHub\Powershell\Freshworks\Scripts\Create-ChangeControl.ps1:39:11
Line |
39 | $cc = Invoke-RestMethod @restParams
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| {"description":"Validation failed","errors":[{"field":"agent_id","message":"Invalid agent_id","code":"invalid_value"},{"field":"department_id","message":"Invalid department_id","code":"invalid_value"},{"field":"maintenance_window_id","message":"Invalid
| maintenance_window_id","code":"invalid_value"},{"field":"group_id","message":"Invalid group_id","code":"invalid_value"}]}
If I wanted to get a department, go into Admin > Departments and click on the department you want to use. Look at the url https://mytenant.freshservice.com/itil/departments/18000196607, so the Group ID is 18000196607. This works almost everywhere, Agents doesn’t show the ID the API is looking for, so you need to do a GET on Agents or find a change with it set and then do a GET on that change control to see what the correct value is.