简体   繁体   English

以管理员身份执行时如何捕获批处理文件中Powershell脚本的错误级别

[英]How capture errorlevel of powershell script in a batch file when execute as admin

I need to capture output of powershell script in a batch file when execute as admin. 以管理员身份执行时,我需要在批处理文件中捕获powershell脚本的输出。

Example: 例:

ps1 file: ps1文件:

Write-Host "PS1 executed"
exit 1

If I execute powershell script without admin access 如果我在没有管理员访问权限的情况下执行Powershell脚本

NotAdminFile.bat: NotAdminFile.bat:

@ECHO OFF
setlocal enableextensions
PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command "& '%~dpn0.ps1'"
echo %ERRORLEVEL%
endlocal

Then, the output is 然后,输出是

PS1 executed
1

This is ok. 还行吧。 But, when I execute powershell script with admin access 但是,当我通过管理员权限执行Powershell脚本时

AdminFile.bat: 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

Then, the output is: 然后,输出为:

0

I don't want that. 我不要 Can you help me please? 你能帮我吗?

exit_1_only.ps1 exit_1_only.ps1

Write-Host "executed: $($MyInvocation.Line)"
# pause
Exit 123+[int]([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::
    GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]"Administrator")

q44600354.bat q44600354.bat

@ECHO OFF
SETLOCAL EnableExtensions DisableDelayedExpansion

(call )
echo errorlevel clear=%errorlevel%

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
  "& 'D:\PShell\tests\exit_1_only.ps1'; exit $LASTEXITCODE"
echo errorlevel non-admin=%errorlevel%

echo(
(call )
echo errorlevel clear=%errorlevel%

PowerShell.exe -NoProfile -ExecutionPolicy Bypass -Command ^
  "& {exit ( Start-Process -Wait -PassThru -FilePath PowerShell.exe -ArgumentList '-NoProfile -ExecutionPolicy Bypass -Command ""D:\PShell\tests\exit_1_only.ps1; exit $LASTEXITCODE"" ' -Verb RunAs).ExitCode}"
echo errorlevel admin=%errorlevel%

Output 产量

==> D:\bat\SO\q44600354.bat
errorlevel clear=0
executed: & 'D:\PShell\tests\exit_1_only.ps1'; exit $LASTEXITCODE
errorlevel non-admin=123

errorlevel clear=0
errorlevel admin=124

==>

Explanation 说明

Exit Codes 退出码

In PowerShell $? 在PowerShell $? contains True if last operation succeeded and False otherwise. 如果上一次操作成功,则包含True ,否则包含False

The exit code of the last Win32 executable execution is stored in the automatic variable $LASTEXITCODE 最后一个Win32可执行文件执行的退出代码存储在自动变量$LASTEXITCODE

To read exit codes (other than 0 or 1 ) launch the PowerShell script and return the $LASTEXITCODE in a single line like this: 要读取退出代码(非01 ),请启动PowerShell脚本,并在$LASTEXITCODE中返回$LASTEXITCODE ,如下所示:

 powershell.exe -noprofile C:\\scripts\\script.ps1; exit $LASTEXITCODE 

Start-Process 开始处理

-PassThru

Returns a System.Diagnostics.Process process object for each process that the cmdlet started. 为该cmdlet启动的每个进程返回一个System.Diagnostics.Process进程对象。 By default, this cmdlet does not generate any output. 默认情况下,此cmdlet不会生成任何输出。

-Wait

Indicates that this cmdlet waits for the specified process to complete before accepting more input. 指示此cmdlet在接受更多输入之前等待指定的过程完成。 This parameter suppresses the command prompt or retains the window until the process finishes. 此参数禁止显示命令提示符或保留窗口,直到过程完成。

(call ) : Dave Benham in reply to setting ERRORLEVEL to 0 question: (call ) :Dave Benham答复了将ERRORLEVEL设置为0的问题:

If you want to force the errorlevel to 0 , then you can use this totally non-intuitive, but very effective syntax: (call ) . 如果要将错误errorlevel强制设置为0 ,则可以使用以下完全不直观但非常有效的语法: (call ) The space after call is critical. 通话后的空间至关重要。 If you want to set the errorlevel to 1 , you can use (call) . 如果要将错误errorlevel设置为1 ,则可以使用(call) It is critical that there not be any space after call . 呼叫后必须没有空格是至关重要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM