Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

PSHELL and @PSHELL errorlevel values

Aug
2,134
92
Code:
c:\users\jlc\utils>ver

TCC  23.00.30 x64   Windows 7 [Version 6.1.7601]

Code:
@setlocal
@echo off
pshell /s "[ValidateRange(1,10)] [int]$x = 1"
echo ErrorLevel %_?
pshell /s "$x = 11"
echo ErrorLevel %_?
echo %@pshell[$x = 11]
echo ErrorLevel %_?
endlocal

Sample run of above...
Code:
c:\users\jlc\utils>ValidateRange.btm
ErrorLevel 0
PSHELL: System.Management.Automation.ValidationMetadataException : The variable cannot be validated because the value 11 is no
t a valid value for the x variable.
ErrorLevel 1510
PSHELL: System.Management.Automation.ValidationMetadataException : The variable cannot be validated because the value 11 is no
t a valid value for the x variable.
ErrorLevel 2

This code works as expected, that is, it returns an error because 11 is not in the range 1 to 10.

Therefore, why do PSHELL and @PSHELL return different errorlevels for the same error?

Joe
 
I believe ERRORLEVEL and _? are set by commands, and not by variable functions.
 
Thanks. I've made changes so that my @PSHELL function will return a numeric value;
Code:
@setlocal
@echo off
pshell /s "[ValidateRange(1,10)] [int]$x = 1"
seterror %@pshell[try {$x = 11} catch [System.Management.Automation.ValidationMetadataException] { 1 } finally { 0 }]
echo ErrorLevel %_?  %?
seterror %@pshell[try {$x = 5} catch [System.Management.Automation.ValidationMetadataException] { 1 } finally { 0 }]
echo ErrorLevel %_?  %?
endlocal
Code:
c:\users\jlc\utils>ValidateRange.btm
ErrorLevel 1  1
ErrorLevel 0  0

Joe
 
Back
Top
[FOX] Ultimate Translator
Translate