Invoke-RestMethod : Content-Length or Chunked Encoding cannot be set for an operation that does not write data - powershell-4.0

I am looking for cURL equivalent command in powershell and then found the below mentioned URL :
https://superuser.com/questions/344927/powershell-equivalent-of-curl
Based on the above URL I tried the below mentioned powershell script in a system which has Powershell version 4.0
Invoke-RestMethod -Uri www.discoposse.com/index.php/feed -Method Get -OutFile C:\Temp\DiscoPosseFeed.xml
Once I run the above command I see a xml file in the specified location but if now I specify the encoding as mentioned below:
Invoke-RestMethod -Uri www.discoposse.com/index.php/feed -TransferEncoding compress -Method Get -OutFile C:\Temp\DiscoPosseFeed.xml
I am getting an error :
Can anyone help me to know is there anything I am missing here?

The explanation is tha your command line does not write anything on the line. TransferEncoding specifies a value for the transfer-encoding HTTP response header. Valid values are Chunked, Compress, Deflate, GZip and Identity.

Related

ffmpeg: How to set a custom HTTP header when using PUT as the output method?

I'm trying to use ffmpeg to PUT encoded files to object storage, and I need to include an API key in a header. I've tried -http_opts '"headers='AccessKey: mykey'" and -headers 'AccessKey: mykey' but neither end up with the header in the request when I use -v trace to see what is getting sent.
Here's the relevant part of my command:
-method PUT -headers 'AccessKey: mykey' \
https://storage/store/stream.mpd
Is this a known issue or have I just got the order of the options wrong?
Your header is missing a trailing CRLF. Try -headers 'AccessKey: mykey'$'\r\n' with your ffmpeg version.
Newer ffmpeg versions have auto adding of trailing CRLF in headers and your code
ffmpeg -v trace -headers 'AccessKey: mykey' -method PUT -i http://localhost/
is working with my ffmpeg version 4.3.4-0+deb11u1+mx21+1 built with gcc 10 (Debian 10.2.1-6)

Easily catch error from maven install task in build pipeline in Azure Devops?

In Azure Devops when your Maven build task fails it will display an an error like below in the build summary:
Maven build error
Now if you click in and scroll up you will be able to find the error next to the reactor summary. This is easy enough to do at home with a mouse and a big screen but for some of our devs on trains or in cafes it can be quite an effort for them to find the error. I want to be able to make the error easier to find. Some suggestions I've thought of but not sure how to implement are below. Do you have any other good ideas?
Reduce output of maven logs - I've tried the -q flag but it doesn't seem to work for this task?
Use rest api somehow to find the error and then send it to a slack channel with a webhook - Not sure how to find the log or error with the rest api though
So I can help with how you might grab the error out of the build logs and send it over to slack.
Assuming we are going to run this task as the next task after the build error.
We can get the build logs via the API. Make sure to add the system_access token so you can auth.
Once we have all the logs grab the url out of the response to download the last log
Download the last log
Parse it and find the [[error]] lines
Marshal the message to slack \ or do whatever else you want with it
YAML
- pwsh: |
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user, $env:System_AccessToken)))
$uriLogs = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$env:BUILD_BUILDID/logs?api-version=5.1"
$response = Invoke-RestMethod -Uri $uriLogs -Method 'GET' -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$logUrl =$response.value[-1].url
$webContent = Invoke-WebRequest -Uri $logUrl -Method 'GET' -Headers #{Authorization = ("Basic {0}" -f $base64AuthInfo)}
$ErrorMessage = $webContent.Content.tostring() -split "[`r`n]" | select-string "[[Error]]"
Write-Output "Error lines found " $ErrorMessage
$postSlackMessage = #{token=$env:SLACK_TOKEN;channel="#user,#channel-name";text=$ErrorMessage}
Invoke-RestMethod -Uri https://slack.com/api/chat.postMessage -Body $postSlackMessage
env:
system_accesstoken: $(System.AccessToken)
condition: failed()
Other alternatives:
parse the log, find the error, and then send a link to the failure directly at that line number. There is a well known format for direct links to each line.
send a direct link to download just the error part of the log. See startLine and endLine parameters here.
I added a line to top of powershell script to solve the error
Invoke-RestMethod : The underlying connection was closed: An unexpected error occurred on a receive.
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
If you don't have any problem about tls you don't need to add this line.
Powershell:
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$uriLogs = "$($env:SYSTEM_TEAMFOUNDATIONCOLLECTIONURI)$env:SYSTEM_TEAMPROJECTID/_apis/build/builds/$env:BUILD_BUILDID/logs?api-version=5.0"
$response = Invoke-RestMethod -Uri $uriLogs -Method Get -Headers #{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$logUrl = $response.value[-1].url
$webContent = Invoke-WebRequest -Uri $logUrl -Method GET -Headers #{ Authorization = "Bearer $env:SYSTEM_ACCESSTOKEN"}
$ErrorMessage = $webContent.Content.tostring() -split "[`r`n]" | select-string "[[error]]"
Write-Output "Error lines found " $ErrorMessage

Get Azure VM size inside on a windows machine

