I am attempting to write a PowerShell that will invoke my Jenkins job remotely. The issue I am running into is that I unable to run my PowerShell script because my "invoke-restmethod" wont call and it throws me an error. I have tried to get the crumb using a curl command in terminal window and that works but I am unable to get the crumb using the restmethod for Jenkins. I tried to call my Jenkins job without the crumb and using the API token but I either get a 401 or 403 error.
I am trying to run this script from Windows while my Jenkins is running on Linux Machine Ubuntu.
I have tried the following script:
$user = 'username'
$pass = 'password'
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.add("Authorization", "$basicAuthValue")
$crumb = (Invoke-RestMethod -Uri 'https://XXX.XXX.XX.XX:8080/job/crumbIssuer/api/json' -Headers $headers).crumb
$headers.add("Jenkins-Crumb", $crumb)
$response = Invoke-RestMethod 'https://XXX.XXX.XX.XX:8080/job/jobName/?token=token' -Method Post -Headers $headers -ContentType 'application/json'
I receive the following error when I run it in powershell:
PS C:\temp\OneDrive - Teradyne\Jenkins> .\test5.ps1
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At C:\temp\OneDrive - Teradyne\Jenkins\test5.ps1:15 char:11
+ $crumb = (Invoke-RestMethod -Uri 'https://XXX.XXX.XX.XX:8080/job/crum ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a send.
At C:\temp\OneDrive - Teradyne\Jenkins\test5.ps1:20 char:13
+ $response = Invoke-RestMethod 'https://XXX.XXX.XX.XX:8080/job/BuildKe ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
This is the curl command that worked for me in terminal window:
curl -s --cookie-jar /tmp/cookies -u username:password http://localhost:8080/crumbIssuer/api/json
Any ideas or help would be appreciated!