【发布时间】:2019-03-14 10:13:39
【问题描述】:
我有一个如下所示的 XML 文件:
<Feature>
<B7>A</B7>
<B8>B</B8>
<B9>C</B9>
<ExitCode>
<Found>123</Found>
<NotFound>789</NotFound>
</ExitCode>
</Feature>
我的 PowerShell 脚本如下所示:
$regex = [regex]$Pattern
$matching = $regex.Match($FB)
if ($matching.Success) {
while ($matching.Success) {
"Match found: {0}" -f $matching.Value
exit 123 #I want to delete this
$matching = $matching.NextMatch()
}
} else {
"Not Found"
exit 789 #I want to delete this
}
我想得到exitcode,但是我不想写exit 123和exit 780,我只是想从XML文件中调用exit code,所以每次我想改变exitcode号,我只是从 XML 文件修改,而不是从 PowerShell 脚本修改。
所以,如果我使用批处理文件运行脚本,我可以获得退出代码日志,如下所示:
set log=C:\Users\Log.txt
set PS=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
set PS=%PS%\powershell.exe -ExecutionPolicy Bypass -File
"C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -ExecutionPolicy Bypass -File C:\Users\XML.ps1 -i C:\Users\Feature.txt -j C:\Users\XML.xml
echo %ERRORLEVEL% > "%log%"
【问题讨论】:
-
我不清楚 XML 在您的 PowerShell 代码中的什么位置。您既不阅读也不处理代码中的任何地方。
-
How can I get powershell to return the correct exit code when called with the -File argument? 的可能重复项。您的问题确实不呈现minimal reproducible example(即使在阅读了您之前的问题之后)
标签: xml powershell batch-file exit-code