Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将命名参数从一个powershell脚本传递到另一个powershell脚本_Powershell - Fatal编程技术网

将命名参数从一个powershell脚本传递到另一个powershell脚本

将命名参数从一个powershell脚本传递到另一个powershell脚本,powershell,Powershell,在这种情况下,我有一个脚本_1.ps1,它直接被用户调用。Script_1.ps1实际上只是Script_2.ps1的一个入口点(至少只提供一个硬编码的配置值),它包含共享的分解逻辑。我希望用户能够将任何或所有必需的参数传递给script_1,而script_1又必须传递给script_2。如果用户没有传递所有必需的参数,则脚本2中的逻辑将提示用户输入信息 在我的理想设置中,我会让script_1接受来自用户的命名参数,然后script_2接受script_1的命名参数。例如,我的脚本_1.ps

在这种情况下,我有一个脚本_1.ps1,它直接被用户调用。Script_1.ps1实际上只是Script_2.ps1的一个入口点(至少只提供一个硬编码的配置值),它包含共享的分解逻辑。我希望用户能够将任何或所有必需的参数传递给script_1,而script_1又必须传递给script_2。如果用户没有传递所有必需的参数,则脚本2中的逻辑将提示用户输入信息

在我的理想设置中,我会让script_1接受来自用户的命名参数,然后script_2接受script_1的命名参数。例如,我的脚本_1.ps1如下所示:

param (
    [switch] $Help = $false,
    [switch] $Quiet,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    .\script_2.ps1 -ServiceName "Service name here" #also pass any other args that were set by user (may be none, several, or all)
}
param (    
    [switch] $Quiet,
    [Alias("l")]
    [string] $ServiceName,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

# do things with arguments here
然后,我的脚本_2.ps1将如下所示:

param (
    [switch] $Help = $false,
    [switch] $Quiet,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    .\script_2.ps1 -ServiceName "Service name here" #also pass any other args that were set by user (may be none, several, or all)
}
param (    
    [switch] $Quiet,
    [Alias("l")]
    [string] $ServiceName,
    [string] $Param1,
    [string] $Param2,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

# do things with arguments here
当我从script_1.ps1调用script_2.ps1时,是否可以在不枚举所有参数的情况下实现这一点?我的脚本_1有大约20个不同的可能参数,因此类似这样的东西很快就会变得混乱:

.\script_2.ps1 -ServiceName "Service name here" -Quiet $Quiet -Param1 $Param1 -Param2 $Param2 
我最接近实现这一点的方法是使用下面的代码,但是当它们使用脚本_2时,它会切断带有空格的参数。将转义引号放在$args周围不会导致传递任何参数

param (
    [switch] $Help = $false,
    [Parameter(Position=0, ValueFromRemainingArguments=$true)] $args
)

if (!$Help) {
    Start-Process -FilePath powershell.exe -ArgumentList "-file `".\script_2.ps1`" -ServiceName `"Service name here`" $args"
}
我确信我作为一个相对新加入powershell的人,错过了一个做这件事的窍门。。。谢谢你的帮助

您可以使用将参数集合从第一个脚本传递到第二个脚本。所有这一切意味着,不是将参数直接写入script2.ps1,而是传入一个键值对哈希表

幸运的是,有一个内置的哈希表,它已经包含了传递给脚本或函数的参数-
$PSBoundParameters
,因此您可以直接从
script1.ps1
扩展到
script2.ps1

请注意,您使用
@
符号调用“splat”,而不是
$
——即在本例中为
@PSBoundParameters

script.ps1

param(
[开关]$Help=$false,
[切换]$Quiet,
[字符串]$Param1,
[字符串]$Param2,
[参数(位置=0,ValueFromRemainingArguments=$true)]$args
)
如果(!$Help){
编写主机“script1.ps1”
写入主机($PSBoundParameters | ft | out字符串);
..\script2.ps1@PSBoundParameters
}
script2.ps1

param(
[切换]$Quiet,
[别名(“l”)]
[字符串]$ServiceName,
[字符串]$Param1,
[字符串]$Param2,
[参数(位置=0,ValueFromRemainingArguments=$true)]$args
)
#在这里用论点做事
编写主机“script2.ps1”
写入主机($PSBoundParameters | ft*|输出字符串)
如果您随后致电:

C:\> powershell .\script1.ps1 -Param1 "aaa" -Quiet:$true
您将获得以下输出:

script1.ps1

Key    Value
---    -----
Param1 aaa
Quiet  True

script2.ps1

Key    Value
---    -----
Param1 aaa
Quiet  True
如果您不喜欢内置的
$PSBoundParameters
哈希表,您可以构建自己的哈希表,并将其替换为:

$mySplat = @{
    "Quiet" = $true
    "Param1" = "myParam1"
}
$mySplat.Add("Param2", "myParam2")
$mySplat.Remove("Quiet");
. .\script2.ps1 @mySplat

您可以让第一个脚本输出一个自定义对象,该对象的属性与第二个脚本的一些参数名称相匹配。然后在第二个脚本中,使用
valuefrompipelinebypropertyname
参数属性。[1]
$args
变量是一个自动的$Var。不应将其用作参数名称。[2] 你看过
$PSBoundParameters
了吗?@Lee_Dailey$PSBoundParameters正是我想要的!当我在谷歌上搜索这个问题时,我真的很惊讶没有想到这一点。谢谢bunch@allison-不客气!很高兴能帮上一点忙。。。[咧嘴笑]