Ready to get started?

Download a free trial of the ServiceNow Data Provider to get started:

 Download Now

Learn more:

ServiceNow Icon ServiceNow ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with ServiceNow data including Schedules, Timelines, Questions, Syslogs, and more!

Automate ServiceNow Integration Tasks from PowerShell



Are you in search of a quick and easy way to access ServiceNow data from PowerShell? This article demonstrates how to utilize the ServiceNow Cmdlets for tasks like connecting to ServiceNow data, automating operations, downloading data, and more.

The CData Cmdlets for ServiceNow are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time access to ServiceNow.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to ServiceNow, but also an SQL interface; this tutorial shows how to use both to retrieve ServiceNow data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for ServiceNow. To access ServiceNow data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for ServiceNow.

Once you have acquired the necessary connection properties, accessing ServiceNow data in PowerShell can be enabled in three steps.

ServiceNow uses the OAuth 2.0 authentication standard. To authenticate using OAuth, you will need to register an OAuth app with ServiceNow to obtain the OAuthClientId and OAuthClientSecret connection properties. In addition to the OAuth values, you will need to specify the Instance, Username, and Password connection properties.

See the "Getting Started" chapter in the help documentation for a guide on connecting to ServiceNow.

PowerShell

  1. Install the module:

    Install-Module ServiceNowCmdlets
  2. Connect:

    $servicenow = Connect-ServiceNow -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -Username "$Username" -Password "$Password" -Instance "$Instance"
  3. Search for and retrieve data:

    $category = "request" $incident = Select-ServiceNow -Connection $servicenow -Table "incident" -Where "category = `'$category`'" $incident

    You can also use the Invoke-ServiceNow cmdlet to execute SQL commands:

    $incident = Invoke-ServiceNow -Connection $servicenow -Query 'SELECT * FROM incident WHERE category = @category' -Params @{'@category'='request'}

ADO.NET

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for ServiceNow\lib\System.Data.CData.ServiceNow.dll")
  2. Connect to ServiceNow:

    $conn= New-Object System.Data.CData.ServiceNow.ServiceNowConnection("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;Username=MyUsername;Password=MyPassword;Instance=MyInstance;InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the ServiceNowDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT sys_id, priority from incident" $da= New-Object System.Data.CData.ServiceNow.ServiceNowDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.sys_id $_.priority }