如何在ExecutionPolicy错误后强制Powershell设置ERRORLEVEL变量

时间:2016-08-18 07:44:31

标签: powershell batch-file error-handling

Powershell未在ExecutionPolicy错误上设置ERRORLEVEL变量 那么如何在批处理代码中确定脚本失败了?

C:\Windows\system32>powershell -File myScript.ps1  
Attempting to perform the InitializeDefaultDrives operation on the 'FileSystem' provider failed.  
File myScript.ps1 cannot be loaded. The file myScript.ps1 is not digitally signed. You cannot run this script on the current system.  
For more information about running scripts and setting execution policy, see about_Execution_Policies at
http://go.microsoft.com/fwlink/?LinkID=135170.
+ CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : UnauthorizedAccess

C:\Windows\system32>echo %ERRORLEVEL%  
0

2 个答案:

答案 0 :(得分:0)

set NLS_LANG=American_America.AL32UTF8

REM your code

echo %ERRORLEVEL%

这是一个简单的批处理文件。 %ERRORLEVEL%会在提示

中显示错误

答案 1 :(得分:0)

在底部,您可以放置​​以下内容:

if (-not $?) { exit 1 }