2012-06-28 2 views

답변

70

당신이 예 오류 메시지가 표시하거나 실행 파일을 (NuGet 생각)을 다운로드에 대한 경로에서 명령의 비 존재를 테스트하려는 경우 당신은 Get-Command (gcm)

if (Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) 
{ 
    pandoc -Ss readme.txt -o readme.html 
} 

을 통해 테스트 할 수 있습니다 :

if ((Get-Command "pandoc.exe" -ErrorAction SilentlyContinue) -eq $null) 
{ 
    Write-Host "Unable to find pandoc.exe in your PATH" 
} 

INF를 얻을 수있는 PowerShell 세션에서

(Get-Help gcm).description 

시도 Get-Command에 관한 ormation.

+1

데이비드. 큰 빨간 오류를 억제하면서 어떻게하면 좋습니까? –

+1

아무도 말한 적이 없기 때문에 추가 된 "-ErrorAction SilentlyContinue"가 큰 빨간색 오류를 없앱니다. –

+1

'where.exe'를 사용하고 종료 코드를 확인했지만 PS 명령이 아닙니다. – orad

1

일부 사용자에게 유용합니다. 다음은 David Brabant의 답변에 대한 최소 버전 번호 확인 기능입니다.

Function Ensure-ExecutableExists 
{ 
    Param 
    (
     [Parameter(Mandatory = $True)] 
     [string] 
     $Executable, 

     [string] 
     $MinimumVersion = "" 
    ) 

    $CurrentVersion = (Get-Command -Name $Executable -ErrorAction Stop).Version 

    If($MinimumVersion) 
    { 
     $RequiredVersion = [version]$MinimumVersion 

     If($CurrentVersion -lt $RequiredVersion) 
     { 
      Throw "$($Executable) version $($CurrentVersion) does not meet requirements" 
     } 
    } 
} 

이것은 당신이 다음을 수행 할 수 있습니다 :

Ensure-ExecutableExists -Executable pscp -MinimumVersion "0.62.0.0" 

그것은 요구 사항이 충족되는 경우 아무것도하지 않습니다 또는 오류가 발생이되지 않습니다.