使用Invoke-WebRequest通过Powershell将数据发布到ServiceNow

时间:2017-06-26 13:45:52

标签: powershell soap servicenow invoke-command

我试图通过Powershell脚本将信息发布到ServiceNow中的表中。当我运行它时出现错误

  

Invoke-WebRequest:远程服务器返回错误:(500)内部服务器错误。

有人可以帮我弄清楚如何解决这个问题吗?提前谢谢大家。

$userName = 'helpMe'
$password = 'iAmStuck' | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($userName, $password)
$uri = 'stuff'
$postParams = "test"
#[System.Net.ServicePointManager]::ServerCertificateValidationCallback = {$true}
Invoke-WebRequest -Uri $uri -Method Post -Body $postParams -Credential $cred 

2 个答案:

答案 0 :(得分:1)

ServiceNow有一个REST API资源管理器,其中包含各种代码示例以便开始使用。

下面是一个我用一个管理员帐户发布到事件表的示例。这里有两个重要因素,用户必须拥有角色(此处为信息https://docs.servicenow.com/bundle/istanbul-servicenow-platform/page/integrate/inbound-rest/reference/r_RESTAPIRoles.html)才能使用API​​,并且必须能够访问您要发布的表格。另请注意,帖子的正文需要是RAW JSON,并且URL中提供了所有正确的标题数据。如果成功,ServiceNow将返回有关帖子的JSON数据。

Microsoft.TeamFoundation.Controls.ITeamExplorer teamExplorer;
  Microsoft.TeamFoundation.Controls.ITeamExplorerPage page;
 teamExplorer = this.ServiceProvider.GetService(typeof(Microsoft.TeamFoundation.Controls.ITeamExplorer))
               as Microsoft.TeamFoundation.Controls.ITeamExplorer;
 page = teamExplorer.CurrentPage;
 var sections = page.PageContent;
 Type type = typeof(System.Windows.Documents.Hyperlink);
 FieldInfo f1 = page.PageContent.GetType().GetField("linkCancelDecline", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);
 PropertyInfo pinfo = sections.GetType().GetProperty("IsEnabled");
 pinfo.SetValue(sections, false, null);
sections.GetType().GetProperty("IsEnabled").GetValue(sections, null);

答案 1 :(得分:0)

即使您发布了代码,您也没有发布任何与此问题相关的信息。无论ServiceNow是什么可能都有一个API供他们期待的参考。通常在与基于Web的API交互时,API需要一种结构来理解您在$ postParams中提供的数据。有时,在平面HTTP POST的情况下,它可以只是key->值对,但对于RESTful API,您通常需要构建API文档定义的JSON头。

如果您搜索“servicenow powershell交互”,它看起来像是一个GitHub项目,用于通过PowerShell与ServiceNow进行交互,还有一个专门涵盖此主题的PDF。