Powershell json, includeFields - json

I am trying to request information from one of our APIs using JSON however there is a lot which needs to be filtered out.
According to our API documentation, we have an "IncludeFields" query. How can I make it so when the information is requested, only information with "CS505MA001" is returned instead of the 100s of objects I'm getting.
$CommcellInfo = Invoke-RestMethod -Method Get -Uri $URL -Headers #{ "X- Auth-Token" = $Global:AuthToken; "includeFields" = "CS505MA001" }
$Include_Query = #{"includeFields"="CS505MA001"}
$ConvertedInclude_Query = ConvertTo-Json $Include_Query
$CommcellInfo = Invoke-RestMethod -Method Get -Uri $URL -Headers #{ "X-Auth-Token" = $Global:AuthToken }
I am trying to only show the returned information which contains certain text, how would I go about achieving this?

Related

How to put a hyperlink in a MS Teams message sent from a Powershell script in a TeamCity build project?

I'm using TeamCity to build my project.
In one of my build steps, I put a Powershell script, which uses Webhook to send a message to a MS Teams channel.
$url = "https://..."
$body = #{
title = "MtTitle";
text = "Visit: $url";
} | ConvertTo-Json
$postBody = [Text.Encoding]::UTF8.GetByres($body)
Invoke-WebRequest -Method Post -Uri "https://mycorp.webhook.office.com/..." -Body $postBody -ContentType "application/json" -UseBasicParsing
As a result of the script above, a message is sent to the Teams channel as expected, but the URL (the string after Visit:) is shown as a plain text.
How is it possible to make it a clickable hyperlink?
Should I use a MessageCard as shown in the link below?
Get Build Job URL in TeamCity Build Step
You can create a link in teams like that:
[HereText](hereLink)
Code would look somthing like that:
$url = "https://..."
$urlText = "some"
$body = #{
title = "MtTitle";
text = "Visit [$urlText]($url)";
} | ConvertTo-Json
Invoke-WebRequest -Method Post -Uri "https://mycorp.webhook.office.com/..." -Body $postBody -ContentType "application/json" -UseBasicParsing

Access members of an object one by one

