通过中间脚本传递命名参数

时间:2014-03-07 15:09:39

标签: powershell parameters scripting

我有两个脚本:

Script1.ps1:

param(
[string]$WhoCares = "",
[string]$PassThrough = ""
)

#do a whole bunch of random stuff here...
.\Script2.ps1 $PassThrough

Script2.ps1:

param(
[string]$FirstParameter = "",
[string]$SecondParameter = ""
)

Write-Host "First Parameter is: " $FirstParameter "Second Parmeter is: " $SecondParameter

我想象的是:

Script1.ps1 -WhoCare one -Passthrough“-FirstParameter Test -SecondParameter Test1”

然后看到:

第一个参数是测试第二个参数是Test1

但我所看到的是

第一个参数是-FirstParameter Test -SecondParameter Test1第二个参数是

,我想发送给script2的参数是以字符串形式出现的。如何通过中间脚本传递参数

我不想修改Script1.ps1以包含所有可能的参数,因为我使用Script1来设置环境,记录等等。

1 个答案:

答案 0 :(得分:1)

我将更改Script1以将Passthrough参数展开到Script2:

param(
[string]$WhoCares = "",
[hashtable]$PassThrough = @{}
)

#do a whole bunch of random stuff here...
.\Script2.ps1 @PassThrough

然后将Passthrough参数作为哈希表传递给Script1:

$Passthru =
 @{
   FirstParameter = 'Test'
   SecondParameter = 'Test1'
  }

  Script1.ps1 -WhoCares One -Passthrough $Passthru
相关问题