0

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 store and pass the accompanying LocationUUID as a parameter to another PowerShell script, and run the script in Stores 1 and 2.

$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 running the script on Store" + $computer + $location

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

Test script to be run in Stores 1 and 2 is like below:

param(
[Parameter(Mandatory = $true)] $LocationUUID
)
Write-host $LocationUUID
New-Item 'T:\test.txt' -type file

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

Can I get some help on this?

3
  • Use return : learn.microsoft.com/en-us/powershell/module/… Commented Feb 1, 2023 at 10:42
  • What is the exact error? Note that the value "ABC" in the example is not a valid Guid: & { param( [Parameter(Mandatory = $true)] [guid] $LocationUUID ) } 'ABC' --> Cannot process argument transformation on parameter 'LocationUUID'. Cannot convert value "ABC" to type "System.Guid". Error: "Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)." Commented Feb 1, 2023 at 10:59
  • Ah yes. I have removed the guid and put ABC just for this question. The actual content was a Guid. Mathias' answer works. Thank you. Commented Feb 1, 2023 at 11:36

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hi, yes it works in my local. But what if I want to run the script from a server, connecting to each store servers? @Mathias R. Jessen

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.