# required for basic auth $Url = "https://northdakota.service-now.com" $HeaderAuth = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $SNowVMCred.UserName, $SnowVMCred.GetNetworkCredential().Password))) $SNOWSessionHeader = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" $SNOWSessionHeader.Add('Authorization', ('Basic {0}' -f $HeaderAuth)) $SNOWSessionHeader.Add('Accept', 'application/json') $Type = "application/json" # open incident in webgui, sysid is in the url # example: https://northdakotatest.service-now.com/nav_to.do?uri=%2Fincident.do%3Fsys_id%3Db6e3f5d91b9a29d0128ec806604bcb9a%26sysparm_record_target%3Dtask%26sysparm_record_row%3D7%26sysparm_record_rows%3D10%26sysparm_record_list%3Dactive%3Dtrue%5Eassigned_to%3Djavascript:gs.user_id()%5EORDERBYDESCnumber # sys_id=guid Invoke-RestMethod -Method GET -Uri ($Url + "/api/now/table/incident/b6e3f5d91b9a29d0128ec806604bcb9a") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x # 10000 results #Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/incident") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov y #### better formatting $InvokeRestMethodParams = @{ Method = 'Get'; Uri = ($Url + "/api/now/table/sc_req_item"); ContentType = $Type; Headers = $SNOWSessionHeader; Body = @{ sysparm_query = 'number=RITM0282695'; sysparm_limit = 1; sysparm_display_value = 'true'; #### show human readable values, not just sysids sysparm_fields = 'request,cat_item'; } } (Invoke-RestMethod @InvokeRestMethodParams -ov c).result # known tables below # incident INC######## Invoke-RestMethod -Method GET -Uri ($Url + "/api/now/table/incident?sysparm_query=number=INC0437213&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x Invoke-RestMethod -Method GET -Uri ($Url + "/api/now/table/incident?sysparm_query=number=INC0437213&sysparm_limit=1&sysparm_display_value=true") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov y # change_request CHG######## Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/change_request?sysparm_query=number=CHG0089327&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type #sc_request REQ######## Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sc_request?sysparm_query=number=REQ0129027&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type #sc_req_item RITM######## Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sc_req_item?sysparm_query=number=RITM0134884&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type #sc_task SCTASK####### #sys_user Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sys_user?sysparm_query=email=zmeier@nd.gov&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x #sys_user_group Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sys_user_group?name=NDIT-Computer Systems&sysparm_limit=1") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x # cmdb_ci_ip_network Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/cmdb_ci_ip_network") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x # appname / cmdb_ci_service_auto_list Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/cmdb_ci_service_auto") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x https://northdakota.service-now.com/cmdb_ci_service_auto_list.do?sysparm_target=ni.VEacf3763e475c7514baa37834846d433c&sysparm_target_value=&sysparm_reference_value=&sysparm_nameofstack=reflist&sysparm_clear_stack=true&sysparm_element=not&sysparm_reference=cmdb_ci_service_auto&sysparm_view=sys_ref_list&sysparm_form_view=default&sysparm_additional_qual=&sysparm_parent_id=a3feaecb47d0b914baa37834846d4381&sysparm_domain_restore=false # all tables $a = Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sys_db_object") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/sc_ic_category_request?&sysparm_limit=100") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov c # searching incidents ?sysparm_query=number=INC0437213 sysparm_query%3Dshort_descriptionLIKEhardware Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/incident&sysparm_limit=3?sysparm_query=short_descriptionLIKEhardware") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x # cmdb_ci_vmware_instance Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/cmdb_ci_vmware_instance") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x # create records # incident [PSCustomObject]$NewRecord = @{ short_description = "failed machine"; caller_id = "Zachary L Meier"; # and others, get record to view } $InvokeRestMethodParams = @{ Method = 'Post'; Uri = ($Url + "api/now/table/incident"); Body = $NewRecord | ConvertTo-Json; Headers = $SnowSessionHeader; ContentType = "application/json" } Invoke-RestMethod @InvokeRestMethodParams # update records # incident, sys_id required # incident state 1 = new, 2 = in progress, 3 = on hold [PSCustomObject]$SetRecord = @{ short_description = "lots of failed hardware"; description = "omg send help" caller_id = "Zachary L Meier"; state = 3 # on hold # and others, get record to view remaining options } $InvokeRestMethodParams = @{ Method = 'Put'; Uri = ($Url + "api/now/table/incident/fec7a7a91ba761504e2afeee034bcba8"); Body = $SetRecord | ConvertTo-Json; Headers = $SnowSessionHeader; ContentType = "application/json" } Invoke-RestMethod @InvokeRestMethodParams # custom fields lookup $RitmNum = 'RITM0157839' $RitmNum = 'RITM0260967' $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $RitmNum $params = @{ Method = 'Get'; Uri = ('https://northdakota.service-now.com/api/now/table/sc_item_option_mtom?request_item=' + $Ritm.Sys_id.value + '&sysparm_limit=1000'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $CustomFieldsLookup = (Invoke-RestMethod @params).result # get each custom field individually $sc_item_options = ForEach ($CustomField in $CustomFieldsLookup) { #$scitemsysid = $sc_item_option.sc_item_option.value $params = @{ Method = 'Get'; Uri = ("https://northdakota.service-now.com/api/now/table/sc_item_option?sys_id=" + $CustomField.sc_item_option.value ); #+ "&sysparm_limit=100" Headers = $SNOWSessionHeader; ContentType = "application/json" } (Invoke-RestMethod @params).result } $MyArrayList = [System.Collections.ArrayList]@() ForEach ($sc_item_option in $sc_item_options) { $params = @{ Method = 'Get'; Uri = ("https://northdakota.service-now.com/api/now/table/item_option_new?sys_id=" + $sc_item_option.item_option_new.value); Headers = $SNOWSessionHeader; ContentType = "application/json" } $vars = (Invoke-RestMethod @params).result $obj = [PSCustomObject]@{ #sc_item_option_sysid = $sc_item_option.sys_id #item_option_new_sysid = $vars.sys_id name = $vars.name question_text = $vars.question_text value = $sc_item_option.value type = $vars.type # YES_NO = 1; MULTI_LINE_TEXT = 2; MULTIPLE_CHOICE = 3; NUMERIC_SCALE = 4; SELECT_BOX = 5; SINGLE_LINE_TEXT = 6; CHECKBOX = 7; REFERENCE = 8; DATE = 9; DATE_TIME = 10; LABEL = 11; BREAK = 12; MACRO = 14; UI_PAGE = 15; WIDE_SINGLE_LINE_TEXT = 16; MACRO_WITH_LABEL = 17; LOOKUP_SELECT_BOX = 18; CONTAINER_START = 19; CONTAINER_END = 20; LIST_COLLECTOR = 21; LOOKUP_MULTIPLE_CHOICE = 22; HTML = 23; SPLIT = 24; MASKED = 25; } $null = $MyArrayList.Add($obj) } #$Ritm | Add-Member -MemberType NoteProperty -Name CustomFields -Value $MyArrayList ####################################### $Ritm | Add-Member @{ 'CustomVariable' = [pscustomobject]@{} } $InvokeRestMethodParams = @{ Method = 'Get'; Uri = ($Url + "/api/now/table/sc_item_option_mtom"); Body = @{ request_item = $Ritm.sys_id; sysparm_fields = (@('sc_item_option.item_option_new.name', 'sc_item_option.value', 'sc_item_option.item_option_new.type', 'sc_item_option.item_option_new.question_text', 'sc_item_option.item_option_new.reference') -join ','); sysparm_query = "request_item=dd02ed854744bd18baa37834846d43fb^sc_item_option.item_option_new.typeIN1,2,3,4,5,6,7,8,9,10,16,18,21,22,26" }; ContentType = 'application/json'; Headers = $SNOWSessionHeader; } $customVars = (Invoke-RestMethod @InvokeRestMethodParams).result #-ov c ForEach ($var in $customVars) { $newVar = [pscustomobject] @{ Value = $var.'sc_item_option.value' DisplayName = $var.'sc_item_option.item_option_new.question_text' Type = $var.'sc_item_option.item_option_new.type' } # show the underlying value if the option is a reference type <#if ($newVar.Type -eq 'Reference' ) { $newVar.Value = (Get-ServiceNowRecord -Table $var.'sc_item_option.item_option_new.reference' -ID $var.'sc_item_option.value' -Property name -AsValue -ServiceNowSession $ServiceNowSession) }#> $RITM.CustomVariable | Add-Member @{ $var.'sc_item_option.item_option_new.name' = $newVar } } request_item=dd02ed854744bd18baa37834846d43fb^sc_item_option.item_option_new.typeIN1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 18, 21, 22, 26 ################################################################# 41e3290a1b1aed14128ec806604bcb50 $params = @{ Method = 'Get'; Uri = ("https://northdakotatest.service-now.com/api/now/table/sc_item_option?sys_id=" + '41e3290a1b1aed14128ec806604bcb50' ); #+ "&sysparm_limit=100" Headers = $SNOWSessionHeader; ContentType = "application/json" } (Invoke-RestMethod @params).result #get standard change template $AllChangeTemplateStandard = Invoke-RestMethod -Method Get -Uri ($Url + "api/sn_chg_rest/change/standard/template?sysparm_query=active=true") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type $AllChangeTemplateStandard = Invoke-RestMethod -Method Get -Uri ($Url + "api/sn_chg_rest/change/standard/template?sysparm_query=active=true^name=$Name") -Headers $SnowSessionHeader -ContentType $Type $SearchResult = $AllChangeTemplateStandard.result | Where-Object { $_.Name.display_value -eq $StdChangeTemplateName } # update record $Number = 'CHG0098990' Invoke-RestMethod -Method GET -Uri ($Url + "api/now/table/change_request/41b66f1e1b5af5104e2afeee034bcb97") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x $Values = @{ short_description = 'this is a new description' } $Body = $Values | ConvertTo-Json Invoke-RestMethod -Method Patch -Uri ($Url + "api/now/table/change_request/41b66f1e1b5af5104e2afeee034bcb97") -Credential $SNowVMCred -Headers $SnowSessionHeader -ContentType $Type -ov x -Body $body # date/time current time [DateTime]::UtcNow.ToString('yyyy-MM-dd HH:mm:ss'); (Get-Date -AsUTC).ToString('yyyy-MM-dd HH:mm:ss') # submit service catalog request $SysId = 'a490f76adbc0e8901b47105f68961941' #generic service request test $CommentsFieldExample = @" Create nd.gov Active Directory service account for use by ITD System Administrators: { "PasswordstateTitle": "itdzmtest99.nd.gov", "PasswordstateList": "CSRC", "SamAccountName": "svczmtest99", "Description": "description for things" } "@ # submit catalog request, the powershell way $BodyJson | ConvertFrom-Json $BodyObj = [PSCustomObject]@{ sysparm_quantity = "1"; variables = [PSCustomObject]@{ v_user_phone = "(701) 328-4171"; v_requested_by = "206d9e091b8898102653997fbd4bcbd4"; v_approval_department_code = "112.0"; requester_information = "true"; v_container_requested_for = "true"; v_manager = "1c1d16451b8898102653997fbd4bcb3b"; v_requested_for = "206d9e091b8898102653997fbd4bcbd4"; v_type = "Server Systems/Hosting"; additional_comments = "comments"; v_user_in_servicenow = "Yes"; v_approval_division = "f40758231b321450bba0113fad4bcb2d"; v_container_requested_by = "true"; approval_information = "true"; v_approval_division_code = "32"; request_commnets = "true"; v_user_email = "zmeier@nd.gov"; request_information = "true"; v_alt_contact = ""; v_approval_department = "f3c65cef1bfed050bba0113fad4bcb1d"; v_approval_charge_code = ""; } } $NewJson = $BodyObj | ConvertTo-Json $InvokeRestMethodParams = @{ Method = 'Post'; Uri = "https://northdakotatest.service-now.com/api/sn_sc/servicecatalog/items/a490f76adbc0e8901b47105f68961941/order_now"; Credential = $SNowVMCred; Headers = $SNOWSessionHeader; ContentType = $Type; Body = $NewJson #'{"sysparm_quantity":"1","variables":{"v_user_phone":"(701) 328-4171","v_requested_by":"206d9e091b8898102653997fbd4bcbd4","v_approval_department_code":"112.0","requester_information":"true","v_container_requested_for":"true","v_manager":"1c1d16451b8898102653997fbd4bcb3b","v_requested_for":"206d9e091b8898102653997fbd4bcbd4","v_type":"Server Systems/Hosting","additional_comments":"moar comments!!2!","v_user_in_servicenow":"Yes","v_approval_division":"f40758231b321450bba0113fad4bcb2d","v_container_requested_by":"true","approval_information":"true","v_approval_division_code":"32","request_commnets":"true","v_user_email":"zmeier@nd.gov","request_information":"true","v_alt_contact":"","v_approval_department":"f3c65cef1bfed050bba0113fad4bcb1d","v_approval_charge_code":""},"sysparm_item_guid":"05adfdcf1b96bd104e2afeee034bcb1d","get_portal_messages":"true","sysparm_no_validation":"true","engagement_channel":"sp","referrer":null}' } Invoke-RestMethod @InvokeRestMethodParams -OutVariable e # new vm form and custom variables $RitmNum = 'RITM0233555' $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $RitmNum $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/sc_item_option_mtom?request_item=' + $Ritm.Sys_id + '&sysparm_limit=1000'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $CustomFieldsLookup = (Invoke-RestMethod @params).result # get each custom field individually $sc_item_options = ForEach ($CustomField in $CustomFieldsLookup) { #$scitemsysid = $sc_item_option.sc_item_option.value $params = @{ Method = 'Get'; Uri = ("https://northdakotadev.service-now.com/api/now/table/sc_item_option?sys_id=" + $CustomField.sc_item_option.value ); #+ "&sysparm_limit=100" Headers = $SNOWSessionHeader; ContentType = "application/json" } (Invoke-RestMethod @params).result } $MyArrayList = [System.Collections.ArrayList]@() ForEach ($sc_item_option in $sc_item_options) { $params = @{ Method = 'Get'; Uri = ("https://northdakotadev.service-now.com/api/now/table/item_option_new?sys_id=" + $sc_item_option.item_option_new.value); Headers = $SNOWSessionHeader; ContentType = "application/json" } $vars = (Invoke-RestMethod @params).result $obj = [PSCustomObject]@{ #sc_item_option_sysid = $sc_item_option.sys_id #item_option_new_sysid = $vars.sys_id name = $vars.name question_text = $vars.question_text value = $sc_item_option.value type = $vars.type # YES_NO = 1; MULTI_LINE_TEXT = 2; MULTIPLE_CHOICE = 3; NUMERIC_SCALE = 4; SELECT_BOX = 5; SINGLE_LINE_TEXT = 6; CHECKBOX = 7; REFERENCE = 8; DATE = 9; DATE_TIME = 10; LABEL = 11; BREAK = 12; MACRO = 14; UI_PAGE = 15; WIDE_SINGLE_LINE_TEXT = 16; MACRO_WITH_LABEL = 17; LOOKUP_SELECT_BOX = 18; CONTAINER_START = 19; CONTAINER_END = 20; LIST_COLLECTOR = 21; LOOKUP_MULTIPLE_CHOICE = 22; HTML = 23; SPLIT = 24; MASKED = 25; } $null = $MyArrayList.Add($obj) } $params = @{ Method = 'Get'; Uri = ($Url + '/api/now/table/sc_item_option?sys_id=624e6fef47244610b7853238436d43a9'); Headers = $SNOWSessionHeader; ContentType = 'application/json' } $x = Invoke-RestMethod @params $params = @{ Method = 'Get'; Uri = ($Url + '/api/now/table/item_option_new_set?sys_id=5939ba3b47b04a90b7853238436d4372'); Headers = $SNOWSessionHeader; ContentType = 'application/json' } $x = Invoke-RestMethod @params #variable set lookup scratch $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/sc_req_item?sys_id=' + '3b94e3f1874e0a14a5aacbb6dabb35ac' + '&sysparm_limit=1000'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $x = (Invoke-RestMethod @params) $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/item_option_new_set?sys_id=' + '5939ba3b47b04a90b7853238436d4372' + '&sysparm_limit=1000'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $a = (Invoke-RestMethod @params).result $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/sc_cat_item'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $b = (Invoke-RestMethod @params).result $c = $b | Where-Object sys_id -EQ 'c64e27af47244610b7853238436d435d' $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/item_option_new_set_list'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $d = (Invoke-RestMethod @params).result $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/sc_multi_row_question_answer'); Headers = $SNOWSessionHeader; ContentType = "application/json" } $e = (Invoke-RestMethod @params).result <# sc_req_item --> sc_multi_row_question_answer.parent_id #> $RitmNum = 'RITM0233555' $RitmNum = 'RITM0231641' $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $RitmNum $params = @{ Method = 'Get'; Uri = ('https://northdakotadev.service-now.com/api/now/table/sc_multi_row_question_answer?parent_id=' + $Ritm.sys_id); Headers = $SNOWSessionHeader; ContentType = "application/json" } $AllRows = (Invoke-RestMethod @params).result #$AllRows | Group-Object row_index ####### USE THIS TO SEPARATE EACH ROW ENTRY #item_option gets value #vars gets name, type, question_text $Row_Indexes = ($AllRows | Select-Object -Unique row_index).row_index $ArrayList = [System.Collections.ArrayList]@() ForEach ($Row in $Row_Indexes) { $RowProperties = $AllRows | Where-Object row_index -EQ $Row $obj = [PSCustomObject]@{ } ForEach ($Property in $RowProperties) { $params = @{ Method = 'Get'; Uri = ("https://northdakotadev.service-now.com/api/now/table/item_option_new?sys_id=" + $Property.item_option_new.value); Headers = $SNOWSessionHeader; ContentType = "application/json" } $vars = (Invoke-RestMethod @params).result $obj | Add-Member -MemberType NoteProperty -Name $vars.name -Value $Property.value } $null = $ArrayList.Add($obj) } $params = @{ Method = 'Get'; Uri = (); Headers = $SNOWSessionHeader; ContentType = "application/json" } $CmdbCi = Get-ITDServiceNowRecord -Table cmdb_ci -SysId '4ac36cd11b0154509d7ada01dd4bcbdb' $params = @{ Method = 'Get'; Uri = "https://northdakota.service-now.com/api/now/table/sys_history_set?sys_id=" + 'ebb4a8cddbbf0254c4582fcb139619b9'; Headers = $SNOWSessionHeader; ContentType = 'application/json' } Invoke-RestMethod @params -ov z #### cmdb key values POC Get-ITDServiceNowRecord -Table 'cmdb_ci_win_server' -Filter @{name = 'itddotrtp2' } $z = Get-ITDServiceNowRecord -Table 'cmdb_key_value' -Filter @{name = 'itddotrtp2' } $ComputerName = 'itddotrtp2' $CmdbCi = Get-ITDServiceNowRecord -Table 'cmdb_ci_server' -Filter ("name=$ComputerName") $KeyValues = Get-ITDServiceNowRecord -Table 'cmdb_key_value' -Filter ('configuration_item=' + $CmdbCi.sys_id.value) $KeyValues | Select-Object {$_.configuration_item.display_value},{$_.key.display_value},{$_.value.display_value} # https://northdakotadev.service-now.com/now/nav/ui/classic/params/target/cmdb_key_value.do%3Fsys_id%3D31f4620bc38496505bc7d685e00131ac%26sysparm_record_target%3Dcmdb_key_value%26sysparm_record_row%3D2%26sysparm_record_rows%3D5%26sysparm_record_list%3Dconfiguration_item%253D67ba954bc30c121046637badc001311c # https://northdakotadev.service-now.com/now/nav/ui/classic/params/target/cmdb_key_value.do%3Fsys_id%3D31f4620bc38496505bc7d685e00131ab%26sysparm_record_target%3Dcmdb_key_value%26sysparm_record_row%3D1%26sysparm_record_rows%3D5%26sysparm_record_list%3Dconfiguration_item%253D67ba954bc30c121046637badc001311c