PowerShell Discord Webhook - powershell

I am getting an error while using this code
$uri = "https://discord.com/api/webhooks/760116538895499265/yhQdNc10asfDzt8hldAJp32et1Gp_1-mvqIGPWZiAMcOwFjb5aqf7uNEF7MX6i15UKJK"
$hash = #{ "content" = "heyyy"; }
$JSON = $hash | convertto-json
Invoke-WebRequest -uri $uri -Method POST -Body $JSON
Invoke-WebRequest : {"message": "Cannot send an empty message", "code": 50006}
At line:7 char:1
+ Invoke-WebRequest -uri $uri -Method POST -Body $JSON
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
what is this due to?

You should specify the Content-Type to application/json in the request:
Invoke-WebRequest -uri $uri -Method POST -Body $JSON -Headers #{'Content-Type' = 'application/json'}
See: https://github.com/discord/discord-api-docs/issues/1210

Related

Trying to use a azure Test Plan api to add tester name in Test suite from power shell script

$UpdateTesterNameUri = "https://dev.azure.com/MyOrg/MyProj/_apis/test/Plans/" + $Id + "/Suites/121221/points/" + $node + "?api-version=6.0"
Write-Output $UpdateTesterNameUri
$testerNameBody =
#{
tester = #{
"displayName" = "Veeresh Kokkalla "
}
}
$testerName = $testerNameBody | ConvertTo-Json
Invoke-RestMethod -Uri $UpdateTesterNameUri -Headers $headers -Method Patch -Body $testerName -ContentType 'application/json'
Getting following issue
Invoke-RestMethod : {"$id":"1","innerException":null,"message":"Value cannot be null.\r\nParameter name: tester","typeName":"System.ArgumentNullException,
mscorlib","typeKey":"ArgumentNullException","errorCode":0,"eventId":0}
At C:\Users\v-vkokkalla\Downloads\ReleaseTrain - Dailymveeresh.ps1:173 char:13
+ Invoke-RestMethod -Uri $UpdateTesterNameUri -Headers $hea ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
When I put debug it shows tester has values.
From your PowerShell sample, you need to define the userID in the request body. Then you can update the tester of the test point.
Here is an PowerShell example:
$token = "PAT"
$url="https://dev.azure.com/org/project/_apis/testplan/Plans/TestPlanID/Suites/TestSuiteID/TestPoint?includePointDetails=true&returnIdentityRef=true&api-version=6.0"
$token = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($token)"))
$JSON = #"
[
{
"id":Testpointid,
"tester":
{
"id":"userid",
"displayName":"xx"
}
}
]
"#
$response = Invoke-RestMethod -Uri $url -Headers #{Authorization = "Basic $token"} -Method Patch -ContentType application/json -Body $JSON
Write-Host "$response"

Webhook With PowerShell Discord

I am getting an error while using this code
$hookUrl = 'https://discord.com/api/webhooks/977171177120358412/U8T5Nv5BDCOL70IeXyPjA8Vlxo-4BB2b6QXhG0nAwD_gsw2AHXCSbbcjmiFvLlFFZD6I'
$content = #"
You can enter your message content here.
With a here-string, new lines are included as well!
Enjoy.
"#
$payload = [PSCustomObject]#{
content = $content
}
Invoke-RestMethod -Uri $hookUrl -Method Post -Body ($payload | ConvertTo-Json)
Invoke-RestMethod : {"message": "Cannot send an empty message", "code": 50006}
At line:17 char:1
+ Invoke-RestMethod -Uri $hookUrl -Method Post -Body ($payload | Conver ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
eption
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I have no idea where I made a mistake, I would be glad if you could help me.
Invoke-restmethod -Uri $hookUrl -Method Post -Body ($payload | ConvertTo-Json) -Headers #{ "Content-Type" = "application/json" }
This seems to work

RestAPI authentication with PowerShell

I am trying to access the API of OpenProvider using PowerShell and I can't seem to get past Authentication.
The documentation for the API is here : https://support.openprovider.eu/hc/en-us/articles/360025683173-Getting-started-with-Openprovider-API
And my code looks like this:
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","myusername")
$Headers.Add("hash","APIpasswordhashgoeshere")
return $Headers
}
$header = Get-ConfHeaders
Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
The response get is :
Invoke-RestMethod : The remote server returned an error: (500) Internal Server Error.
At line:36 char:1
+ Invoke-RestMethod -Method Post -Uri $EndPoint -Headers $header
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I am by no means an expert when it comes to API and any help would be appreciated.
Ok, I think the API documentation here leaves a lot to be desired.
You need to include the authentication in the body and it needs to be converted to JSON format. So the working code looks like this:-
$EndPoint = "https://api.openprovider.eu/v1beta/auth/login"
function Get-ConfHeaders
{
##Configure headers
$Headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$Headers.Add("ip","0.0.0.0")
$Headers.Add("username","username")
$Headers.Add("password","passwordhere")
return $Headers
}
$header = Get-ConfHeaders | ConvertTo-Json
Invoke-RestMethod -Method Post -Uri $EndPoint -body $header -ContentType 'application/json'
Thanks for the help everyone.

