我需要在以admin身份執行時捕獲批處理文件中powershell腳本的輸出。當以admin身份執行時,如何捕獲批處理文件中powershell腳本的錯誤級別
例子:
PS1文件:
Write-Host "PS1 executed"
exit 1
如果我執行PowerShell腳本沒有管理員權限
NotAdminFile.bat:
@ECHO OFF
setlocal enableextensions
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
echo %ERRORLEVEL%
endlocal
然後,輸出是
PS1 executed
1
這沒關係。 但是,當我執行PowerShell腳本擁有管理員權限的
AdminFile.bat:
@ECHO OFF
setlocal enableextensions
PowerShell.exe -NoProfile -Command "& {Start-Process PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File "%~dpn0.ps1" ' -Verb RunAs}"
echo %ERRORLEVEL%
endlocal
然後,輸出是:
0
,我不希望出現這種情況。你能幫我嗎?
IMO它**是** OK,因爲批錯誤級別反映PowerShell運行啓動過程沒有錯誤。 – LotPings