Hi, I want to get data:
$IP = Read-host “IP”
$loc = Read-host “Location”
$notes = Read-host “Notes”
Then I want to put the variables into a json file which I will use as input to an API. I have having trouble with accomplishing this, can someone help please? ThankS!
$json = @’
{
“address”: [“$ip”],
“worker”: “$loc”,
“notes”: “$notes”
}
'@ | out-file C:\temp\temp.jason
$IP = Read-host “IP”
$loc = Read-host “Location”
$notes = Read-host “Notes”
$json = @{“address”=$ip;
“worker”=$loc;
“notes”=$notes
}
$obj = New-Object -TypeName PSObject -Property $json
$obj | ConvertTo-Json | Out-File -FilePath C:\temp\temp.json
Alternative version which does not use Read-Host and New-Object:
Param (
[Parameter(Mandatory)]
$IPAddress,
[Parameter(Mandatory)]
$Location,
[Parameter(Mandatory)]
$Notes,
$OutputFilePath = 'C:\TEMP\My.json'
)
@{
"address" = $IPAddress
"worker" = $Location
"notes" = $Notes
} | ConvertTo-Json | Out-File -FilePath $OutputFilePath
Thanks all - the reason I had the brackets was that it is required by the API. This would be the required command line input if I were to just run it (I don’t need all of the variables listed though)
{“hosttype”:“server”, “address”: [“127.0.0.1”], “parents”: , “tag”: [“demo”], “umiworker”: “worker”, “notes”: “this is a test”, “url”: “http://www.tblahbah.com”}
obviously the brackets are for a list, I guess an array…
Thanks!
sorry, I made an error in my first post. The input file will be:
$json = @{“address”: [$ip];,
“worker” : $loc,
“tag” : [$tag]
“notes” : $notes
}
If I do this, I get an error:
At line:1 char:20
- $json = @{“address”: [$ip];,
-
~
Missing ‘=’ operator after key in hash literal.
can someone help, thanks!
You are converting Powershell to JSON. In Powershell, a hashtable is being generated with the data to convert to JSON. The format you provided is not a hashtable, which is why you are getting an error. If you just want to wrap a value in square brackets, you could do something like this:
$IPAddress = "10.10.10.10"
$Location = "Location A"
$tag = "test"
$notes = "Some notes"
$hashTable = @{
"address" = "[{0}]" -f $IPAddress;
"worker" = $Location;
"tag" = $tag;
"notes" = $Notes;
}
$hashTable | ConvertTo-JSON
Which would produce JSON like this:
{
"worker": "Location A",
"tag": "test",
"address": "[10.10.10.10]",
"notes": "Some notes"
}
You just need to make sure that Powershell is formatted as a hashtable. You can manipulate the column or the value, but not the format of the hashtable. Hopefully this will help.
I think you are looking for something like below:
Param (
[Parameter(Mandatory)]
[String[]]
$IPAddress,
[Parameter(Mandatory)]
[String]
$Worker,
[Parameter(Mandatory)]
[String[]]
$Tag,
[Parameter(Mandatory)]
[String]
$Notes,
$OutputFilePath = 'C:\TEMP\My.json'
)
[ordered]@{
"address" = $IPAddress
"worker" = $Worker
"tag" = $Tag
"notes" = $Notes
} | ConvertTo-Json
Input
IPAddress[0]: 10.0.0.1
IPAddress[1]: 10.0.0.2
IPAddress[2]:
Worker: Location A
Tag[0]: Self
Tag[1]: help
Tag[2]:
Notes: Important
JSON output
{
"address": [
"10.0.0.1",
"10.0.0.2"
],
"worker": "Location A",
"tag": [
"Self",
"help"
],
"notes": "Important"
}