Powershell 명령의 매개변수 사이 공백

Powershell 명령의 매개변수 사이 공백

Powershell 명령에서 매개변수 사이에 공백을 두면 오류가 발생하는 이유를 알고 싶습니다.

와 함께공간 : (작동하지 않음)

wmic desktopmonitor get screenwidth, screenheight
Expression GET non valide.

없이공간 : (일)

wmic desktopmonitor get screenwidth,screenheight
ScreenHeight  ScreenWidth

그게 정상인가요? 인터넷에서 공백이 포함된 명령을 많이 보기 때문입니다!


업데이트 2016/01/27 버전 세부정보:

$PSVersionTable

Name                           Value
----                           -----
PSVersion                      5.0.10240.16384
WSManStackVersion              3.0
SerializationVersion           1.1.0.1
CLRVersion                     4.0.30319.42000
BuildVersion                   10.0.10240.16384
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3

두 가지 방법(공백 유무)은 CMD에서 작동하지만 두 번째 방법(공백 없음)만 PowerShell에서 작동합니다(아래 스크린샷 참조).

Cmd.exe

파워셸

답변1

쉼표는 PowerShell의 배열 연산자입니다. 따라서 귀하의 명령은 다음과 같습니다.

wmic desktopmonitor get screenwidth, screenheight

다음과 같은 의미를 갖습니다: wmic세 개의 인수로 호출합니다: string desktopmonitor, string get및 두 개의 문자열이 있는 배열 screenwidthscreenheight. 기본 애플리케이션 이므로 wmicPowerShell은 인수를 명령줄로 변환해야 합니다. PowerShell은 배열을 명령줄로 변환할 때 구분 기호로 공백을 사용합니다. 따라서 결과 명령줄은 다음과 같습니다.

wmic desktopmonitor get screenwidth screenheight

다음 명령을 입력하면 이를 확인할 수 있습니다.

cmd /c echo wmic desktopmonitor get screenwidth, screenheight

PowerShell v5부터 여기에는 특별한 경우가 하나 있습니다. 배열이 하위 표현식이 아닌 직접 제공되고 쉼표와 배열 요소 사이에 공백이 없는 경우 PowerShell은 배열을 명령줄로 변환할 때 쉼표를 구분 기호로 사용합니다.

PS> cmd /c echo 1,2,3 (4,5,6) 7,8 ,9
1,2,3 4 5 6 7 8 9

이 명령은 다음과 같습니다.

wmic desktopmonitor get screenwidth,screenheight

이 특별한 경우에 적합하며 결과 명령줄은 다음과 같습니다.

wmic desktopmonitor get screenwidth,screenheight

관련 정보