CMD返回ErrorLevel而不运行程序

时间:2012-06-15 21:38:30

标签: cmd errorlevel nul

我正在通过CMD提示运行Powershell命令,我想在开始运行命令之前检查是否先安装了Powershell。如果PowerShell不存在而我没有在下面显示实际错误,我希望脚本退出。这是我的剧本:

@echo off
setlocal enabledelayedexpansion
:: Check to see if Powershell is installed
powershell.exe -command {"test"} > NUL
if errorlevel 1 (
    echo/Powershell is NOT Installed
    EXIT
) else (
    goto PSI
)

:PSI
powershell Set-ExecutionPolicy RemoteSigned

我遇到的问题是我将其作为输出:

Powershell is NOT Installed

'powershell.exe' is not recognized as an internal or external command,
operable program or batch file.

1 个答案:

答案 0 :(得分:1)

想出来!我不得不使用2> NUL而不是NUL来重定向输出:

:: Check to See if Powershell is Installed
powershell.exe test 2>NUL
    if errorlevel 1 (
        echo/Powershell is NOT Installed
    EXIT
    ) else (
    goto PSI
    )