2012-06-15 54 views
0

我通過DOS提示符運行Powershell命令,我想在開始運行我的命令之前檢查是否安裝了Powershell。如果沒有顯示下面的實際錯誤,powershell不存在,我希望腳本退出。這裏是我的腳本:DOS返回ErrorLevel沒有運行程序

@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

想通了!我不得不使用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 
    ) 
+0

實際上,上述powershell命令錯誤出來爲好。我不得不使用PowerShell「Write-Output」'n'「2> NUL –