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"}]
}
Related
When I was following this document Publish Test Results task
I didn't find any field to link a work item to a test case. Do I need to call an api to do this? Or is there any field that isn't in the document?
Thanks!
We could add the work item in the test run result summary page.
In the build summary page->click the tab test->select the test result, we could see the run id and resultId in the URL
Open Test Plans->click the tab Runs->select the test run and open it->click the tab Test results and open it->we could see the button Bug->click it, then we could select create new bug or add existing bug. You could check the pic below.
Update1
We could do this via REST API to add work item.
Sample:
PATCH https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results?api-version=6.0
Request Body:
[
{
"id": {Result ID},
"state": "Completed",
"comment": "{Comment}",
"associatedBugs": [
{
"id": {Bug ID}
}
]
}
]
Then we could check it in the UI or this REST API
Sample:
GET https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results/{testCaseResultId}?detailsToInclude=workItems&api-version=6.0-preview
Note: We could get test run ID in the task publish test result log. We could add power shell task and enter the script to add the work item in the pipeline.
Power shell script sample:
$connectionToken="{PAT}"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$Url = "https://dev.azure.com/{organization}/{project}/_apis/test/Runs/{runId}/results?api-version=6.0"
$Body = #"
[
{
"id": {Result ID},
"state": "Completed",
"comment": "{Comment}",
"associatedBugs": [
{
"id": {Bug ID}
}
]
}
]
"#
$Result = Invoke-RestMethod -Uri $Url -ContentType "application/json-patch+json" -Body $Body -Headers #{Authorization=("Basic {0}" -f $base64AuthInfo)} -Method PATCH
I have a very basic pipeline with 4 stages, for prod stage i want to run from separate pipeline . I was trying to use build queue rest api as per documentation and using below payload
{
"stagesToSkip": [
"Build"
],
"definition":
{
"id": "24"
},
"resources": {
"repositories": {
"self": {
"refName": "refs/heads/master"
}
}
},
"variables": {}
}
I got this payload when i manually run the build by selecting only one stage and run it works as expected as highlighted but whn i use the rest api with the same request payload , it runs both the stages.
any pointers what i'm doing wrong??
You can use Run Pipeline rest api to skip stages.
POST https://dev.azure.com/{organization}/{project}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1
See below example in powershell script:
$url="https://dev.azure.com/{org}/{proj}/_apis/pipelines/{pipelineId}/runs?api-version=6.0-preview.1"
$PAT='personal access token'
$body='{
"stagesToSkip":[ "Test" ]
}'
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$result1 = Invoke-RestMethod -Uri $url -Headers #{authorization = "Basic $base64AuthInfo"} -Method post -Body $body -ContentType "application/json"
See below test result. The stage was successfully skipped.
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
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.
I'm trying to query Elasticsearch from PowerShell using Invoke-WebRequest. Elasticsearch requires that the GET request have a body which iwr forbids. Is there a workaround or something? Maybe some other program that allows this? curl on POSIX allows this. I know it's not standard, but take it up with Elasticsearch.
Here's what I tried:
Invoke-WebRequest http://localhost:9200/index* -Body '{
"size": 1,
"sort": [
{ "timestamp": {"order": "asc"}}
],
"query": {
<query here>
}
}' -Method GET
This returned:
Invoke-WebRequest : Cannot send a content-body with this verb-type.
Thanks.