I do only have access on a Windows VM which is hosted on Azure.
Is there a way to find out the VM size inside of the virtual machine, lets say for example over commandline (powershell / cmd), without having further information about the envrionment like for example Ressource Group or VM Ressource Name?
I did find a way like this, but it requires the information about the name of VM and Ressource group:
$VM = Get-AzureRMVM –Name HSG-Linux1 –ResourceGroupName HSG-AzureRG
$VM.HardwareProfile
Source
You can use the metada to get the VM information inside the VM like this:
Invoke-RestMethod -Headers #{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance/compute?api-version=2019-03-11 -Method get
For more details, see Azure Instance Metadata service. You can also put the output as a variable, then get the special properties. For example:
$vm = Invoke-RestMethod -Headers #{"Metadata"="true"} -URI http://169.254.169.254/metadata/instance/compute?api-version=2019-03-11 -Method get
$vm.vmSize
Or get the property directly through the request like this:
$vmSize = Invoke-RestMethod -Headers #{"Metadata"="true"} -URI "http://169.254.169.254/metadata/instance/compute/vmSize?api-version=2019-03-11&&format=text" -Method get

Calling a REST API on a .NETCore server

I am not a linux guru and using curl is too complicated for me. So I found httpie and try to call one of my own REST servers written in C# against .NETCore 2 and running on Docker under CentOS 7.
I have a working Powershell script on Windows and try to port this to a bash script using httpie, unfortunately with no luck.
The API generates a file on the server and returns it as octet-stream. The headers on the server are defined as follows:
Options = new Dictionary<string, string>
{
{"Content-Type", "application/octet-stream"},
{"X-Api-DocType", filetype},
{"X-Api-DocLength", responseStream.Length.ToString()}
};
My POWERSHELL script (which works fine) looks like so:
$request = #{
Namespace = "MyApplication.MessagingApi"
Version = "current"
DataDefinitionLanguage = "Csharp"
MakePartial = $false
MakeVirtual = $false
}
$docPath = "D:\Projects\AccountingApiMessageDefinitions.cs"
$Url = "http://172.16.63.241:6083/bbopman/messaging/apireferences"
$response = Invoke-WebRequest -Method Get -Uri $Url -Body $request
Set-Content -Path $docPath -Encoding Byte -Value $response.Content
I ported this to a bash script as follows:
#!/bin/bash
url="http://172.16.63.241:6083/bbopman/messaging/apireferences"
request="Namespace=='MyApplication.MessagingApi' Version=='current' DataDefinitionLanguage=='Csharp'"
http -d GET $url $request > ~/AccountingApiMessageDefinitions.cs
What I get back is BAD REQUEST but on the server the call is successfully processed. So I guess I make something wrong when reading the response, but don't know what.... Here is the output in my shell:
$ ./ExecuteRestCall.sh
HTTP/1.1 400 Bad Request
Content-Type: text/html
Date: Wed, 30 May 2018 18:45:50 GMT
Server: Kestrel
Transfer-Encoding: chunked
Vary: Accept
X-Powered-By: ServiceStack/5.02 NETStandard/.NET
Could anybody tell me how I correctly call this from a Linux shell? Many thanks.
You don't need quotes for the parameter values for httpie. Send them without quotes.
request="Namespace==MyApplication.MessagingApi Version==current DataDefinitionLanguage==Csharp"
Also, you seem to be missing the MakePartial and MakeVirtual parameters in the shell script version.
By the way: "on the server the call is successfully processed" - this cannot be true. The bad request status is returned by the server, not something that happens on the client.

Ansible Tower REST API: Is there any way to get the logs/output of a job?

I have a Ansible job started by another Process. Now I need to check the status of the current running job in Ansible Tower.
I am able to track the status whether it is running/success/failed/canceled with /jobs/{id} using REST API.
But I would also need the information of the console logs/ouputs of the task for processing as well. Is there any direct API call for the same?
You can access the job log via a link similar to:
https://tower.yourcompany.com/api/v1/jobs/12345/stdout?format=txt_download
Your curl command would be similar to:
curl -O -k -J -L -u ${username):${password} https://tower.company.com/api/v1/jobs/${jobnumber}/stdout?format=txt_download
obviously replacing ${username}, ${password}, and ${jobnumber} with your own values
The curl flags used:
-O : output the filename that is actually downloaded
-k : insecure SSL (don't require trusted CAs)
-J : content header for file download https://curl.haxx.se/docs/manpage.html#-J
-L : follow redirects
-u : username and password
You can do this via their restful call.
To get the job number use a GET against https://yourtowerinstance/api/v2/job_templates/
this will return your templates, and their IDs
To get the output in real time I use this powershell code
$stdouturl = "https://yourtowerinstance/api/v2/jobs/$($templateResult.id)/stdout/?format=txt"
$resultstd = Invoke-Restmethod -uri $stdouturl -Method 'Get' -Headers $authHeader
while ($resultstd -notmatch 'PLAY RECAP') {
$resultstd = Invoke-Restmethod -uri $stdouturl -Method 'Get' -Headers $authHeader
start-sleep -s 5
}
$resultstd
Once you launch a template you get the job-id in response but I don't think there is a API to get the output of the job. However from the dashboard under jobs section you can download the individual job output.

Resources