2017-09-09 42 views
0

我的PowerShell腳本:爲什麼PowerShell無法發送正確的Crumb?

$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 

錯誤:

No valid crumb was included in the request

回答

0

我修改了代碼如下所示,它的工作。不知道爲什麼以前的代碼拋出錯誤。

$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 
+0

你試過'UTF8.GetBytes'而不是'ASCII.GetBytes'嗎?否則像「pásswörd」這樣的密碼可能會導致異常。 –

相關問題