ServiceNow PowerShell Module

repository·master·Indexed 19 days ago

https://github.com/snow-shell/servicenow-powershell

A PowerShell module for interacting with the ServiceNow REST API. It provides cmdlets for record management (Get, New, Update, Remove), incident and change request creation, attachment handling, Service Catalog operations, and GraphQL queries via Invoke-ServiceNowGraphQL.

Tokens
2.6K
Snippets
7
Records
9
Agent score
14%

What's inside servicenow-powershell

  1. Create a new ServiceNow session

    master

    Use New-ServiceNowSession to establish a connection. This creates a script-scoped variable $ServiceNowSession which is used by default by other module functions.

    Note: ServiceNow's API does not support SSO.

    Retry Logic: Requests receiving retryable HTTP errors (429, 502, 503, 504, 408, 409) are automatically retried. You can control this behavior using RetryCount, RetryWaitSeconds, and MaxRetryAfterSeconds parameters.

    # Basic authentication
    $params = @{
        Url = 'instance.service-now.com'
        Credential = $userCred
    }
    New-ServiceNowSession @params
    
    # OAuth with user and client credentials (automatic token refresh)
    $params = @{
        Url = 'instance.service-now.com'
        Credential = $userCred
        ClientCredential = $clientCred
    }
    New-ServiceNowSession @params
    
    # OAuth client_credentials grant (machine-to-machine)
    $params = @{
        Url = 'instance.service-now.com'
        ClientCredential = $clientCred
    }
    New-ServiceNowSession @params
  2. Use the ServiceNow module via Docker

    master

    A Docker image is available with the module preinstalled. When using the Docker image, you do not need to manually create a session. Provide authentication via the following environment variables:

    • SNOW_SERVER: The ServiceNow instance (e.g., instance.service-now.com).
    • SNOW_TOKEN: A pre-generated OAuth token. Alternatively, use SNOW_USER and SNOW_PASS.
    • SNOW_USER: Username for the ServiceNow instance.
    • SNOW_PASS: Password for the ServiceNow user.
  3. Query data via GraphQL

    master

    To use GraphQL, you must first create a session using the -GraphQL switch. This interacts with a Scripted GraphQL API designed by your ServiceNow administrators. Use Invoke-ServiceNowGraphQL to execute queries or mutations against a specific Application and Schema.

    # Create a GraphQL session
    New-ServiceNowSession -Url instance.service-now.com -Credential $userCred -GraphQL
    
    # Execute a GraphQL query
    Invoke-ServiceNowGraphQL -Application myapp -Schema incident -Query 'findById (id: "INC0010001") {sys_id {value} description {value}}'
  4. Manage ServiceNow Attachments

    master

    Use the following cmdlets to handle file attachments:

    • Add-ServiceNowAttachment: Attach a file to an existing record.
    • Get-ServiceNowAttachment: Retrieve attachment details.
    • Export-ServiceNowAttachment: Download/export an attachment.
    • Remove-ServiceNowAttachment: Remove an attachment using its sys_id.
  5. Use the ServiceNow Service Catalog

    master

    Manage user carts and order items from the Service Catalog:

    • Get-ServiceNowCart: View the current user's cart.
    • New-ServiceNowCartItem: Add an item to the cart. CatalogItem supports tab/menu completion.
    • Remove-ServiceNowCartItem: Remove specific items (via CartItemId) or all items (via -All).
    • Submit-ServiceNowCart: Check out the current cart.
    # Add an item to the cart
    New-ServiceNowCartItem -CatalogItem 'Standard Laptop' -Quantity 1
    
    # Review and submit the cart
    Get-ServiceNowCart
    Submit-ServiceNowCart -PassThru
    
    # Remove items
    Remove-ServiceNowCartItem -CartItemId 'a1b2c3d4e5f6a7b8c9d0e1f3a4b5c6d7'
    Remove-ServiceNowCartItem -All
  6. Create ServiceNow Incidents and Change Requests

    master

    Specialized cmdlets are available for common ServiceNow objects:

    • New-ServiceNowIncident: Create a new incident.
    • New-ServiceNowChangeRequest: Create a new change request.
    • New-ServiceNowChangeTask: Create a new change task.
    • New-ServiceNowConfigurationItem: Create a new configuration item.
    # Create an incident with custom table entries
    $params = @{
        Caller = "UserName"
        ShortDescription = "New PS Incident"
        Description = "This incident was created from Powershell"
        InputData = @{
            u_service = "MyService"
            u_incident_type = "Request"
            urgency = 1
        }
    }
    New-ServiceNowIncident @params
    
    # Create a change task
    New-ServiceNowChangeTask -ChangeRequest CHG0010001 -ShortDescription 'New PS change task' -Description 'This change task was created from Powershell' -AssignmentGroup ServiceDesk
  7. Manage ServiceNow records

    master

    The module provides cmdlets to interact with any ServiceNow table:

    • Get-ServiceNowRecord: Retrieve records.
    • New-ServiceNowRecord: Create a new record.
    • Update-ServiceNowRecord: Update existing record values.
    • Remove-ServiceNowRecord: Delete a record.
    • New-ServiceNowQuery: Build query strings.
    • Export-ServiceNowRecord: Export records to a file.

    Tip: Use tab completion on the -Table parameter to find tables, and once a table is selected, use tab completion on the -Property parameter to discover available fields, their types, and example values.

    # Discover fields via tab completion
    Get-ServiceNowRecord -Table incident -Property <press tab>
    
    # Get incidents opened in the last 30 days
    Get-ServiceNowRecord -Table incident -Filter @('opened_at', '-ge', (Get-Date).AddDays(-30))
    
    # Search for an incident by description
    Get-ServiceNowRecord -Table incident -Description 'powershell'
    
    # Update a record
    Get-ServiceNowRecord inc0010002 | Update-ServiceNowRecord -InputData @{comments='Updated via PowerShell'}
  8. Reference: ServiceNow Module Functions

    master

    A summary of the available cmdlets categorized by functional area.

    ### Session & Authentication
    - `New-ServiceNowSession`: Create a new session using basic auth, OAuth (user or client-credentials grant), or an existing access token
    
    ### Records
    - `Get-ServiceNowRecord`: Retrieve records from any table
    - `New-ServiceNowRecord`: Create a new record in any table
    - `Update-ServiceNowRecord`: Update record values
    - `Remove-ServiceNowRecord`: Remove a record
    - `New-ServiceNowQuery`: Build a query string for an API call
    - `Export-ServiceNowRecord`: Export table records to a file
    
    ### Incidents & Changes
    - `New-ServiceNowIncident`: Create a new incident
    - `New-ServiceNowChangeRequest`: Create a new change request
    - `New-ServiceNowChangeTask`: Create a new change task
    - `New-ServiceNowConfigurationItem`: Create a new configuration item
    
    ### Attachments
    - `Add-ServiceNowAttachment`: Attach a file to an existing record
    - `Get-ServiceNowAttachment`: Retrieve attachment details
    - `Export-ServiceNowAttachment`: Export (download) an attachment
    - `Remove-ServiceNowAttachment`: Remove an attachment by sys_id
    
    ### Service Catalog
    - `Get-ServiceNowCart`: Get the current user's cart
    - `New-ServiceNowCartItem`: Add an item to the current user's cart
    - `Remove-ServiceNowCartItem`: Remove one or all items from a cart
    - `Submit-ServiceNowCart`: Check out the current user's cart
    
    ### GraphQL
    - `Invoke-ServiceNowGraphQL`: Query or mutate data via a Scripted GraphQL API