GoPro/Powershell: Protocol violation when trying to read .json status file - json

I try to read the status file of a GoPro Hero5.
If you type "http://10.5.5.9/gp/gpcontrol/status" while connected to the GoPro-Wifi the camera responds with a json status file.
I want to get this status file with powershell, with the Invoke-RestMethod:
Invoke-RestMethod -Uri "http://10.5.5.9/gp/gpControl/status"
This gives me the following error:
Invoke-RestMethod : The server committed a protocol violation.. Section=ResponseHeader Detail=Auf CR muss LF folgen
At line:3 char:13
+ $response = Invoke-RestMethod -Uri "http://10.5.5.9/gp/gpControl/stat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
Here is an example on what the status file should look like: https://github.com/KonradIT/goprowifihack/blob/master/HERO4/CameraStatus.md
Any hints how to get the file with powershell would be appreciated!

Related

Issues with calling json file using powershell

I am trying to run simple power-shell command to call json file to deploy some resources on Azure but it give me the same errors each time I try to run the file.The error message I am getting is -
New-AzDeployment : Cannot retrieve the dynamic parameters for the cmdlet. Invalid property identifier character: �. Path '', line 2, position 0.
At line:1 char:1
+ New-AzDeployment -Name $DeploymentName -Location uksouth -TemplateUri ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [New-AzDeployment], ParameterBindingException
+ FullyQualifiedErrorId : GetDynamicParametersException,Microsoft.Azure.Commands.ResourceManager.Cmdlets.Implementation.NewAzureDeploymentCmdlet
below is the powershell, I am trying to run -
$DeploymentSubscription = "Visual Studio Enterprise with MSDN"
$DeploymentRSG = "xyz"
$DeploymentName = "vvv"
$ParameterRoute = "D:\xyz\json\VM\"
$ParameterFile = $ParameterRoute + "Build.json"
$TemplateFileRoute = "D:\xyz\VM\"
$TemplateFileName = "Deploy_RG_Resources.json"
$TemplateFile = $TemplateFileRoute + $TemplateFileName
$TemplateFileURi = $TemplateFileRoute + $TemplateFileName
New-AzResourceGroupDeployment -Name $DeploymentName -ResourceGroupName $DeploymentRSG -DeploymentDebugLogLevel All -Verbose -TemplateUri $TemplateFileURi -TemplateParameterFile $ParameterFile
Thanks
That error is very specific.
What you are passing is valid. The help files for the cmdlet shows this command in action.
New-Az​Resource​Group​Deployment
$newAzResourceGroupDeploymentSplat = #{
TemplateParameterFile = "D:\Azure\Templates\EngSiteParams.json"
TemplateObject = $TemplateObject
ResourceGroupName = "ContosoEngineering"
}
New-AzResourceGroupDeployment #newAzResourceGroupDeploymentSplat
Note the fully qualified UNC to the .json file.
But you are doing this..
$ParameterFile = $ParameterRoute + "Build.json"
... and based on your post, that is not the path to your .json file.
Try using -TemplateFile as parameter instead of -TemplateUri if your Template file in on local machine.
This is resolved now .Issue was with the JSON template syntax.Quite weird though I have validate the template many times

ConverFrom-JSON Error: Invalid JSON Primitive || PowerShell PoweBi Embeded Token Generator Code

ConverFrom-JSON : Invalid JSON Primitive error, primitive is not being recognized by PowerShell for some reason.
I need to generate PowerBi Embedded Tokens for my PowerBi reports. I can log in to my Microsoft account with out no problem and can invoke the report as well. At the end code supposed to return a embedded token to me, but I encounter with error :
ConvertFrom-Json : Invalid JSON primitive: .
At line:13 char:21
+ $json = $response | ConvertFrom-Json
+ ~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [ConvertFrom-Json], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.ConvertFromJsonCommand
I have already tried the Out-String and Raw with Get-Content method, they did not work either.
CODE:
//Sign in with a user that has admin rights to App Workspace
Login-PowerBI
//Regular Report
$url = "https://app.powerbi.com/reportEmbed?reportId=5515f33b-c114-41c9-a925-d1f85c323dab&groupId=e53e4fcd-16f8-46ef-8740-8e7167562ceb&autoAuth=true&ctid=c760270c-f3da-4cfa-9737-03808ef5579f/GenerateToken"
$body = "{ 'accessLevel': 'View' }"
$response = Invoke-PowerBIRestMethod -Url $url -Body $body -Method Post
$response
$json = $response | ConvertFrom-Json
$json.token
It should return a huge paragraph of gibberish code, which will be my report's embedded token.
$url is incorrect - need to call api.powerbi.com
https://api.powerbi.com/v1.0/myorg/groups/{GroupID}/reports/{ReportID}/GenerateToken
try
$url = "https://api.powerbi.com/v1.0/myorg/groups/e53e4fcd-16f8-46ef-8740-8e7167562ceb/reports/5515f33b-c114-41c9-a925-d1f85c323dab/GenerateToken"

