medinibster
medinibster

Reputation: 57

Passing parameters to a remote scriptblock

Say i have 2 scripts. main-script.ps1 and base-script.ps1 and base-script being called from main-script.

#base-script.ps1
Param(
    [string]$Site,
    [string]$Env,
    [string]$Package,
    [bool]$New_Variable,
    [string]$New_Variable2
)
Write-Host "i am in base script"
Write-Host " i am in server " $env:COMPUTERNAME

Write-Host $Site
Write-Host $Env
Write-Host $Package
Write-Host $New_Variable
Write-Host $New_Variable2
#main-script.ps1
Param(
    [string]$SiteName,
    [string]$Environment,
    [string]$PackageName

)
Write-Host "I am in main-script"

$new_var = $true
$new_var2 = "Green"

$deployToBaseBlock = get-command '.\Base-script.ps1' | Select-Object -ExpandProperty ScriptBlock

Invoke-Command S1 -ScriptBlock $deployToBaseBlock -ArgumentList $SiteName,$Environment,$PackageName,$new_var,$new_var2

Write-Host "I am back in main. exiting"

Now as the parameters in base-script.ps1 grows, the arguments being passed in argumentlist is getting long and unmanageable. Is there a better way to do this. I wasnt able to get splatting work on this.

According to the first comment, we need to try something like this:

start-job -scriptblock { & 'c:\ps\bcpCopy.ps1' @args } -ArgumentList $ARGS

but in my case, scriptblock is defined outside.

$deployToBaseBlock = get-command '.\Base-script.ps1' | Select-Object -ExpandProperty ScriptBlock

Invoke-Command S1 -ScriptBlock {$deployToBaseBlock @args} -ArgumentList $arr

$arr is the array of arguments. This doesnt seem to work. Please suggest.

Upvotes: 0

Views: 718

Answers (1)

Mathias R. Jessen
Mathias R. Jessen

Reputation: 174900

The remote endpoint will need to re-create and re-compile the scriptblock anyway, so you might as well just pass it the raw Base-Script.ps1 file.

Fortunately Invoke-Command already has a parameter for this exact use case:

Invoke-Command -FilePath '.\Base-script.ps1' -ComputerName S1 -ArgumentList $arr

Upvotes: 1

Related Questions