0

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.

medinibster
  • 57
  • 2
  • 11
  • 1
    Does this answer your question? [Powershell: passing parameters to a job](https://stackoverflow.com/questions/4472284/powershell-passing-parameters-to-a-job), See [splatting](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting) – iRon Aug 22 '20 at 07:19
  • in the given example, the script is being called inside the script-block like this: start-job -scriptblock { & 'c:\ps\bcpCopy.ps1' @args } -ArgumentList $ARGS in my case, scriptblock is defined outside the scriptblock: $deployToBaseBlock = get-command '.\Base-script.ps1' | Select-Object -ExpandProperty ScriptBlock so this doesnt seem to work: Invoke-Command S1 -ScriptBlock {$deployToBaseBlock @args} -ArgumentList $arr, $arr being array of arguments – medinibster Aug 22 '20 at 11:33
  • 1
    Please do not add code in a comment because that becomes unreadable. [edit](https://stackoverflow.com/posts/63533452/edit) you rquestion and paste it there as [Formatted](https://meta.stackexchange.com/questions/184108/what-is-syntax-highlighting-and-how-does-it-work) text. – Theo Aug 22 '20 at 13:21

1 Answers1

1

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
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206