Home > Net >  How to run a powershell script passing a parameter to another script in a remote server?
How to run a powershell script passing a parameter to another script in a remote server?

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 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?

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