I have two PowerShell scripts and use them on Linux with PowerShell Core, and want to pass the switch parameter from one.ps1 to second.ps1, but the $args is populated as a string with True and False sub-strings in it, which is not accepted by the second.ps1, as it has validation in param block.
How do I change to make it work? I know I can manually enumerate all the parameters of second.ps1 and make up a command argument string, but considering there are so many parameters in second.ps1, is there a better way ?
My scripts look like this:
# one.ps1
param(
[string][ValidateSet("value1", "value2")]$para1,
[switch]$para3 = $false
)
Write-Host "one.ps1: para1=$para1"
Write-Host "one.ps1: para3=$para3"
Write-Host "args=$args"
pwsh ./second.ps1 $args
second.ps1
# second.ps1
param(
[string][ValidateSet("value1", "value2")]$para1="value1",
[string][ValidateSet("value3", "value4")]$para2="value3",
[switch]$para3 = $false,
[switch]$para4 = $false,
[switch]$para5 = $false,
[switch]$para6 = $false,
[switch]$para7 = $false,
[switch]$para8 = $false
)
Write-Host "second.ps1: para1=$para1"
Write-Host "second.ps1: para2=$para2"
Write-Host "second.ps1: para3=$para3"
Write-Host "second.ps1: para4=$para4"
Write-Host "second.ps1: para5=$para5"
Write-Host "second.ps1: para6=$para6"
Write-Host "second.ps1: para7=$para7"
Write-Host "second.ps1: para8=$para8"
Here is the command to use them
pwsh one.ps1 -para1 value1 -para2 value3 -para3:true -para4:false
Error message:
one.ps1: para1=value1
one.ps1: para3=True
args=-para2 value3 -para4 False
second.ps1: Cannot validate argument on parameter 'para1'. The argument "False" does not belong to the set "value1,value2" specified by the ValidateSet attribute. Supply an argument that is in the set and then try the command again.