PowerShell GraphQL Mutation Parsing Error - powershell

I'm querying the Monday API v2 (https://monday.com/developers/v2) I'm getting stuck in getting the PowerShell query to work, the script is below:
[Net.ServicePointManager]::SecurityProtocol = "tls12, tls11, tls"
$url = "https://api.monday.com/v2/"
$hdr = #{ }
$hdr.Add("Authorization", "redacted")
$hdr.Add("Content-Type", "application/json")
$bodytxt = '{"query":"mutation{change_column_value (board_id: 528033866, item_id: 574335355, column_id: \"status_1\", value: "{\"index\": 1}") {id}}"}'
$response = Invoke-WebRequest -Uri $url -Headers $hdr -Method Post -body $bodytxt
Write-Host $response
And it keeps returning
{"errors":[{"message":"Parse error on \": 1}\" (STRING) at [1, 110]","locations":[{"line":1,"column":110}]}],"account_id":5305133}
The mutation query in Postman works fine
change_column_value (board_id: 528033866, item_id: 574335355, column_id: "status_1", value: "{\"index\": 1}"){id}
}
I have tried working with what quotes are escaped, but I still haven't been able to get it to run succesfully. I was referencing the tips here https://www.reddit.com/r/PowerShell/comments/b9jasa/query_graphql_with_powershell/
Any thoughts on how to fix this error?
EDIT: Sorry forgot to add the current query (not mutation) that works fine through Powershell
$bodytxt = '{"query":"{ boards (ids: 528033866) { groups (ids: \"group_title\"){ items () { id name updates () { body } column_values () { id title value text } } title } } }"}'

The easiest way to create a GraphQL query is
create a hashtable with HERE-STRINGS
convert it to a JSON string with ConvertTo-Json.
$bodytxt = #{"query" = #'
mutation {
change_column_value(
board_id: 123123
item_id: 456456
column_id: "status1"
value: "{\"index\":1}"
) {
id
}
}
'#
} | ConvertTo-Json
White spaces inside #'...'# are optional.
reply to this comment
it should always work in the API call with the GraphQL?
I think it depends on services, but in most cases, yes. application/json is the GraphQL default content type for the request body.
and for monday.com
Be sure to use the application/json content type

Related

Pass Azure Automation Runbook Webhook Body to Logic Apps

I have created a webhook in Azure Automation. I am looking to pass the body when the webhook is called to a logic app. Please see the powershell code designed of runbook.
param
(
[Parameter(Mandatory=$false)]
[object] $WebhookData
#[string] $jobId
)
if ($WebhookData.RequestBody) {
$names = (ConvertFrom-Json -InputObject $WebhookData.RequestBody)
Write-Output $names
$webhookName = $WebhookData.WebhookName
$logicappURI = "https://logicapp/api/test/triggers/manual/invoke/properties/"+$webhookName+"?api-version=2022-05-01&sp=%2Ftriggers%2Fmanual%2Frun&sv=1.0&sig={signature_certificate"
$response = Invoke-WebRequest -Method GET -Uri $logicappURI
}
else {
Write-Output "Hello World!"
}
Currently I am able to call the logic app and pass the webhook name, but the i would like to pass the complete request body. Below is the request body for webhook.
{
"WebhookName": "wh-test-webhook-ext-cus-001",
"RequestBody": "[{\"Name\": \"Hawaii\" },{\"Name\": \"Seattle\"},{ \"Name\": \"Florida\"}]",
"RequestHeader": {
"Connection": "Keep-Alive",
"Host": "14304127-e302-4499-b819-8ac4493555e9.webhook.cus.azure-automation.net",
"User-Agent": "Mozilla/5.0",
"x-ms-request-id": "47ca3f1a-2418-4553-94c4-9f20c2095c80"
}
}
The logic app is designed to trigger when an HTTP request is recieved. As shown in the image I am passing passing webhook name. Similarly is there a way I can pass the complete ReuqestBody.
Thanks in advance.
Logic App Design

Cannot mention person in Adaptive Card using Teams PowerShell Module

I have registered an Incoming Messages Webhook in one of my Teams channels.
I am using Microsoft Teams PowerShell to create an adaptive card and send it to a channel:
$SimpleCard = #{
type = "message";
attachments = #(#{
contentType = 'application/vnd.microsoft.card.adaptive';
contentUrl = $null;
content = #{
'$schema' = 'http://adaptivecards.io/schemas/adaptive-card.json';
type = 'AdaptiveCard';
version = '1.2';
body = #(#{
type = 'TextBlock';
text = "Hello <at>andrea.tino#mycompany.com</at>";
})
}
});
msteams = #{
entities = #(#{
type = 'mention';
text = "<at>andrea.tino#mycompany.com</at>";
mentioned = #{
id = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx";
name = 'Andrea Tino';
}
})
}
}
Invoke-RestMethod -Method post
-ContentType 'Application/Json'
-Body ($SimpleCard | ConvertTo-Json -Depth 10)
-Uri "https://myorg.webhook.office.com/webhookb2/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx#xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/IncomingWebhook/xxxxxxxx/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
The card is correctly sent but the mention is not correctly rendered.
Wrong ID?
It is worth mentioning that the doc makes explicit reference to a type of User ID:
{
"type": "mention",
"text": "<at>John Doe</at>",
"mentioned": {
"id": "29:123124124124",
"name": "John Doe"
}
}
You can see that the ID in the example is: "id": "29:123124124124" while mine is a GUID. I retrieved that User ID by means of Get-TeamUser.
What am I doing wrong?
Mention in adaptive card is supported only in context of bot and the available ways to get id are listed in the document - mention-support-within-adaptive-cards-v12
Concerned team is working to support user mention using AAD Object Id and UPN(use Graph API) in Incoming Webhook.
ID and Name can be fetched from the activity as follows
from: {
id: '29:15fREhoUuf6vVCOuaJYVH-AB6QXXX',
name: 'MOD Administrator',
aadObjectId: 'XXXX'
}
Giving a reference that will help - mention-support-within-adaptive-cards-v12

Querying GraphQL with PowerShell

So I've been trying to query GraphQL from PowerShell. Everything looks perfect in a cmd, but I cannot make it work in PS, see, I have this code so far:
$oAuth2TokenUrl = "https://api.cloudflare.com/client/v4/zones/0000/analytics/dashboard"
$accessKey = '1111'
$Cloudflare_Oauth_Header = #{
"Authorization" = "Bearer $accessKey";
}
$query = #'
{ "query": "query { viewer {
zones(filter: {zoneTag: 0000}) {
httpRequests1mGroups(orderBy: [datetimeMinute_ASC], limit: 1000, filter: {datetime_geq: "2019-09-08T20:00:00Z", datetime_lt: "2019-09-08T20:02:00Z"}) {
dimensions {datetimeMinute}
sum {
browserMap {
pageViews
uaBrowserFamily
}
bytes
cachedBytes
cachedRequests
contentTypeMap {
bytes
requests
edgeResponseContentTypeName
}
clientSSLMap {
requests
clientSSLProtocol
}
countryMap {
bytes
requests
threats
clientCountryName
}
encryptedBytes
encryptedRequests
ipClassMap {
requests
ipType
}
pageViews
requests
responseStatusMap {
requests
edgeResponseStatus
}
threats
threatPathingMap {
requests
threatPathingName
}
}
uniq {
uniques
}
}
}
} }" }
'#
$Cloudflare_zone = Invoke-RestMethod -Method Post -Headers $Cloudflare_Oauth_Header -ContentType "application/json; charset=utf-8" -Uri $oAuth2TokenUrl -Body $query
And I keep getting the error Invoke-RestMethod : The remote server returned an error: (501) Not Implemented.
But if I curl in cmd it returns what is expected and no error is shown.
Please let me know if you need more details.
I would suggest getting a simple query to work first, and then expanding it to be more complex. Also, be sure to use -ContentType "application/json" which some GraphQL servers require. The switch -UseBasicParsing can also help avert error but probably isn't the issue here. Lastly, you might also need a trailing slash in the URL: E.g., http://127.0.0.1:8080/graphql/.
This thread might also help:
https://www.reddit.com/r/PowerShell/comments/b9jasa/query_graphql_with_powershell/
I have a GraphQL module for PowerShell with a single function (Invoke-GraphQLQuery that may help you here). Link: https://github.com/anthonyg-1/PSGraphQL
But I definitely agree that you should start with a much smaller query. Check out some of the examples in my readme or run Get-Help Invoke-GraphQLQuery for more.

Using GoPhish API in Powershell to Launch Campaigns

currently i'm attempting to setup GoPhish to launch campaigns through a Windows PowerShell script using it's API. The issue that i'm running into that is i'm getting this error.
Invoke-WebRequest : { "message": "Invalid JSON structure", "success": false, "data": null }
The thing is I have triple checked my JSON structure and it doesn't show any errors in JSON formatting checkers. So my question is if there are any errors in the code I have below for actually pushing these campaigns via PowerShell.
$jsonString = #"
{
"name":"Test6",
"template":{
"name":"Test"
},
"url":"http://redacted",
"page":{
"name":"Test"
},
"smtp":{
"name":"Test SMTP"
},
"launch_date":"2019-25-07T08:20:00+00:00",
"send_by_date":null,
"groups":{
"name":"Test Group"
}
}
"#
Invoke-WebRequest -Uri https://redacted/api/campaigns/?api_key=redacted -Method POST -Body $jsonString
So if you see any errors in what I have an could help me out, that'd be awesome. The API documentation shows that it should be in this format
{
"name":"CC Example Campaign",
"template":{"name":"Example Template"},
"url":"http://localhost",
"page":{"name":"Example Landing Page"},
"smtp":{"name":"Example Sending Profile"},
"launch_date":"2018-10-08T16:20:00+00:00",
"send_by_date":null,
"groups":[{"name":"Example Group"}]
}

