You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过REST API发布ServiceNow知识库文章(Tokyo版本)

ServiceNow Tokyo版本通过REST API发布知识库文章的方案

方案一:直接创建已发布状态的知识库文章

直接创建已发布状态的文章需要满足两个前提:

  • 操作账号拥有kb_knowledge_publish权限
  • 目标知识库基础(kb_knowledge_base)未配置强制审批流程

修改你的PowerShell代码,添加必要的状态字段即可:

$serviceNowCredential = Get-Credential
$uri = "https://<instance>.service-now.com/api/now/table/kb_knowledge"
$method = "POST"

# Build auth header
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $ServiceNowCredential.UserName, $serviceNowCredential.GetNetworkCredential().Password)))

# Set proper headers
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add('Authorization', ('Basic {0}' -f $base64AuthInfo))
$headers.Add('Accept', 'application/json')
$headers.Add('content-type', 'application/json')

# 生成符合ISO 8601格式的当前时间
$currentISO8601Date = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffZ"

$tempObj = [PsCustomObject]@{
    text              = 'Some stuff goes here' 
    author            = 'e9326af747d9611091271c42846d43bd'
    active            = 'true'
    kb_knowledge_base = 'cf33206447e5a11091271c42846d43ac'
    topic             = 'General'
    short_description = 'How to do a Thing'
    article_type      = 'text'
    kb_category       = 'a196b8a44729a11091271c42846d438c'
    # 添加发布状态相关字段
    workflow_state    = 'published'
    publish_date      = $currentISO8601Date
    approved          = 'true' # 若知识库要求审批则需设置
}

$response = Invoke-WebRequest -Headers $headers -Method $method -Uri $uri -Body ($tempObj | ConvertTo-Json) -ErrorAction Stop

注意:sys_created_by字段无需手动设置,ServiceNow API会自动填充当前认证用户。

方案二:创建草稿后触发即时发布工作流

如果知识库配置了强制审批流程,直接设置状态无法生效,需要先创建草稿,再触发发布工作流:

步骤1:创建草稿文章(使用你原有的代码)

执行原代码后,从响应中提取文章的sys_id:

$responseObj = $response.Content | ConvertFrom-Json
$articleSysId = $responseObj.result.sys_id

步骤2:触发发布工作流/更新状态

有两种方式完成发布:

方式A:通过更新记录触发业务规则发布

$publishUri = "https://<instance>.service-now.com/api/now/table/kb_knowledge/$articleSysId"
$publishMethod = "PUT"
$currentISO8601Date = Get-Date -Format "yyyy-MM-ddTHH:mm:ss.fffZ"

$publishBody = [PsCustomObject]@{
    workflow_state = 'published'
    publish_date = $currentISO8601Date
    approved = 'true'
} | ConvertTo-Json

$publishResponse = Invoke-WebRequest -Headers $headers -Method $publishMethod -Uri $publishUri -Body $publishBody -ErrorAction Stop

方式B:直接触发「Publish Knowledge」工作流

如果方式A无效,可直接调用工作流触发API:

$workflowUri = "https://<instance>.service-now.com/api/now/workflow/trigger"
$workflowBody = [PsCustomObject]@{
    table_name = "kb_knowledge"
    table_sys_id = $articleSysId
    workflow_name = "Publish Knowledge" # 也可替换为工作流的sys_id
} | ConvertTo-Json

$workflowResponse = Invoke-WebRequest -Headers $headers -Method "POST" -Uri $workflowUri -Body $workflowBody -ErrorAction Stop

关键注意事项

  • 确保操作账号拥有对应知识库的发布权限,以及web_service_access_only角色(若API访问需要)
  • 若知识库配置了多级审批,需先通过API完成审批步骤后再触发发布
  • 检查目标知识库的「发布设置」,确认是否允许即时发布或需要遵循审批流程

内容的提问来源于stack exchange,提问作者StackExchangeGuy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 10:57:40