IT이야기

실행 파일이 PowerShell의 경로에 있는지 테스트

cyworld 2021. 3. 29. 21:16
반응형

실행 파일이 PowerShell의 경로에 있는지 테스트


내 스크립트에서 명령을 실행하려고합니다.

pandoc -Ss readme.txt -o readme.html

하지만 pandoc이 설치되어 있는지 확실하지 않습니다 . 그래서 (의사 코드)하고 싶습니다.

if (pandoc in the path)
{
    pandoc -Ss readme.txt -o readme.html
}

실제로 어떻게 할 수 있습니까?


Get-Command (gcm)를 통해 테스트 할 수 있습니다.

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

예를 들어 오류 메시지를 표시하거나 실행 파일을 다운로드하기 위해 경로에 명령이 존재하지 않는지 테스트하려면 (NuGet을 생각해보십시오) :

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

시험

(Get-Help gcm).description

PowerShell 세션에서 Get-Command에 대한 정보를 가져옵니다.


다음은 최소 버전 번호를 확인하는 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"

요구 사항이 충족되거나 그렇지 않은 오류가 발생하면 아무 작업도 수행하지 않습니다.

참조 URL : https://stackoverflow.com/questions/11242368/test-if-executable-is-in-path-in-powershell

반응형