0

我正在通过 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.
4

1 回答 1

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
    )
于 2012-06-15T21:43:13.673 回答