使用& cmd /c myscript.cmd
如果在调用外部脚本之前引发了很多错误(比方说200),则外部调用处理不当:批处理文件根本不执行。
当错误数不高时(假设为10),它正常工作(调用外部批处理)。
这是一个错误吗?这是PoC:将$ numberOfError的值更改为不同的值(10,然后是200)并查看输出更改。
# Creation of a temporary Batch
Set-Content foo.cmd '@echo THE BATCH SCRIPT HAS BEEN CALLED !' -Encoding ASCII
$numberOfError = 10
# $numberOfError = 200 # if high value (sometimes only 40 is enough,
# other times > 140 do the trick)
for($i=0;$i -lt $numberOfError;$i++){
Rename-Item c:\not_existing.txt c:\blah.txt # just used to raise an Error
}
Write-Host "Before call"
# the below line won't be called if $numberOfError is >= 87
# (might be a higher value on some other machines maybe)
& cmd /c foo.cmd
Write-Host "Still there"
以下是我得到的输出类型:
[... Bunch of Error Messages ...]
Before call
THE BATCH SCRIPT HAS BEEN CALLED ! <------ THIS LINE CHANGE
Still there
[... Bunch of Error Messages ...]
Before call
Still there
如果这是一个已知的powershell错误,请告诉我,我知道我可以处理错误,但这种行为对我来说似乎不正常。
P.S:仅使用Powershell v2.0二进制文件进行测试。