Get value in JSON object from external URI

In my PowerShell code I have:
$retrieveSourceBranchFromBuildURL = "https://dev.azure.com/$organization/$project/_apis/build/builds/$buildId" + "?api-version=5.0"
When I navigate to the URL in the browser I see:
{
_links: {
self: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46070"
},
web: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_build/results?buildId=46070"
},
sourceVersionDisplayUri: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/sources"
},
timeline: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/builds/46070/Timeline"
},
badge: {
href: "https://dev.azure.com/something/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/status/67"
}
},
parameters: "{"system.pullRequest.pullRequestId":"5766","system.pullRequest.sourceBranch":"refs/heads/pb/31333-test-branch/name","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"1cf19b95a59478a8554c2c03d65dcefe203529a6","system.pullRequest.sourceRepositoryUri":"https://something#dev.azure.com/something/something%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"1"}",
}
The $buildInformation variable:
$buildInformation = Invoke-RestMethod -Uri $retrieveSourceBranchFromBuildURL -Headers #{Authorization = $pat } -Method Get -ContentType 'application/json'
When I output the value of $buildInformation I get:
#{_links=; properties=; tags=System.Object[]; validationResults=System.Object[]; plans=System.Object[]; triggerInfo=; id=46079; buildNumber=20190624.3; status=completed; result=succeeded; queueTime=2019-06-24T07:57:05.7271255Z; startTime=2019-06-24T07:57:12.8021227Z; finishTime=2019-06-24T08:12:27.003113Z; url=https://dev.azure.com/embrace/7720f8d2-bf64-47d9-8b10-53f21220d54d/_apis/build/Builds/46079; definition=; buildNumberRevision=3; project=; uri=vstfs:///Build/Build/46079; sourceBranch=refs/pull/5740/merge; sourceVersion=735f7a813c343dbbca5e1d1b3e966e0bad1db762; priority=normal; reason=pullRequest; requestedFor=; requestedBy=; lastChangedDate=2019-06-24T08:14:41.11Z; lastChangedBy=; parameters={"system.pullRequest.pullRequestId":"5740","system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team","system.pullRequest.targetBranch":"refs/heads/master","system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276","system.pullRequest.sourceRepositoryUri":"https://embrace#dev.azure.com/embrace/Embrace%20Suite/_git/client-web","system.pullRequest.pullRequestIteration":"5"}; orchestrationPlan=; logs=; repository=; keepForever=False; retainedByRelease=True;
When I output the value of $buildInformation.parameters:
Write-Host $buildInformation.parameters
{
"system.pullRequest.pullRequestId":"5740",
"system.pullRequest.sourceBranch":"refs/heads/master-md/social-create-team",
"system.pullRequest.targetBranch":"refs/heads/master",
"system.pullRequest.sourceCommitId":"e744e5c35bc3fd1539d5c49daa29147f048f3276",
"system.pullRequest.sourceRepositoryUri":"https://embrace#dev.azure.com/embrace/Embrace%20Suite/_git/client-web",
"system.pullRequest.pullRequestIteration":"5"
}
But when I try to read a value:
Write-Host $buildInformation.parameters.system.pullRequest.sourceBranch
The output is empty.
So how do I retrieve the parameters.system.pullRequest.sourceBranch value?
To access the property with 'special characters' in its name, enclose the parameter name inside quotes like so:
$buildInformation.parameters.'system.pullRequest.sourceBranch'
NOTE: The trick here is that the value of parameters is inside double quotes. Therefore you might not be able to access the properties under parameters the usual way. To workaround it, you can make a conversion directly on parameters:
$convertedParams = $buildInformation.parameters | ConvertFrom-Json
# Access desired property
$convertedParams.'system.pullRequest.sourceBranch'
When you access the property like you tried
$buildInformation.parameters.system.pullRequest.sourceBranch
the structure of your JSON should be like this:
PS> $buildInformation = '{
"parameters": {
"system": {
"pullRequest":{
"sourceBranch": "refs/heads/master-md/social-create-team"
}
}
}
}' | ConvertFrom-Json
# Checking output
PS> $buildInformation.parameters.system.pullRequest.sourceBranch
refs/heads/master-md/social-create-team

Categories