我有一个如下所示的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
}
我想获取退出代码,但是我不想写exit 123
和exit 780
,我只想从XML文件中调用退出代码,所以每次我想更改退出代码时数字,我只是从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%"
答案 0 :(得分:1)
这样的事情;读取XML文件,然后通过属性引用从中获取数字:
$xmlData = New-Object -TypeName System.Xml.XmlDocument
$xmlData.Load('c:\test\data.xml')
$regex = [regex]$Pattern
$matching = $regex.Match($FB)
if ($matching.Success) {
while ($matching.Success) {
"Match found: {0}" -f $matching.Value
exit $xmlData.Feature.ExitCode.Found
$matching = $matching.NextMatch()
}
} else {
"Not Found"
exit $xmlData.Feature.ExitCode.NotFound
}
编辑:进行更新以更好地处理XML,从而可以正确处理XML文件编码。感谢@tomalak的评论。