我正在尝试编写一个PowerShell脚本,该脚本将自动执行向Jira实例添加新用户帐户的过程。我提供了我的代码,但老实说我甚至没有达到这一点,因为我收到401错误:
此资源需要WebSudo。
我在Jira支持论坛上看过这两篇文章,但我不清楚如何调整代码以获取然后将其应用到我的REST调用中。我可以改变这个以使用.Net WebClient类,如果这样可以使所有这些更容易,但是现在我有点不知所措。
$url = "https://devjira.domain.com/rest/api/2/user"
$user = "admin"
$pass = "super secure password"
$secpasswd = ConvertTo-SecureString $user -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($pass, $secpasswd);
$userObject = @{
name = "rkaucher@domain.net";
emailAddress = "robert_kaucher@domain.com";
displayName = "Bob Kaucher";
notification = $true;
}
$restParameters = @{
Uri = $url;
ContentType = "application/json";
Method = "POST";
Body = (ConvertTo-Json $userObject).ToString();
Credential = $cred;
}
Invoke-RestMethod @restParameters
JSON输出
{
"name": "rkaucher@domain.net",
"displayName": "Bob Kaucher",
"emailAddress": "robert_kaucher@domain.com",
"notification": true
}
答案 0 :(得分:2)
我将脚本的身份验证组件更改为:
$cred = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$user`:$pass"))
$headers = @{Authorization=("Basic $cred")}
这是基于以下所选答案:
PowerShell's Invoke-RestMethod equivalent of curl -u (Basic Authentication)
该方法的最终调用如下所示:
$restParameters = @{
Uri = $url;
ContentType = "application/json";
Method = "POST";
Body = (ConvertTo-Json $userObject).ToString();
Headers = $headers;
}
$response = Invoke-RestMethod @restParameters