我有 powrshell 脚本来检查 .xml 文件,如果找到 - 以 5 退出,否则以 0 退出:
# Ensure the script is run as administrator
if (-not ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Host "You need to run this script as an administrator."
exit 6
}
# Define the directories
$xmlRoot = "D:\myFolder"
# Count xml files in all subdirectories
$xmlFiles = Get-ChildItem -Path $xmlRoot -Recurse -Filter *.xml
$xmlCount = $xmlFiles.Count
Write-Host "Total xml files found: $xmlCount"
if ($xmlCount -gt 0) {
exit 5
} else {
exit 0
}
在 powershell 中,当我检查 $LASTEXITCODE 时,我确实看到了 5, 但是当我尝试在外部应用程序(例如 .bat)中捕获它时,我没有收到错误代码:
@echo off powersheel "c:\Users\location\countXML.ps1" echo exit code is %LASTEXITCODE% pause
(我得到“退出代码是”,没有任何错误代码)
我尝试过:
但似乎没有任何帮助。
谢谢你。
我在另一个线程中找到了答案,作者:@js2010 https://stackoverflow.com/a/57429573/3170556
非常感谢!