Webscraping Local web application, Trying to update the JSON object with Powershell - json

I am working on a project. I have a JSON object that has been pulled from a web scrape. The web is a html version of something I am working on to see current movies and if it is added to my watchedlist for IMDB. :
{
"title": "The Gallows Act II",
"alternativeTitles": [],
"secondaryYearSourceId": 0,
"sortTitle": "gallows act ii",
"status": "released",
"overview": "When Ana Rue transfers to a prestigious
new acting school, she encounters a malevolent spirit after participating in a viral challenge.",
"inCinemas": "2019-10-25T05:00:00Z",
"physicalRelease": "2019-10-25T00:00:00Z",
"physicalReleaseNote": "",
"images": [
{
"coverType": "poster",
"url": "/MediaCover/54956/poster.jpg"
},
{
"coverType": "fanart",
"url": "/MediaCover/54956/fanart.jpg"
}
],
"website": "",
"year": 2019,
"studio": "Tremendum Pictures",
"profileId": 1,
"pathState": "dynamic",
"monitored": true,
"minimumAvailability": "inCinemas",
"isAvailable": true,
"runtime": 99,
"lastInfoSync": "2020-01-28T16:40:11.982967Z",
"cleanTitle": "thegallowsactii",
"imdbId": "tt7473716",
"tmdbId": 505060,
"titleSlug": "the-gallows-act-ii-505060",
"genres": [],
"tags": [],
"ratings": {
"votes": 12,
"value": 5.3
},
$address="http://localhost:PORT"
$movie_id = ""
#$movie_title = Read-Host "Movie Title"
$params = #{"name"="RescanMovie";"movieId"="$movie_id";} | ConvertTo-Json
$apikey = 'APIKEY FOR APPLICATION'
$converted = Invoke-WebRequest -URI "$address/api/movie/" -Method GET/(10) -UseBasicParsing -Header #{"X-Api-Key" = $apikey}
$converted
#$convertto = Invoke-WebRequest -URI "$address/api/movie/" -Method PUT -UseBasicParsing -Header #{"X-Api-Key" = $apikey}
Now I pull this information with API pull : Invoke-Webrequest.
I get the objects.
I want to change the "Monitored" if it is on the list I want to change it to false or true respectively.
I break the object down to initial search "Monitored" is -eq true.
It pulls them in.
I want to pull the JSON information for all fields" update the Monitored status to true or false if its found in the list.
-Then pass all fields back to the json object and updated it.
However. I am not sure how to perform the POST command in powershell to update the whole list passing it back.

Related

Convert Azure DevOps rest api test plans for all projects to csv file using powershell

I am trying to generate report for all the test plans available in azure DevOps organization. here is my script which generates list of projects and the iterate through every project to find test plans available in it. I want all this data to be saved in csv file. I am able to get json file. Is there any way I can get this data saved in csv file with every project iterate for test plan ?
$connectionToken = ""
$BaseUrl = "https://dev.azure.com/{organization_name}/_apis/projects?
api-versions=5.0"
$base64AuthInfo=
[System.Convert]::ToBase64String([System.Text.Encoding]
'::ASCII.GetBytes(":$($connectionToken)"))
$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers
#{authorization = "Basic $base64AuthInfo"} -
Method Get
$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100
$Projectname = $ProjectInfo.value.name
ForEach ($project in $Projectname){
$TestPlanApi = "https://dev.azure.com/{org}/$project/_apis/test/plans?
api-version=5.0"
$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers
#{authorization = "Basic $base64AuthInfo"} -Method Get
if (-NOT ($TestplanInfo.count -eq 0)){
$info = $TestplanInfo | ConvertTo-Json -Depth 100
$info
}
}
This gives me following json file , I want to convert in to csv. Due to every project test plans value starts the Json result with value I am not able to expand it and save to csv
{
"value": [
{
"id": 134,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url":
"https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-
Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl": "mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
}
],
"count": 1
}
{
"value": [
{
"id": 567,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl":"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
},
{
"id": 678,
"name": "sprint1",
"url": "https://dev.azure.com/fabrikam/fabrikam-fiber-
tfvc/_apis/test/Plans/1",
"project": {
"id": "eb6e4656-77fc-42a1-9181-4c6d8e9da5d1",
"name": "Fabrikam-Fiber-TFVC",
"url": "https://dev.azure.com/fabrikam/_apis/projects/Fabrikam-Fiber-
TFVC"
},
"area": {
"id": "343",
"name": "Fabrikam-Fiber-TFVC"
},
"iteration": "Fabrikam-Fiber-TFVC\\Release 1\\Sprint 1",
"state": "Active",
"rootSuite": {
"id": "1"
},
"clientUrl":
"mtms://fabrikam.visualstudio.com:443/DefaultCollection/p:Fabrikam-
Fiber-TFVC/Testing/testplan/connect?id=1"
}
],
"count": 2
}
These are the values for different projects test runs, some projects have count = 1 then it shows one id , some projects has count =3 then it shows all the 3 ids and so on
I want this json file in csv file with columns -
id , name , url , project.name , project.id , project.url ,area.id , area.name , iteration , owner , revision , state , rootsuite.id , clienturl
How can I expand all the values in csv file ? I tried
Select-object Expand-property value but its fails to to expand all the values in json data
As a workaround, we can save the response body and then convert the json file to csv file.
We cannot save all info in one csv file, the latest info will overwrite the old data, so we need to save the test plan info in different csv files.
Sample:
$connectionToken = "{PAT}"
$BaseUrl = "https://dev.azure.com/{Org}/_apis/projects?api-version=6.1-preview.4"
$base64AuthInfo= [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes(":$($connectionToken)"))
$ProjectInfo = Invoke-RestMethod -Uri $BaseUrl -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
$ProjectDetails = $ProjectInfo | ConvertTo-Json -Depth 100
$Projectname = $ProjectInfo.value.name
ForEach ($project in $Projectname){
$TestPlanApi = "https://dev.azure.com/{Org}/$project/_apis/test/plans?api-version=5.0"
$TestplanInfo = Invoke-RestMethod -Uri $TestPlanApi -Headers #{authorization = "Basic $base64AuthInfo"} -Method Get
if (-NOT ($TestplanInfo.count -eq 0)){
#Save the test plan info to json file
$info = $TestplanInfo | ConvertTo-Json -Depth 100 | out-file E:\test\$project.json
#convert the json file to csv file
Get-Content -Path E:\test\$project.json | ConvertFrom-Json | Select-Object -expand value | ConvertTo-Csv -NoTypeInformation | Set-Content E:\test\$project.csv
#delete json file
Remove-Item E:\test\$project.json
}
}
Result:

Adding array objects to a JSON with a property name in powershell

I've to retrieve products and it's details and save it together in a json file. Unfortunately the name and details cannot be retrieved from the same API, I gotta use 2 different API. So I run a code inside a for loop to retrieve the list of product names as well as its details as follows:
For($i=0; $i -lt $cart.total; $i++)
{
$query = 'dummy value with the cart number'
$productName = Invoke-RestMethod -Uri 'https://abc' -Body $query -Method Post
$details = Invoke-RestMethod -Uri 'https://xyz' -Body $query -Method Post
}
All this works, and below is sample data in $productname and $details.
$productName
"iPhone"
$details
"data":[
{"Moduleid": "1", "propertyName": "camera", "value": "12MP"},
{"Moduleid" : "43", "propertyName":"battery", "value": "4000MAj"}
]
However what I am trying to do is to save the $productName and $details into a json as follows:
{
"iPhone 7":[
{"Moduleid": "1", "propertyName": "camera", "value": "12MP"},
{"Moduleid" : "423", "propertyName":"battery", "value": "3000MAh"}
],
"S10+": [
{"Moduleid": "21", "propertyName": "camera", "value": "12MP"},
{"Moduleid" : "43", "propertyName":"battery", "value": "4000MAh"}
]
}
I've tried several ways but couldn't reach any conclusion. Please let me know how can these to variable datas be put into a json format as shown above.
Thank you
The question doesn't make clear what each query is returning. Generally speaking, you need to convert the return JSON from both queries into objects then merge the objects into the objects you really want. The run those objects through ConvertTo-Json which should do the trick. The source object structure is what results in the JSON text etc...
It would look something like below, but again, I have to guess at the starting point, for example I'm sure that $productName isn't a flat array, but is itself JSON.
# For test
$productName = "iPhone","S10+"
# For test
$details =
#"
{
"Data": [{
"Moduleid": "1",
"propertyName": "camera",
"value": "12MP"
},
{
"Moduleid" : "43",
"propertyName": "battery",
"value": "4000MAj"
}]
}
"# |
ConvertFrom-Json
$Results =
For( $i = 0; $i -lt $productName.count; ++$i )
{
[PSCustomObject]#{
$productName[$i] = $details.data[$i]
}
}
$Results | ConvertTo-Json
This would give back:
[
{
"iPhone": {
"Moduleid": "1",
"propertyName": "camera",
"value": "12MP"
}
},
{
"S10+": {
"Moduleid": "43",
"propertyName": "battery",
"value": "4000MAj"
}
}
]
Not quite what you're looking for, but should at least demonstrate the point.
Note: Normally I would look for a common property between the 2
returns to relate the data. Absent that I used the array index. Again
part of the reason this doesn't look right.
If you can please edit the question to include the return JSON from both queries, then we can refine the above.
I created a hashtable like:
$hashTable = [HashTable]::New(0, [StringComparer]::Ordinal)
This is so that the keys are case sensitive. Normally if you create it as #{}, it won't be case sensitive.
Then i mapped the key and value to the hash table as follows,
$hashTable.Add($productName.data.name,$details.data)
Now, I convert it to Json as I wanted it using ConvertTo-Json
$hashTable| ConvertTo-Json | Out-File $outputPath

How can I parse nested JSON in PowerShell?

I'm trying to parse the results of a cURL command and the information I need is in a structure.
I tried getting to the data unsuccessfully and tried converting to PS Object but not sure how to access the structure as I'm new to PS.
Below is a sample of our cURL response.
I have a git commit hash ('c64a568399a572e82c223d55cb650b87ea1c22b8' matches latestCommit in fromRef for entry id 1101) and I need to find the corresponding displayId ('develop' in toRef)
I've done this in Linux using jq but need to replicate this in PS.
jq '.values | map(select(.fromRef.latestCommit=="'"$HASH"'")) | .[0].toRef.displayId'
I'm having 2 issues.
I can get to fromRef but it looks like #{id=refs/heads/feature/add-support; displayId=feature/add-support; latestCommit=c64a568399a572e82c223d55cb650b87ea1c22b8; repository=} and I cannot figure out how to parse
I'm not sure how to get the id so I can find the correct corresponding toRef
Any help would be greatly appreciated.
{
"size": 15,
"limit": 20,
"isLastPage": true,
"values": [
{
"id": 1101,
"version": 0,
"title": "Added header",
"description": "Added notes in header",
"state": "OPEN",
"open": true,
"closed": false,
"createdDate": 1595161367863,
"updatedDate": 1595161367863,
"fromRef": "#{id=refs/heads/feature/add-support; displayId=feature/add-support; latestCommit=c64a568399a572e82c223d55cb650b87ea1c22b8; repository=}",
"toRef": "#{id=refs/heads/develop; displayId=develop; latestCommit=58b3e3482bb35f3a735048849c2474cc676fbd9b; repository=}",
"locked": false,
"author": "#{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
"reviewers": " ",
"participants": "",
"properties": "#{mergeResult=; resolvedTaskCount=0; openTaskCount=0}",
"links": "#{self=System.Object[]}"
},
{
"id": 1053,
"version": 4,
"title": "Help with checking,",
"description": "fixed up code.",
"state": "OPEN",
"open": true,
"closed": false,
"createdDate": 1591826401310,
"updatedDate": 1595018917357,
"fromRef": "#{id=refs/heads/bugfix/checking-2.7; displayId=bugfix/checking-2.7; latestCommit=cf7d8860262c6a46b0b65ef5b6d66ae8cd698b75; repository=}",
"toRef": "#{id=refs/heads/hotfix/2.7_Improvements; displayId=hotfix/2.7_Improvements; latestCommit=01f1100c559ba41ec317421399c3bfb9a0aea91f; repository=}",
"locked": false,
"author": "#{user=; role=AUTHOR; approved=False; status=UNAPPROVED}",
"reviewers": " ",
"participants": "",
"properties": "#{mergeResult=; resolvedTaskCount=0; commentCount=4; openTaskCount=0}",
"links": "#{self=System.Object[]}"
}
],
"start": 0
}
Once you have converted the result with ConvertTo-Json and the correct -Depth parameter, you can get the values of the returned object quite easily in PowerShell.
Let's say you have used something like $json = $curlResult | ConvertTo-Json -Depth 100, then finding the displayId from the corresponding toRef can be done like this:
# this is the known hashvalue of the `fromRef` value to look for
$latestCommitHash = "c64a568399a572e82c223d55cb650b87ea1c22b8"
# get the value item. from here you can get all other properties belonging to that item
$valueItem = $json.values | Where-Object { $_.fromRef.latestCommit -eq $latestCommitHash }
# get the displayId value of the corresponding 'toRef' element:
$displayId = $valueItem.toRef.displayId
Returns
develop

"The validation of the PDF file failed." when trying to send signing request via email - docusign

****EDIT: Thanks for those links, #Hackerman. I was able to get it working in Postman and then in Powershell using the Docusign postman sample. However I cannot get it working with my own PDF documents. I suspect this is because I am not properly converting my PDFs to Base64. Anyone know how this can be done via Powershell?****
EDIT2: Was able to encode my PDFs to Base64 via powershell with this simple 1-liner
$docEncodedBase64 = [Convert]::ToBase64String((Get-Content $PDFPath -Encoding Byte))
We are trying to use Powershell 5.0 (and the invoke-restmethod cmdlet) to make requests to the docusign REST API in order to send a signing request via email.
In my tests I have been following this guide: https://docs.docusign.com/esign/guide/usage/request_a_signature.html#prepare-the-document-to-send-through-docusign but am getting an error when I send my POST request
We are going the route of a normal request (i.e., not multi-part request), and thus supplying the PDF document as bytes in base64-encoded format as the value of the documentBase64 property.
Here is my code to convert the PDF to base64 bytes:
# PDF document
$docContent = Get-Content 'Mutual_NDA.pdf'
# PDF as bytes
$docBytes = [System.Text.Encoding]::Unicode.GetBytes($docContent)
# PDF as Base-64 Encoded bytes
$docEncoded = [System.Convert]::ToBase64String($docBytes)
Then I define my JSON payload, which will be sent as the body in the POST request. In here I set the 'documentBase64' property to the base64 encoded string that I just converted above.
# JSON payload
$jsonPayload = #"
{
"documents": [
{
**"documentBase64": "$docEncoded"**,
"documentId": "1",
"fileExtension": "pdf",
"name": "Mutual_NDA.pdf"
}
],
"emailSubject": "Please sign the NDA",
"recipients": {
"signers": [
{
"email": "the_nda_signer#mailinator.com",
"name": "Tester test",
"recipientId": "1",
"routingOrder": "1",
"tabs": {
"dateSignedTabs": [
{
"anchorString": "signer1date",
"anchorYOffset": "-6",
"fontSize": "Size12",
"name": "Date Signed",
"recipientId": "1",
"tabLabel": "date_signed"
}
],
"fullNameTabs": [
{
"anchorString": "signer1name",
"anchorYOffset": "-6",
"fontSize": "Size12",
"name": "Full Name",
"recipientId": "1",
"tabLabel": "Full Name"
}
],
"signHereTabs": [
{
"anchorString": "signer1sig",
"anchorUnits": "mms",
"anchorXOffset": "0",
"anchorYOffset": "0",
"name": "Please sign here",
"optional": "false",
"recipientId": "1",
"scaleValue": 1,
"tabLabel": "signer1sig"
}
]
}
}
]
},
"status": "sent"
}
"#
And lastly, the HTTP request:
$Envelope = Invoke-RestMethod -uri ($BaseURL + '/envelopes') -Method Post -Body $jsonPayload -ContentType 'application/json' -Headers #{"X-Docusign-Authentication" = $XMLHeader}
Anyone have experience with this? Perhaps I am encoding the PDF as base64 incorrectly? I am truly stuck. Any help appreciated!
Thanks,
Eric
My PDF was not properly converting to Base64. Using this line of code in Powershell I was able to successfully create and send an envelope
$docEncodedBase64 = [Convert]::ToBase64String((Get-Content $PDFPath -Encoding Byte))
Thanks to #Hackerman for referencing Postman!

Zabbix Json Api get health of host or hostgroup

I am currently trying out Zabbix 2.4 json api and can connect get alerts and triggers but I'm having trouble finding a way to check if a given hostgroup contains no open errors and is thereby healthy. A simple way to check this doenst seem to exist? I have searched stackoverflow, google and Zabbix documentation. any clues as to tackeling this question? The following reuqest gives me errors but no way to tell if errors are still open.
{
"jsonrpc": "2.0",
"method": "alert.get",
"params": {
"output": "extend",
"groupids": "83",
"time_from": "1441065600"
},
"auth": "ZZZZZZZZZZZZZZZZZZZZZZZZZ",
"id": 1
}
Update: (solved)
$resource = "http://zabbix/api_jsonrpc.php"
$body = '
{
"jsonrpc": "2.0",
"method": "trigger.get",
"params": {
"active": 1,
"only_true": "1",
"monitord": 1,
"selectGroups": 1,
"output": "extend",
"expandDescription": "1",
"selectGroups": "1",
"groupids": 83, <---- this field changes :)
"filter": {
"value": 1,
"priority": [3,4,5] <----- filter off low prios
},
"sortfield": "priority",
"sortorder": "DESC"
},
"auth": "ZZZZZZZZZZZZZZZZZZZZZZZZZ",
"id": 1
}
'
$result = Invoke-RestMethod -Method Post -Uri $resource -Body $body -ContentType "application/json"
$result.result
Use active triggers of host/group. Some my code, which I use:
zapi.trigger.get(only_true=1,
skipDependent=1,
monitored=1,
active=1,
output='extend',
expandDescription=1,
selectGroups=1,
expandData='host',
hostids=<host_id>,
sortfield='priority',
sortorder='DESC',
filter={"value": 1, "priority": ['4','5']}
)
https://www.zabbix.com/documentation/2.4/manual/api/reference/trigger/get