in Powershell I have an object from query response, and it contains multiple members. I have to add some strings before and after every member of the object, and then send the produced json as query to create similar rules for different service. What happens is that $l_rules_json = $l_rules | ConvertTo-Jsondoes not access single member of the object but all members. How do I access members one by one in the cycle?
$l_rules |
ForEach-Object {
write-host("Output from rules objects in a cycle")
$l_rules_json = $l_rules | ConvertTo-Json
$l_rules_json_ext = "'{ `"rule`": { $l_rules_json }'"
write-host("Actual json BODY:")
write-host($l_rules_json_ext)
$response_t = Invoke-Restmethod -Uri $api_uri_t -Method POST -Headers $headers -ContentType 'application/json' -UseBasicParsing -Body $l_rules_json_ext
}

SurveyMonkey creating a collector in Powershell returning "The body provided was not a proper JSON string

I'm using Powershell to automate a survey creation, adding a collector, and sending the survey invitation - a pretty common use case. I was able to create a survey but I'm running into an issue in creating the collector. I created a collector via Postman but I get the error "1001 - The body provided was not a property JSON string" when using Powershell. I've tried everything I can think of but continue to get the error no matter how I create the JSON. Here is the code I'm using:
$SurveyCollector = [PSCustomObject]#{ type = 'email' }
$BodyText = $SurveyCollector | Select-Object -Property type |ConvertTo-Json -Depth 100 -Compress
$Results = Invoke-RestMethod -Uri $uri -StatusCodeVariable "ExtIDscv" -SkipHttpErrorChe -Method
Post -ContentType "application/json" -Authentication Bearer -Token $SecureToken -body -$BodyText
I'm sure I'm missing something simple. I'd appreciate any help.
The answer was more simple than what tripleee suggested. I had introduced a -$BodyText into the call and didn't catch it. I removed it and it works just fine.
$Results = Invoke-RestMethod -Uri $uri -StatusCodeVariable "ExtIDscv" -SkipHttpErrorChe -Method
Post -ContentType "application/json" -Authentication Bearer -Token $SecureToken -body -$BodyText

How do I fix a "cannot bind parameter 'uri'. Cannot convert the...." when converting json in powershell?

I am doing a Get-Weather project in powershell, where I pull data down from weatherapi.com . I am able to successfully connect to the website using an API key but, when I try to convert it from json in the script it doesn't work. The error I get is:
"Cannot bind parameter 'Uri'. Cannot convert the..."
I have tried so many different ways to write this:
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $url
The request for the website is:
$url = Invoke-WebRequest "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
Any help would be very much apperciated, thank you!
The input of the ConvertFrom-Json cmdlet is a JSON object. Look at the document below for more information
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/convertfrom-json?view=powershell-7.1#description
$url = "http://api.weatherapi.com/v1/forecast.json?key=$key&q=$location&days=$Days"
$response = Invoke-RestMethod -uri $url -Method Get -ResponseHeadersVariable r -StatusCodeVariable s
$weatherobject = ConvertFrom-Json $response

Only see ' rows=System.Object[] ' when enumerating JSON results in Powershell

I am returning a web service call that supposedly comes back in json format, and it does indeed appear to be valid json. As a string, it looks like this:
{"total":10,"rows":[{"process":"VISIO.EXE","computer_id":57,"last_used":"2016-04-20T01:34:09Z"},{"process":"VISIO.EXE","computer_id":64,"last_used":"2016-04-01T04:09:35Z"},{"process":"VISIO.EXE","computer_id":181,"last_used":"2016-03-10T23:02:53Z"},{"process":"VISIO.EXE","computer_id":230,"last_used":"2016-04-19T05:31:32Z"},{"process":"VISIO.EXE","computer_id":237,"last_used":"2016-04-04T10:23:23Z"},{"process":"VISIO.EXE","computer_id":284,"last_used":"2016-04-15T10:54:29Z"},{"process":"VISIO.EXE","computer_id":8401,"last_used":"2016-05-12T21:55:39Z"},{"process":"VISIO.EXE","computer_id":9045,"last_used":"2016-05-12T08:10:40Z"},{"process":"VISIO.EXE","computer_id":9527,"last_used":"2016-05-11T00:49:11Z"},{"process":"VISIO.EXE","computer_id":10198,"last_used":"2016-05-06T06:59:29Z"}]}
I am trying to enumerate through the results, using the following script. I have tried all of the listed options 1-4 by uncommenting them out one at a time, but I cannot get more than one result to return.
Any ideas as to what I'm doing wrong?
$url = 'https://servername:port/api/sam/raw_app_usage_property_values?criteria={"and":[["process","=","visio.exe"]]}&limit=100&columns[]=process&columns[]=computer_id&columns[]=last_used'
# option 1. get all results, we see a full list of processes, full string returned
#$results = Invoke-WebRequest -Uri $url -Method GET -Headers $headers
#write-host $results
# option 2. get all results but break them down as part of the request. Only one result returned - #{total=10; rows=System.Object[]}
#$results = Invoke-WebRequest -Uri $url -Method GET -Headers $headers | ConvertFrom-Json
#write-host $results
# option 3. use a different method, which supposedly breaks down the request natively. Only one result returned - #{total=10; rows=System.Object[]}
#$results = Invoke-RestMethod -Uri $url -Method GET -Headers $headers
#write-host $results
# option 4. get content directly from file, only one result returned - #{total=10; rows=System.Object[]}
#$results = (Get-Content -path "C:\temp\raw_app_usage_property_values.json" -raw)
# is there anything in the array?
foreach ($result in $results) {
write-host $result
}
You seem to be getting the data back. Remember that the returned object has two properties, "total" and "rows".
Try:
foreach ($result in $results.rows) {
write-host $result
}