Powershell script to export the computers from Cmdb_ci_computer table
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2020 02:49 AM
Dear Team,
I'm new to this community and trying to automate one task using powershell. Tried checking in the community for my requirement but couldn't find any.
My requirement is
- From Cmdb_ci_computer table get the computer names starting with the name PC & assigned status as "Installed"
- Add the machines from step 1 to a specific AD group (I have created a script to add machines to AD group using .csv file)
I would like to know if there is any powershell script to get the computer details for step1?
It will be really great if you can share any sample scripts, if you have any.
Thank you in advance.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2020 06:55 AM
Hi Ram,
Are you wanting to do this via Orchestration / IntegrationHub or simply call out to ServiceNow via your powershell script?
If the latter, you can utilise the REST API Explorer which will help you with your requirement.
Build the query (nameSTARTSWITHPC &^install_status=1) and select the name field
You can then use the SEND button to see if you're getting the right data, and use the Powershell button to get a script example
# Eg. User name="admin", Password="admin" for this code sample.
$user = "admin"
$pass = "admin"
# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $pass)))
# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization',('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept','application/json')
# Specify endpoint uri
$uri = "https://instancename.service-now.com/api/now/table/cmdb_ci_computer?sysparm_query=nameSTARTSWITHPC%20%26%5Einstall_status%3D1&sysparm_fields=name"
# Specify HTTP method
$method = "get"
# Send HTTP request
$response = Invoke-RestMethod -Headers $headers -Method $method -Uri $uri
# Print response
$response.RawContent
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2020 07:11 AM
Hi Kieran,
Thank you for your response.
I Just want to call ServiceNow via PowerShell script to get the computer details. Using UI, I'm able to get this information and I'm looking for PowerShell script to do the same.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
09-28-2020 07:17 AM
Hi,
You just need to add the following onto the end of the script to take the response.result and parse it to a column in a CSV file.
$response.result | Export-Csv C:\temp\computers.csv -NoTypeInformation