POST method in powershell gives 405 error

I have a POST method that works fine in Postman but I can't get it working in powershell.
The client id and client secret are setup in AWS cognito.
I am using basic authentication to retrieve the access token.
Below is my code,
$encodedlogin="MNBvbmZfYWsxUTRocG1qOG5zOGZqZ8xvN646a1plMDNsMTZjY6xucTA5MzVkOGg2aGdlaG1mdXAxaHZkMmlxaXY0PKOdM3BcxNBtPLs5"
$authorisation = "Basic " + $encodedlogin
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Authorization",$authorisation)
$headers.Add("Accept","application/json")
$headers.Add("Content-Type","application/json")
$headers.Add("ResponseType","id_token")
$body = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$body.Add("grant_type","client_credentials")
$body.Add("scope","cybergate-gateway-resources/cybertron.fullaccess")
$uri = "https://cybergate.auth.eu-west-2.amazoncognito.com/oauth2/token"
$response = Invoke-WebRequest -Uri $uri -Headers $headers -body $body -Method Post -ContentType "application/json"
The error as follows,
Invoke-WebRequest : The remote server returned an error: (405) Method Not Allowed.
At C:\CyberGate\src\CyberGate.API\Scripts\Deploy\CyberGate.ps1:33 char:17
+ ... $response = Invoke-WebRequest -Uri $uri -Headers $headers -body $body ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Any ideas?
Thanks

Power BI Powershell Export to PDF Script not Working

Below code I m using to export pdf.When i pass direct value value in line 5 then its working but when i m pass $id in line 5,it snot working.
1 $uri = "https://api.powerbi.com/v1.0/myorg/reports/$Report_ID/ExportTo"
2 $body = "{"format":"pdf"}"
3 $FileExport = Invoke-RestMethod -Uri $uri –Headers $auth_header –Method POST -body $body
4 $id = $FileExport.id
5 $uri = "https://api.powerbi.com/v1.0/myorg/reports/$Report_ID/exports/$id/file"
6 Invoke-RestMethod -Method GET -Uri $uri –Headers $auth_header -OutFile "\Desktop\PDF\test.pdf"
Below error
*Invoke-RestMethod : The remote server returned an error: (400) Bad Request.
At line:79 char:2
Invoke-RestMethod -Method GET -Uri $uri –Headers $auth_header -OutFi ...
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand*
$uri = "https://api.powerbi.com/v1.0/myorg/reports/$Report_ID/exports/$id/file"
This is a string, you have to break it up or it will be read literally as part of the string.
$uri = "https://api.powerbi.com/v1.0/myorg/reports/" + $Report_ID + "/exports/" + $id + "/file"