Home > Enterprise >  How to get contents from a json file and pass them as parameter to another script in Powershell?
How to get contents from a json file and pass them as parameter to another script in Powershell?

Time:02-02

I am trying to read a json file which is like below

[
{ "STORE": "1", "LocationUUID": "ABC" },
{ "STORE": "2", "LocationUUID": "DEF" }
]

and then run a for loop for each Stores and pass the accompanying LocationUUID as a parameter to another powershell script.

$storeLocationUUIDJSON = Get-Content '{my path here}'  -Raw | ConvertFrom-Json
$storeLocationUUIDJSON | Select-Object -Property STORE, LocationUUID |

ForEach-Object {
$computer = $_.STORE
$location = $_.LocationUUID
$filepath = 'test.ps1'
Write-Host "Started runnig the script on Store"   $computer   $location

try{
Invoke-Command -FilePath $filepath -LocationUUID $location
}
catch
{
Write-Host "Not successful on Store"   $computer
Write-Host $_.Exception.Message
}
}

Test is like below:

param(
[Parameter(Mandatory = $true)] [guid] $LocationUUID
)
Write-host $LocationUUID

Whenever I run this, I am getting the error A parameter cannot be found that matches parameter name 'LocationUUID'.

Can I get some help on this?

CodePudding user response:

It's complaining that Invoke-Command doesn't have a LocationUUID parameter.

Don't use Invoke-Command to run code locally, use the invocation operator & instead:

& $filepath -LocationUUID $location
  • Related