将参数传递给远程脚本块
Passing parameters to a remote scriptblock
假设我有 2 个脚本。 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"
现在,随着 base-script.ps1 中参数的增长,argumentlist 中传递的参数变得越来越长且难以管理。有一个更好的方法吗。我没能在这方面进行激烈的工作。
根据第一条评论,我们需要尝试这样的事情:
start-job -scriptblock { & 'c:\ps\bcpCopy.ps1' @args } -ArgumentList $ARGS
但就我而言,脚本块是在外部定义的。
$deployToBaseBlock = get-command '.\Base-script.ps1' | Select-Object -ExpandProperty ScriptBlock
Invoke-Command S1 -ScriptBlock {$deployToBaseBlock @args} -ArgumentList $arr
$arr 是参数数组。这似乎不起作用。请提出建议。
无论如何,远程端点都需要 re-create 和 re-compile 脚本块,因此您不妨将原始 Base-Script.ps1
文件[=18] 传递给它=].
幸运的是 Invoke-Command
已经有一个用于这个确切用例的参数:
Invoke-Command -FilePath '.\Base-script.ps1' -ComputerName S1 -ArgumentList $arr
假设我有 2 个脚本。 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"
现在,随着 base-script.ps1 中参数的增长,argumentlist 中传递的参数变得越来越长且难以管理。有一个更好的方法吗。我没能在这方面进行激烈的工作。
根据第一条评论,我们需要尝试这样的事情:
start-job -scriptblock { & 'c:\ps\bcpCopy.ps1' @args } -ArgumentList $ARGS
但就我而言,脚本块是在外部定义的。
$deployToBaseBlock = get-command '.\Base-script.ps1' | Select-Object -ExpandProperty ScriptBlock
Invoke-Command S1 -ScriptBlock {$deployToBaseBlock @args} -ArgumentList $arr
$arr 是参数数组。这似乎不起作用。请提出建议。
无论如何,远程端点都需要 re-create 和 re-compile 脚本块,因此您不妨将原始 Base-Script.ps1
文件[=18] 传递给它=].
幸运的是 Invoke-Command
已经有一个用于这个确切用例的参数:
Invoke-Command -FilePath '.\Base-script.ps1' -ComputerName S1 -ArgumentList $arr