一尘不染

为什么PowerShell无法发送适当的Crumb?

jenkins

我的PowerShell脚本:

$Params = @{
    uri     = "http://${API_URL}:${API_Port}/crumbIssuer/api/json";
    Method  = 'Get';
    Headers = @{
        Authorization = 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${API_User}):$(${API_Pass})"));
    }
}

$API_Crumb = Invoke-RestMethod @Params

$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Jenkins-Crumb", $API_Crumb.Crumb)

$Params = @{
    uri     = "http://${API_URL}:${API_Port}/job/${API_Job}/build";
    Method  = 'Post';
    Headers = $headers;
}

Invoke-RestMethod @Params

错误:

请求中未包含有效面包屑


阅读 259

收藏
2020-07-25

共1个答案

一尘不染

我修改了代码,如下所示,它可以正常工作。不知道为什么以前的代码会抛出错误。

$API_User = "admin"
$API_Pass = "password"
$API_URL = "localhost"
$API_Port = "8080"
$API_Job = "test01"

$h = @{}
$h.Add('Authorization', 'Basic ' + [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes("$(${API_User}):$(${API_Pass})")))

$Params = @{uri = "http://${API_URL}:${API_Port}/crumbIssuer/api/json";
        Method = 'Get';
        Headers = $h;}
$API_Crumb = Invoke-RestMethod @Params

$h.Add('Jenkins-Crumb', $API_Crumb.crumb)
$Params['uri'] = "http://${API_URL}:${API_Port}/job/${API_Job}/build"
$Params['Method'] = 'Post'
$Params['Headers'] = $h

Invoke-RestMethod @Params
2020-07-25