Post to json rest API from powershell

Hi can someone tell me where i'm going wrong here, i get the following error executing an API call from powershell. I have tried to format the body i many ways but cannot get it to work.
Invoke-RestMEthod : Cannot send a content-body with this verb-type.
At line:7 char:20
+ ... sponseKan = Invoke-RestMEthod -Uri $kanboardserver -ContentType "app ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ProtocolViolationException
+ FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
My Code;
$headersKan = New-Object "System.Collections.Generic.Dictionary[[String],[String]]" -ErrorAction Stop
$headersKan.Add("X-API-Auth", "jsonrpc:$kanboardtoken")
$bodyKan = #{jsonrpc = "2.0"
id = "1"
method = "getAllProjects"
} | ConvertTo-Json
$responseKan = Invoke-RestMEthod -Uri $kanboardserver -ContentType "application/json" -Headers $headersKan -Body $bodyKan -ErrorAction Stop
Write-Host $responseKan
The default http verb is GET which does not allow a body/payload. Pass POST as method argument and do not json-serialize the body yourself.

How to POST in powershell to canvas LMS

I am currently making an API for an university. The purpose of this project to to look at every course and check if it contains a certain module and if not, add one in. Seems fairly simple enough, but the thing that is making the project complicated is on learning the syntax on to actually do it! I am writing this code in powershell and I have tried to use curl and invoke web request. I tried following canvas' documentation, but I cannot get it. Here are two instances I have, can you please show me how to properly format this.
######this is the first way I tried it and the error I get Invoke-WebRequest : {"message":"missing module parameter"}
$temp2=($mainURL+$course.ID+"/modules"+$securityToken)
$reply = curl -Uri $temp2 -Method Post -Body '{"module[name]"="Getting started","module[position]"="1"}'
#######
#########This is the second way I've tried and the error I get Invoke-WebRequest : The remote server returned an error: (422) Unprocessable Entity.
$url="https://University.instructure.com/api/v1/courses/1371/modules?access_token=abc123"
$body= "{'module[name]'='Getting started,'module[position]'=1}"
Invoke-WebRequest -Uri $url -ContentType "application/json" -Method Post -Body $body
#########
Documentation from the website https://canvas.instructure.com/doc/api/index.html
UPDATE 5-26-2016
I have figured out how to properly format the body for the message. Now I am getting the error message
$header = #{"Authorization"="Bearer "+ $security_token}
$body= '{"module[name]":"Getting started","module[position]":"1"}'
$curlly=Invoke-WebRequest -Headers $header -Body $body -Method Post -ContentType "application/json" -Uri ($url_main+"courses/1371/modules")
$module = ConvertFrom-Json $curlly.Content
curl : Invalid URI: The hostname could not be parsed.
At line:1 char:1
+ curl
+ ~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], UriFormatException
+ FullyQualifiedErrorId : System.UriFormatException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I really do not know what to do at this point any guidance will be appreciated at this point.
After a week of fiddling around with the actual JSON format, I took out the content type on the Invoke-WebRequest to see if the server would be able to just guess the format. It worked!

Problems in reading a commandline json object using powershell

I have a powershell script "test.ps1" which takes a json string as a command line input & tries to parse it.
The powershell script is as below -
param(
[string]$json = $(throw 'test')
)
$currentPath = Get-Location
[Reflection.Assembly]::LoadFile("$currentPath\Newtonsoft.Json.dll")
$result = [Newtonsoft.Json.Linq.JObject]::Parse($json)
foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString()}
But it is giving me the below error -
PS C:\Users\aghosh.RJFDEV\Documents> ./test.ps1 '{"DevResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"QaResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"PreProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"}],"ProdResults":[{"TechnologyName":"TFS","RuleName":"Alt CI ID for ESB","OutputValue":"ESClientCenter"},{"TechnologyName":"TFS","RuleName":"TFS Team Project Name","OutputValue":"ClientCenter"},{"TechnologyName":"TFS","RuleName":"Process Template","OutputValue":"Raymond James CMMI V3.5"}]}'
GAC Version Location
--- ------- --------
False v2.0.50727 C:\Users\aghosh.RJFDEV\documents\Newtonsoft.Json.dll
You cannot call a method on a null-valued expression.
At C:\Users\aghosh.RJFDEV\Documents\test.ps1:13 char:82
+ foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString <<<< ()}
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
You cannot call a method on a null-valued expression.
At C:\Users\aghosh.RJFDEV\Documents\test.ps1:13 char:82
+ foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString <<<< ()}
+ CategoryInfo : InvalidOperation: (ToString:String) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
My powershell version is 2.0 & the Json.Net library version is 3.5.
Please help me with the error or please suggest any other suitable approach.
Thanking in advance.
Change this line:
foreach($unit in $result["DevResults"]) {Write-Host $unit.TechnologyName.ToString()}
To this:
foreach($unit in $result["DevResults"]) {Write-Host $unit["TechnologyName"].ToString()}