Reputation: 1144
I have 2 powershell scripts script1.ps1
and script2.ps1
. They both contain the same parameter flag and integer parameter [int]$num, [switch]$silent = $false
.
Now I call script.ps1
with parameters script.ps1 222 -silent
. I want to call script2.ps1
from script1.ps1
with that flag and another integer parameter. 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 ?
Upvotes: 0
Views: 36
Reputation: 200193
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. The second script in this case will always be passed the parameter -silent
, just with an explicit value of $true
or `$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
Upvotes: 2