$x = Invoke-RestMethod -Method "Get" -uri $url -Headers $get_headers
**Start-Process "chrome.exe" $x.url**
Start-Sleep -s 10
Hello,
I am using the above to download a file via chrome. Is it possible to wait until the file download is complete to continue the script instead of sleeping or implementing an incremental retry function.
Thank you
So lets talk about what i see and ways to make it better
$x = Invoke-RestMethod -Method "Get" -uri $url -Headers $get_headers
Start-Process "chrome.exe" $x.url
Start-Sleep -s 10
Well you can save using the Invoke-RestMethod
Here is a example
Invoke-RestMethod "http://www.peoplelikeus.org/piccies/codpaste/codpaste-teachingpack.pdf" -OutFile "C:\codpaste-teachingpack.pdf"
In your case this might work.
$x = Invoke-RestMethod $url -Headers $get_headers
Invoke-RestMethod $x.url -OutFile "C:\SomeTypeOfFile.Txt"
The Invoke-RestMethod will wait until it is complete to move on in the code.
FYI (putting this here as it is too long for a normal comment), here are 3 other ways, that you could have done this:
$url = "http://mirror.internode.on.net/pub/test/10meg.test"
$output = "$PSScriptRoot\10meg.test"
$start_time = Get-Date
# 1. Invoke-WebRequest
Invoke-WebRequest -Uri $url -OutFile $output
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 2. System.Net.WebClient
(New-Object System.Net.WebClient).DownloadFile($url, $output)
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# 3. Start-BitsTransfer
Start-BitsTransfer -Source $url -Destination $output -Asynchronous
"Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
Related
I am trying to store the output of the Invoke-RestMethod into a variable and than exact the output from the variable.
Invoke-RestMethod -Method POST -Uri $exportURL -Headers $logonHeader -ContentType "application/zip" -TimeoutSec 2700 -OutFile "$PlatformZipPath\$PlatformID.zip" -ErrorAction SilentlyContinue
$URI = New-Object System.Uri("$URL_DeployPlatfoms/$Repository/$Valut/$EMV/$platfromTech/$PlatformID.zip")
$AF_PWD = ConvertTo-SecureString $JfrogAuthToken -AsPlainText -Force
$AFCREDS = New-Object System.Management.Automation.PSCredential ($Jfroguser, $AF_PWD)
$SOURCE = "$PlatformZipPath\$PlatformID.zip"
Invoke-WebRequest -Uri $URI -InFile $SOURCE -Method Put -Credential $AFCREDS
So, What this piece of code of does is export the cyberark platform and deploy to jfrog artifactory. But the invoke rest method store platform zip locally. I dont want. I want to directly deploy it to artifactory.
But i dont know how to do that. Please help me with that. Thanks in advance.
what can i put in which can help me re run a failed release in azure devops?
$timer = New-TimeSpan -Minutes 120
while ($sw.elapsed -lt $timer) {
$ReleaseStatus = Invoke-RestMethod -Uri "https://vsrm.dev.azure.com/company/project/_apis/release/releases/$RId/environments/$EId/?`api-version=6.0" -Method GET -Headers $Header -Verbose
start-sleep -seconds 10
if ($ReleaseStatus.Status -eq 'Succeeded') {
write-host "Release Succeeded"
return
}
if ($ReleaseStatus.status -eq 'Rejected' -or 'active') {
Invoke-RestMethod "https://vsrm.dev.azure.com/company/project/_apis/release/releases?api-version=6.0" -Method POST -Headers $Header -Body $Body -Verbose
write-host "Error occured, Re-running Release"
return
}
}
i had to ping this endpoint in the end.
Invoke-RestMethod -Uri "vsrm.dev.azure.com/company/project/_apis/release/releases/$RId/…?`api-version=6.0" -Method GET -Headers $Header -Verbose
This allowed me to rerun the release instead of creating and running a new one in my script.
I have a Powershell script and it has a Invoke-RestMethod code for sending a file over API to a URL.
Code is shown below
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls;
Invoke-RestMethod -Method POST -ContentType "multipart/form-data" -Uri $Uri -InFile $FilePath >> $Log
Now while appending the return response to the "Log" file, I get the following format
success data
------- ----
True #{created=0; updated=0; skipped=0; unchanged=19; uploaded=19; errors=0; statusUrl=https://sandbox.myurl.com/Services/Conta...
The URL is not complete. This return message is generated as incomplete.
I require it in the below format
Could anyone help me get this thing going on?
Thanks
The following code did the trick
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12 -bor [System.Net.SecurityProtocolType]::Tls11 -bor [System.Net.SecurityProtocolType]::Tls;
$response = Invoke-RestMethod -Method POST -ContentType "multipart/form-data" -Uri $Uri -InFile $FilePath
$response
$response.success >> $Log
$response.data >> $Log
$response.numRows >> $Log
$response[0] >> $Log
As I was logging the response, I had to use the >>$Log to append the log file
$uri="http:\\www.SomeUrl.com"
Measure-Command { $request = Invoke-WebRequest -Uri $uri -UseBasicParsing}
In above powershell script, how can I exit from Invoke-WebRequest if it takes time more than 10 secs, and return a error code if possible.
You can use the Timeout parameter to the Invoke-WebRequest command,
$uri="http://www.SomeUrl.com"
Measure-Command { $request = Invoke-WebRequest -Uri $uri -UseBasicParsing -Timeout 10}
You can cover it with try / catch block to get the error message.
try {
$uri="http://www.SomeUrl.com"
Measure-Command { $request = Invoke-WebRequest -Uri $uri -UseBasicParsing -Timeout 10 -ErrorAction Stop}
}
catch {
Write-Output "Timeout occured. Exception: $_"
}
You can also use -Headers #{"Cache-Control"="no-cache"} with Invoke-WebRequest which will not cache the pages you are visiting.
I am new to powershell. I have a powershell script which basically makes an REST API request and gets back a JSON response. But I have an issue with the Invoke-RestMethod cmdlet. I mean the script sometimes doesn't wait for the response and continues with the next line of code.
Is there some trick in powershell which asks the code to wait for the API response and then run the remaining code. Below is my code
if ($EventID -eq '4726') {
$ad_user = "$TargetUsername#avayaaws.int"
$jsonbody = #{
"setupName" = "avayadev-or"
"instanceName" = "000_JumpServer_DMZ"
"command" = "$Command"
"parameters" = #{
"mobile" = "$getMobileAttr"
"ad_user"="$ad_user"
"label" ="$environment"
}
"eventToken" = "optional"
} | ConvertTo-Json
#$response = Start-Job {
# Invoke-RestMethod -Uri $uri -Method $verb -Body $jsonbody -Headers $header
#} | wait-job | receive-job
#$response = Invoke-RestMethod -Uri $uri -Method $verb -Body $jsonbody -Headers $header
$response = Invoke-WebRequest -Uri $uri -Method $verb -Body $jsonbody -Headers $header
$EventTrigger = $translEvent4726
Write-Output "Response is: $response"
PushLogs -transaction $EventTrigger -adUser $ad_user -transResult $response
}
I have tried many ways like using Out-Null and Wait-Job, Recieve-Job but couldn't get it to work. Any help is much appreciated.