To pass a JSON text as a parameter to PowerShell, you can follow these steps:
Create a new file with a .json extension and save the JSON text in it.
In your PowerShell script, use the Get-Content cmdlet to read the JSON text file and store the contents in a variable:
$json = Get-Content -Path "C:\path\to\my\json\file.json" -Raw
Note: The -Raw parameter is used to read the entire contents of the file as a single string.
$obj = $json | ConvertFrom-Json
Alternatively, you can pass the JSON text as a string parameter directly to your script using single quotes:
.\myScript.ps1 -json '{ "name": "John", "age": 30 }'
In your script, you can retrieve the parameter value using the $args variable:
$json = $args[0]
$obj = $json | ConvertFrom-Json
Asked: 2022-12-25 11:00:00 +0000
Seen: 12 times
Last updated: Aug 13 '21