It does however work with the same caller script when the called script is NOT converted to an exe
capture error works with calling a script likewise
CODE CALLING SCRIPT
- $cmd = "c:\temp\test_param2.ps1"
- $ar=@(
- "-SearchNonDefaultRootDirs2",
- "True",
- "-DirectoryNamesOnly2",
- "True"
- )
- try{
- & $cmd @ar -ea Stop
- }
- catch{
- $errmsg=($_.Exception.Message).replace("`r`n","")
- write-output "ERROR: $errmsg"
- }
CODE CALLED SCRIPT
- param(
- [parameter(ParameterSetName = "SearchDirectories")]
- [ValidateSet('True', 'False')]
- [string]$WritableWindir='False',
- [parameter(ParameterSetName = "SearchDirectories")]
- [ValidateSet('True', 'False')]
- [string]$DirectoryNamesOnly='False',
- [parameter(ParameterSetName = "SearchDirectories")]
- [ValidateSet('True', 'False')]
- [string]$SearchNonDefaultRootDirs='False'
- )
- write-output "WritableWindir($WritableWindir)"
- write-output "DirectoryNamesOnly($DirectoryNamesOnly)"
- write-output "SearchNonDefaultRootDirs($SearchNonDefaultRootDirs)"
Output (as expected)
PS C:\temp> ./test_param2.ps1 -DirectoryNamesOnly2 $True
C:\temp\test_param2.ps1 : A parameter cannot be found that matches parameter name 'DirectoryNamesOnly2'.
At line:1 char:19
+ ./test_param2.ps1 -DirectoryNamesOnly2 $True
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [test_param2.ps1], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,test_param2.ps1[/size]
HOWEVER
The following seems NOT to work correctly?
Same caller script and same called script but this time the called script has been converted to an exe
exe outputs: (in a window when using build setting Windows Application)
Line 1: A parameter cannot be found that matches parameter name 'SearchNonDefaultRootDirs2'.
I tried various build settings for the exe but the error does not get captured in the caller
Is this expected behaviour or not? If it is not, what can be done to capture this error in the caller?