I’m having difficulty using the Change API in Powershell. I don’t understand why I can’t retrieve all of the values to populate in variables. I appear to be getting all of the raw information, however.
If I make a standard HTTPS GET request to /sdpapi/change/217?TECHNICIAN_KEY=***obscured***&format=json, I get the following output:
{"operation":{"name":"GET","module":"Change","result":{"status":"Success","message":"Change details fetched successfully"},"details":[{"changeid":217,"title":"Upgrade FortiADC from 4.6.2 to 4.7.3","description":"FortiADC has released a new version with several improvements and bugfixes.<div><br /><\/div><div>Release notes can be viewed online: http://docs.fortinet.com/d/fortiadc-d-series-fortiadc-4.7.3-release-notes<\/div><div><br /><\/div>","templatename":"YAM","workflowname":"SDGeneral","stagename":"Planning","statusname":"Planning In Progress","createdtime":"18 Aug 2017, 09:25:41","completedtime":"","requestedBy":"Joe Pobuda","technician":"Joe Pobuda","changemanager":"Joe Pobuda","risk":"Low","category":"Network","sitename":"IT HQ","reasonforchange":"Updates","scheduledstarttime":"17 Sep 2017, 09:23:00","changetype":"Affects Application","accountname":"YIT"}],"resources":[],"SSP_SETTINGS":{}}}
If I run this output through JSONLint, it comes back valid. Cool. However, when I try to pull this in through Powershell I’m “losing” some of the key/value pairs. Here is my code:
$SDLink = "***obscured***"
$api="sdpapi/change?"
$format="json"
$APIKey = "***obscured***"
$inputData = @{
operation=
@{details=
@{}
}
} | ConvertTo-Json
$URI = $SDLink+$api
$postParams = @{TECHNICIAN_KEY=$($APIKey);data=$($inputData);format=$($format)}
$results = Invoke-WebRequest -Uri $URI -Body $postParams -Method GET -TimeoutSec 10 | convertfrom-json
if ($results.operation.result.status -ne "Success") {
write-host -foregroundcolor red "An error occurred while connecting to $SDLink."
break
}
write-host -foregroundcolor yellow Validate Results
write-host -nonewline $results.operation.details.title[217]
write-host -foregroundcolor yellow " <= Title"
write-host -nonewline $results.operation.details.changeid[217]
write-host -foregroundcolor yellow " <= Change ID"
write-host -nonewline $results.operation.details.requestedBy[217]
write-host -foregroundcolor yellow " <= Requested By"
write-host -nonewline $results.operation.details.category[217]
write-host -foregroundcolor yellow " <= Category"
write-host -nonewline $results.operation.details.risk[217]
write-host -foregroundcolor yellow " <= Risk"
write-host -nonewline $results.operation.details.accountname[217]
write-host -foregroundcolor yellow " <= Account Name"
write-host -nonewline $results.operation.details.reasonforchange[217]
write-host -foregroundcolor yellow " <= Reason for change"
write-host -nonewline $results.operation.details.changetype[217]
write-host -foregroundcolor yellow " <= Change type"
write-host -nonewline $results.operation.details.scheduledstarttime[217]
write-host -foregroundcolor yellow " <= Scheduled Start Time"
write-host -nonewline $results.operation.details.description[217]
write-host -foregroundcolor yellow " <= Description"
write-host -nonewline $results.operation.details.templatename[217]
write-host -foregroundcolor yellow " <= Template Name"
write-host -nonewline $results.operation.details.workflowname[217]
write-host -foregroundcolor yellow " <= Workflow Name"
write-host -nonewline $results.operation.details.stagename[217]
write-host -foregroundcolor yellow " <= Stage"
write-host -nonewline $results.operation.details.statusname[217]
write-host -foregroundcolor yellow " <= Status"
write-host -nonewline $results.operation.details.sitename[217]
write-host -foregroundcolor yellow " <= Site Name`n"
When I run this code, I get the following output:
Validate Results
Upgrade Paxton from 5.03 to 5.04 <= Title
216 <= Change ID
Joe Pobuda <= Requested By
Software <= Category
Low <= Risk
YIT <= Account Name
<= Reason for change
<= Change type
<= Scheduled Start Time
A new version of Paxton Net2 has been released.<div><br /></div><div>No information other than the update files were provided by ICU.</div> <= Description
YAM <= Template Name
SDGeneral <= Workflow Name
Close <= Stage
In Progress <= Status
<= Site Name
This does not make any sense to me, as “reasonforchange” “changetype” and “sitename” are in the json output ahead of “accountname” yet “accountname” is returned. Am I missing something silly here? I feel like this should work.