咨询:能否通过PowerShell脚本关闭Service Now工单?
Absolutely! You can definitely close ServiceNow tickets using a PowerShell script—ServiceNow’s REST API makes this totally doable. Let me break down the process with a working example, including key details you’ll need to get right:
Prerequisites
First, make sure you have these handy:
- Your ServiceNow instance URL (e.g.,
https://yourcompany.service-now.com) - A user account with API access and permissions to close tickets (usually the
itilrole or a custom role with update rights on incident records) - The ticket number you want to close (like
INC0012345)
PowerShell Script Example
This script first fetches the ticket’s unique sys_id (required for updating records via the API), then sends a patch request to set the ticket to closed:
# Configure your ServiceNow details $instanceUrl = "https://yourcompany.service-now.com" $targetTicket = "INC0012345" $apiUser = "your_service_now_username" $apiPass = "your_password" # Pro tip: Use Get-Credential instead of hardcoding! # Encode credentials for basic auth $authString = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$apiUser`:$apiPass")) # Step 1: Get the ticket's sys_id (API uses this instead of ticket number for updates) $getTicketRequest = @{ Uri = "$instanceUrl/api/now/table/incident?sysparm_query=number=$targetTicket&sysparm_fields=sys_id" Headers = @{ "Authorization" = "Basic $authString" "Accept" = "application/json" } Method = "GET" } try { $ticketData = Invoke-RestMethod @getTicketRequest $sysId = $ticketData.result[0].sys_id if (-not $sysId) { Write-Error "Couldn't find a ticket with number $targetTicket" exit 1 } } catch { Write-Error "Failed to retrieve ticket details: $_" exit 1 } # Step 2: Prepare payload to close the ticket $closePayload = @{ state = "7" # Default code for 'Closed' in most instances (verify yours if needed) close_code = "Resolved by Automation" # Use a valid close code from your instance close_notes = "Ticket closed automatically via PowerShell script" } | ConvertTo-Json # Step 3: Send update request to close the ticket $updateTicketRequest = @{ Uri = "$instanceUrl/api/now/table/incident/$sysId" Headers = @{ "Authorization" = "Basic $authString" "Accept" = "application/json" "Content-Type" = "application/json" } Method = "PATCH" Body = $closePayload } try { $updateResult = Invoke-RestMethod @updateTicketRequest Write-Host "Success! Ticket $targetTicket has been closed. Record details: $($updateResult.result | ConvertTo-Json -Compress)" } catch { Write-Error "Failed to close ticket: $_" }
Important Notes
- State Code Check: The
state = "7"is the default for closed incidents, but some custom instances might use different codes. To confirm yours, go to System Definition > Choice Lists in ServiceNow, filter forincident.state, and check the value for "Closed". - Secure Credentials: Never hardcode passwords! Instead, use
Get-Credentialto collect credentials securely at runtime, or store them as a secure string usingConvertTo-SecureStringfor automated runs. - Table Names: If you’re closing a different ticket type (like a change request or problem), replace
incidentin the API URLs with the correct table name (e.g.,change_request). - Permissions: Double-check that your API user has permission to update the ticket’s state and add close notes—without this, the script will throw an authorization error.
内容的提问来源于stack exchange,提问作者Shaik Shahabuddin
相关产品推荐
相关产品推荐

