In previous post, I talked about Service Now Incident management using Powershell. In this blog, I am going to talk about Change management using Powershell with an example. Again, the scenario remains the same, still trying to automate a lot of stuff and this time leveraging Service Now Change management. In one of my previous blog, I wrote about Share audit automation.The next step of that process was to create change request to work on identified issues like shares created with wrong permission or set as public etc.
As mentioned earlier, Service Now provides a lot of nice documentation on it’s API management.
Let’s start with defining the credentials with permissions to access Service Now REST API and API URL using Powershell.
$SNowUser = "service now rest api user id"
$SNowPass = "service now rest api user password"
$Chguri = "https://<COMPANY SERVICENOW INSTANCE NAME>.service-now.com/api/now/table/change_request"
$header = @{"Authorization" = "Basic "+[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($SNowUser+":"+$SNowPass))}
Every change request ticket will have a good description with a start time and end time. Usually , this is defined based on the Service Level Agreement defined for certain work type. Rest of the fields you would like to add to a change request are based on defined attributes required for your organization. Service Now administrator in your organization should be able to provide more details on those.
Now, we will create the JSON body of the change request based on what I just mentioned above.
$sTime = (get-date).toString("yyyy-MM-dd HH:mm:ss")
$eTime = (get-date).AddDays(5).toString("yyyy-MM-dd HH:mm:ss")
$sdesc = "Remidiate issues with wrong permission setup for File Shares"
$desc = "Remidiate issues with wrong permission setup for File Shares.This is part of Monthly Audit on Shares. Details of all File shares with issues can be found at location : \\<server>\<folder>\<file>"
$desc += "===================================================================================================="
$body = @{
assignment_group = "SERVICE NOW GROUP WHO WILL HANDLE THIS CHANGE REQUEST"
requested_by = "NAME OR ID WHO REQUESTED IT"
category = "TYPE OF CHANGE - EXAMPLE, DEPLOYMENT"
description = $desc
implementation_plan = $desc
impact = 3
priority = 4
risk = 4
reason = "Maintenance"
short_description = $sdesc
start_date = $sTime
end_date = $eTime
state = "Propose"
type = "Normal"
work_notes = $desc
}
$bodyJson = $body | ConvertTo-Json
Creating a Service Now change request is a post operation on the REST api exposed by Service Now. We will define that and also make the REST api call to create the change request as shows below.
$method = "post"
$post = Invoke-WebRequest -Uri $Chguri -Headers $header -Method $method -Body $bodyJson -ContentType "application/json"
$ChgOut = $post | ConvertFrom-JSON
$ChgNumber = $ChgOut.result.Number
If the operation is successful, you will receive a 200 return code and based on the above script snippet, it will return the change request number. That’s it on creating a new change request.
Now, we will update the change request using Powershell with some notes and close it as all remediation have been made and everything looks good. To do that, we will again make REST api call with a post method. However, this time, it will be to set the status of the change request to “Closed”. As we have already defined all those variables and values, no need to redefine. We will just use those. However, make sure to provide the change request number. Also, I am assuming it took just one hour to fix the issues, I will generate the time by using Powershell. You may hard code it if you want.
$sTime = (get-date).AddHours(-1).toString("yyyy-MM-dd HH:mm:ss")
$eTime = (get-date).toString("yyyy-MM-dd HH:mm:ss")
$body = @{
close_notes = "Closing this change request after successful completion"
number = $ChgNumber
state = "Closed"
u_closure_code = "Successful"
work_end = $eTime
work_start = $sTime
work_notes = "Change has been executed successfully as planned.Marking the Change as Closed"
}
$bodyJson = $body | ConvertTo-Json
$post = Invoke-WebRequest -Uri $Chguri -Headers $header -Method $post -Body $bodyJson -ContentType "application/json"
$ChgOut = $post | ConvertFrom-JSON
If the operation is successful, you will receive a 200 return code.
That’s it. We are done with automating Change Management process using Powershell. Let me know if you face any issue by posting questions or comments in the blog.