Ready to get started?

Learn more about the CData ADO.NET Provider for ServiceNow or download a free trial:

Download Now

Automate ServiceNow Integration Tasks from PowerShell

Are you looking for a quick and easy way to access ServiceNow data from PowerShell? We show how to use the Cmdlets for ServiceNow and the CData ADO.NET Provider for ServiceNow to connect to ServiceNow data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the ServiceNow API, 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.

After obtaining the needed connection properties, accessing ServiceNow data in PowerShell consists of three basic 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 }