I have a script code below, its using curl for this operation, i wanna replicate same using Invoke-WebRequest of powershell. how to do it.
$FX_USER=$args[0]
$FX_PWD=$args[1]
$FX_JOBID=$args[2]
$REGION=$args[3]
$FX_HOST=$args[4]
$runId=$(curl -k --header "Content-Type: application/json;charset=UTF-8" -X POST -d '{}' -u ""${FX_USER}":"${FX_PWD}"" ""${FX_HOST}"/api/v1/runs/job/${FX_JOBID}?region=${REGION}" | jq -r '."data"|."id"')
Write-Host "runId = $runId" 2 Answers
$FX_USER=$args[0]
$FX_PWD=$args[1]
$FX_JOBID=$args[2]
$REGION=$args[3]
$TAGS=$args[4]
$SUITES=$args[5]
$CATEGORIES=$args[6]
$FX_HOST=$args[7]
Write-Host "user = ${FX_USER}"
Write-Host "region = ${REGION}"
Write-Host "jobid = ${FX_JOBID}"
Write-Host "hostname = ${FX_HOST}"
$pair = "${FX_USER}:${FX_PWD}"
Write-Host "$pair"
$bytes = [System.Text.Encoding]::ASCII.GetBytes($pair)
$base64 = [System.Convert]::ToBase64String($bytes)
Write-Host "$base64"
$basicAuthValue = "Basic $base64"
Write-Host "$basicAuthValue"
$headers = @{ Authorization = $basicAuthValue }
Write-Host "$headers"
add-type @"
using System.Net;
using System.Security.Cryptography.X509Certificates;
public class TrustAllCertsPolicy : ICertificatePolicy { public bool CheckValidationResult( ServicePoint srvPoint, X509Certificate certificate, WebRequest request, int certificateProblem) { return true; }
}
"@
$AllProtocols = [System.Net.SecurityProtocolType]'Ssl3,Tls,Tls11,Tls12'
[System.Net.ServicePointManager]::SecurityProtocol = $AllProtocols
[System.Net.ServicePointManager]::CertificatePolicy = New-Object TrustAllCertsPolicy
$runId=$((Invoke-WebRequest -Uri "${FX_HOST}/api/v1/runs/job/${FX_JOBID}?region=${REGION}" -Headers $headers -Method POST -ContentType "application/json;charset=UTF-8" ) | ConvertFrom-Json | select -expand data | select -expand id)
Write-Host "runId = $runId" # get function / cmdlet details
(Get-Command -Name Invoke-WebRequest).Parameters
Get-help -Name Invoke-WebRequest -Full
Get-help -Name Invoke-WebRequest -Online
Get-help -Name Invoke-WebRequest -ExamplesHave you even tried what you are doing with Invoke-WebRequest to see if it is successful or not? What errors did you encounter.
PowerShell uses curl as a alias for Invoke-WebRequest. They of course are not the same thing and thus cannot replicated identically.
The web cmdlets have been improved PSCore (Windows / Linux / OSX) than they are in PSv5x and below. To make sure you do not run into site connection issues, make sure to use ...
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 ...in your code. PSCore is designed to run side by side with Windows PowerShell, it does not replace it.
Using the cmdletcUrl vs Invoke-WebRequest
$headers = @{ 'X-JFrog-Art-Api' = $apiKey "Content-Type" = "application/json" "Accept" = "application/json"
}
Invoke-WebRequest -InFile $file -Method Put -Uri "$ARTIFACTORY_HOST/third-party/test/readme.md" -Headers $headers -Verbose
curl -T readme.md "${ARTIFACTORY_HOST}/third-party/test/readme.md " \
-H "X-JFrog-Art-Api: ${apiKey}" \
-H "Content-Type: application/json" \
-H "Accept: application/json"You can use curl.exe directly in PowerShell as well, if you choose to do so, just as you can with any other external exe.