[英]Move flag from one script to another in PowerShell
I have 2 powershell scripts script1.ps1
and script2.ps1
. 我有2个powershell脚本script1.ps1
和script2.ps1
。 They both contain the same parameter flag and integer parameter [int]$num, [switch]$silent = $false
. 它们都包含相同的参数标志和整数参数[int]$num, [switch]$silent = $false
。
Now I call script.ps1
with parameters script.ps1 222 -silent
. 现在,我使用参数script.ps1 222 -silent
调用script.ps1
。 I want to call script2.ps1
from script1.ps1
with that flag and another integer parameter. 我想使用该标志和另一个整数参数从script1.ps1
调用script2.ps1
。 But the only decision I found is 但是我发现的唯一决定是
param([int]$num, [switch]$silent = $false)
if($silent) {
.\script2.ps1 333 -silent
} else {
.\script2.ps1 333
}
Is there more brief and convenient way to send parameter flag from one script to another ? 有没有更简单,方便的方法将参数标志从一个脚本发送到另一个脚本?
Switch parameters accept boolean values, so you could do something like this: 开关参数接受布尔值,因此您可以执行以下操作:
Param(
[int]$num,
[Switch]$silent = $false
)
.\script2.ps1 333 -silent:$silent.IsPresent
Note that this doesn't behave exactly like a passthru. 请注意,这并不完全像passthru。 The second script in this case will always be passed the parameter -silent
, just with an explicit value of $true
or `$false. 在这种情况下,第二个脚本将始终传递参数-silent
,只是其显式值为$true
或`$ false。
If you want to not pass the parameter when your first script wasn't called with it you can't get around actually making that distinction. 如果您不希望在未调用第一个脚本时不传递参数,则无法绕开该区别。 In that case you need something like this: 在这种情况下,您需要以下内容:
Param(
[int]$num,
[Switch]$silent = $false
)
$extraParams = @{}
if ($PSBoundParameters.ContainsKey('silent')) {
$extraParams['silent'] = $silent.IsPresent
}
.\script2.ps1 333 @extraParams
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.