详细分析 ▾
运行时依赖
版本
New tools: sn_batch (bulk operations with dry-run safety) and sn_health (instance health check — nodes, jobs, semaphores, stats). 10 tools total.
安装命令 点击复制
技能文档
Query and manage records on any ServiceNow instance via the REST Table API.
Setup
Set environment variables for your ServiceNow instance:
export SN_INSTANCE="https://yourinstance.service-now.com"
export SN_USER="your_username"
export SN_PASSWORD="your_password"
All tools below use scripts/sn.sh which reads these env vars.
Tools
sn_query — 查询 任何 表
bash scripts/sn.sh query [options]
Options:
--查询 "" — ServiceNow encoded 查询 (e.g. 活跃=真^priority=1)
--fields "" — Comma-separated fields 到 return
--limit — Max records (默认 20)
--offset — 分页 offset
--orderby "<字段>" — 排序 字段 (prefix 带有 - 对于 descending)
--display <真|假|所有> — Display values mode
Examples:
# List open P1 incidents
bash scripts/sn.sh query incident --query "active=true^priority=1" --fields "number,short_description,state,assigned_to" --limit 10# All users in IT department
bash scripts/sn.sh query sys_user --query "department=IT" --fields "user_name,email,name"
# Recent change requests
bash scripts/sn.sh query change_request --query "sys_created_on>=2024-01-01" --orderby "-sys_created_on" --limit 5
sn_get — 获取 single 记录 由 sys_id
bash scripts/sn.sh get [options]
Options:
--fields "" — Fields 到 return
--display <真|假|所有> — Display values mode
Example:
bash scripts/sn.sh get incident abc123def456 --fields "number,short_description,state,assigned_to" --display true
sn_create — 创建 记录
bash scripts/sn.sh create ''
Example:
bash scripts/sn.sh create incident '{"short_description":"Server down","urgency":"1","impact":"1","assignment_group":"Service Desk"}'
sn_update — 更新 记录
bash scripts/sn.sh update ''
Example:
bash scripts/sn.sh update incident abc123def456 '{"state":"6","close_code":"Solved (Permanently)","close_notes":"Restarted service"}'
sn_delete — 删除 记录
bash scripts/sn.sh delete --confirm
The --confirm flag is required to prevent accidental deletions.
sn_aggregate — Aggregate queries
bash scripts/sn.sh aggregate --type [options]
Types: COUNT, AVG, MIN, MAX, SUM
Options:
--类型 <类型> — Aggregation 类型 (必填)
--查询 "" — 过滤 records
--字段 "<字段>" — 字段 到 aggregate 在...上 (必填 对于 AVG/MIN/MAX/求和)
--分组-由 "<字段>" — 分组 results 由 字段
--display <真|假|所有> — Display values mode
Examples:
# Count open incidents by priority
bash scripts/sn.sh aggregate incident --type COUNT --query "active=true" --group-by "priority"# Average reassignment count
bash scripts/sn.sh aggregate incident --type AVG --field "reassignment_count" --query "active=true"
sn_schema — 获取 表 schema
bash scripts/sn.sh schema [--fields-only]
Returns field names, types, max lengths, mandatory flags, reference targets, and choice values.
Use --fields-only for a compact field list.
sn_batch — Bulk 更新 或 删除 records
bash scripts/sn.sh batch --query "" --action [--fields '{"field":"value"}'] [--limit 200] [--confirm]
Performs bulk update or delete operations on all records matching a query. Runs in dry-run mode by default — shows how many records match without making changes. Pass --confirm to execute.
Options:
--查询 "" — 过滤 records 到 operate 在...上 (必填)
--action <更新|删除> — Operation 到 perform (必填)
--fields '' — JSON fields 到 设置 在...上 每个 记录 (必填 对于 更新)
--limit — Max records 到 affect per run (默认 200, safety cap 在 10000)
--dry-run — Show match 计数 仅, 否 changes (默认 behavior)
--confirm — Actually execute operation (disables dry-run)
Examples:
# Dry run: see how many resolved incidents older than 90 days would be affected
bash scripts/sn.sh batch incident --query "state=6^sys_updated_on# Bulk close resolved incidents (actually execute)
bash scripts/sn.sh batch incident --query "state=6^sys_updated_on# Dry run: count orphaned test records
bash scripts/sn.sh batch u_test_table --query "u_status=abandoned" --action delete
# Delete orphaned records (actually execute)
bash scripts/sn.sh batch u_test_table --query "u_status=abandoned" --action delete --limit 50 --confirm
Output (JSON summary):
{"action":"update","table":"incident","matched":47,"processed":47,"failed":0}
sn_health — Instance health check
bash scripts/sn.sh health [--check ]
Checks ServiceNow instance health across multiple dimensions. Default is --check all which runs every check.
Checks:
- version — Instance build version, 日期, 和 tag 从 sys_properties
- nodes — Cluster 节点 status (在线/离线) 从 sys_cluster_state
- jobs — Stuck/overdue scheduled jobs 从 sys_trigger (state=就绪, next_action > 30 min past)
- semaphores — 活跃 semaphores (potential locks) 从 sys_semaphore
- stats — Quick dashboard: 活跃 incidents, 打开 P1s, 活跃 changes, 打开 problems
Examples:
# Full health check
bash scripts/sn.sh health# Just check version
bash scripts/sn.sh health --check version
# Check for stuck jobs
bash scripts/sn.sh health --check jobs
# Quick incident/change/problem dashboard
bash scripts/sn.sh health --check stats
Output (JSON):
{
"instance": "https://yourinstance.service-now.com",
"timestamp": "2026-02-16T13:30:00Z",
"version": {"build": "...", "build_date": "...", "build_tag": "..."},
"nodes": [{"node_id": "...", "status": "online", "system_id": "..."}],
"jobs": {"stuck": 0, "overdue": []},
"semaphores": {"active": 2, "list": []},
"stats": {"incidents_active": 54, "p1_open": 3, "changes_active": 12, "problems_open": 8}
}
sn_attach — Manage attachments
# List attachments on a record
bash scripts/sn.sh attach list # Download an attachment
bash scripts/sn.sh attach download
# Upload an attachment
bash scripts/sn.sh attach upload
[content_type]
Common Tables
Table Description incidentIncidents change_requestChange Requests problemProblems sc_req_itemRequested Items (RITMs) sc_requestRequests sys_userUsers sys_user_groupGroups cmdb_ciConfiguration Items cmdb_ci_serverServers kb_knowledgeKnowledge Articles taskTasks (parent of incident/change/problem) sys_choiceChoice list values
Encoded 查询 Syntax
ServiceNow encoded queries use ^ as AND, ^OR as OR:
活跃=真^priority=1 — 活跃 和 P1
活跃=真^ORactive=假 — 活跃 或 非活跃
short_descriptionLIKEserver — Contains "server"
sys_created_on>=2024-01-01 — Created 之后 日期
assigned_toISEMPTY — Unassigned
stateIN1,2,3 — State 1, 2, 或 3
caller_id.name=John Smith — Dot-walk 通过 references
Notes
- 所有 API calls 使用 Basic Auth 通过
SN_USER / SN_PASSWORD
- 默认 结果 limit 20 records; 使用
--limit 到 adjust
- 使用
--display 真 到 获取 human-readable values 代替 的 sys_ids 对于 reference fields
- script auto-detects whether
SN_INSTANCE includes protocol prefix