반응형
실행 파일이 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
반응형
'IT이야기' 카테고리의 다른 글
내 node.js 인스턴스가 개발 또는 프로덕션인지 확인 (0) | 2021.03.29 |
---|---|
MemoryStream-닫힌 스트림에 액세스 할 수 없습니다. (0) | 2021.03.29 |
rm -rf의 rf 의미 (0) | 2021.03.29 |
파이썬 문자열 리터럴에서 백 슬래시 인용 (0) | 2021.03.29 |
WordPress가 제대로 프로그래밍되지 않은 것으로 간주되는 이유 (0) | 2021.03.29 |