Shell & CMD/PowerShell
-
[PowerShell] 자동변수(Automatic Variable) - 7. $IsCoreCLR, $IsLinux, $IsMacOS, $IsWindowsShell & CMD/PowerShell 2023. 11. 5. 11:52
$IsCoreCLR (PowerShell Core 전용) 실행중인 Powershell이 Core 버전인지 아닌지 확인할 때 사용할 수 있는 Boolean 변수 아래 Test() 함수를 생성 후 실행시 PowerShell Core에선 `core` 라는 메시지를 확인할 수 있다. function Test() { if($IsCoreCLR) { Write-Host 'core' } else { Write-Host 'not core' } } 예측가능한 결과가 나왔지만 $IsCoreCLR 값은 Windows PowerShell 자체에서 유효하지 않을 수 있다. $IsCoreCLR.GetType() 를 실행하면 Core 기반 PowerShell에선 아래 이미지와 같이 정상적인 정보가 표출된다. 반면, Windows 버..
-
[PowerShell] 자동변수(Automatic Variable) - 6. $HOME, $Host, $inputShell & CMD/PowerShell 2023. 11. 5. 09:53
$HOME 사용자 홈 경로값이다. Windows 기준으로 기본 설정을 건드리지 않는 한 C:\Users\{계정} 위치가 표기된다. $Host 현재 사용중인 Powershell의 정보를 표기한다. $input $foreach와 마찬가지로 열거자를 가리키는 자동변수이다. $foreach, $input, $switch 와 같은 열거자 자동변수는 가급적 사용하지 않는 것을 권장하고 있다. https://learn.microsoft.com/ko-kr/powershell/module/microsoft.powershell.core/about/about_automatic_variables?view=powershell-7.3#using-enumerators 아래 소스코드는 입력받은 pipline 을 받아 그대로 화면에 ..
-
[PowerShell] 자동변수(Automatic Variable) - 5. $ExecutionContext, $true, $false, $foreachShell & CMD/PowerShell 2023. 11. 4. 21:25
$ExecutionContext 현재 PowerShell 호스트 실행 컨텍스트 객체를 가리킨다. 입력시 아래와 같은 정보를 출력한다. ❯ $ExecutionContext Host : System.Management.Automation.Internal.Host.InternalHost Events : System.Management.Automation.PSLocalEventManager InvokeProvider : System.Management.Automation.ProviderIntrinsics SessionState : System.Management.Automation.SessionState InvokeCommand : System.Management.Automation.CommandInvocati..
-
[PowerShell] 자동변수(Automatic Variable) - 4. $Error, $Event, $EventArgs, $EventSubscriber, $SenderShell & CMD/PowerShell 2023. 11. 4. 18:56
$Error 세션실행 후 오류발생시 내역이 저장되는 변수. 명령어에 오류가 발생하면 해당 $Error 배열형 변수에 누적된다. 콘솔에서 아래와 같은 순서로 오류를 유발한 뒤 $Error를 조회하면 실행실패한 오류내역이 모두 출력된다. 1/0 $arr = 1,2,3 $arr[3] = 4 $Event 실행 중인 이벤트 정보를 가지고 있는 변수. 이벤트를 통해 등록한 스크립트 블록 내에서 유효하다. $EventArgs 실행 중인 이벤트 정보 중 이벤트 인수를 의미한다. 이벤트를 통해 등록한 스크립트 블록 내에서 유효하다. $EventSubscriber 실행 중인 이벤트 정보 중 이벤트 구독자를 의미한다. 이벤트를 통해 등록한 스크립트 블록 내에서 유효하다. $Sender 이벤트를 실행한 객체 정보를 의미한다...
-
[PowerShell] 자동변수(Automatic Variable) - 3. $args, $ConsoleFileName, $EnabledExperimentalFeaturesShell & CMD/PowerShell 2023. 11. 4. 13:37
$args 함수, 스크립트 또는 스크립트 블록에 전달되는 선언되지 않은 매개 변수에 대한 값 배열을 의미한다. function 생성시 별도 파라메터를 정의하지 않았지만 입력받은 파라메터에 대한 표현이 필요할 경우 사용할 수 있다. 아래 test() 코드와 같이 별도 파라메터를 선언하지 않았어도, 파라메터를 추가하여 메소드를 전달했다면 $args를 통해 파라메터 값을 확인할 수 있다. function test() { Write-Host $args }; $ConsoleFileName (Windows Powershell 5.1) 해당 자동변수는 Windows Powershell 5.1 한정으로 존재하는 Export-Console 과 연관된 변수이다. Export-Console로 생성된 psc1 파일을 실행하면..
-
[PowerShell] 자동변수(Automatic Variable) - 2. $$, $?, $^, $_Shell & CMD/PowerShell 2023. 11. 4. 13:04
(0,1,2) | Foreach-Object { Write-Host $_ } $$ powershell 실행창(=세션)에서 받은 마지막 줄 마지막 토큰 출력. 예시로 Write-Host 'a','b','c' 입력시 가장 마지막 토큰인 c가 출력된다. Write-Host 'a','b','c' $? 마지막 명령의 실행 성공여부를 판별하는 boolean 변수. 성공시 True, 실패시 False가 출력된다. 아래 예시에서 1/0 실행 후 $?를 입력하면 0으로 나누기 오류로 인해 False가 출력된다. 1/0 $^ powershell 실행창(=세션)에서 받은 마지막 줄 가장 첫번째 토큰 출력. 예시로 Write-Host 'a','b','c' 입력시 첫번째 토큰인 Write-Host가 출력된다. $_ 파이프라인의..
-
[PowerShell] 자동변수(Automatic Variable) - 1. 자동변수 개념Shell & CMD/PowerShell 2023. 11. 4. 11:51
자동변수란? Powershell 에선 자동지정 변수라는 개념이 존재한다. 미리 정의된 특정 변수명이며, 지정된 역할에 따라 스크립트 동작시 값이 자동할당된다. 예를 들어, 배열 @(1,2,3)을 Foreach-Object 실행하여 Write-Host한다고 한다고 가정하자. @(1,2,3) | ForEach-Object { Write-Host $_ } Write-Host 에 사용자가 정의하지 않은 $_ 변수명이 보이는데, 해당 변수를 자동변수라고한다. $_ 에는 Foreach-Object를 통해 Arrey Item이 1개씩 자동할당 된다. 자동변수는 사용자가 임의로 지정할 수 없 출처 변수 정보 - PowerShell | Microsoft Learn 자동 변수 정보 - PowerShell | Microso..
-
[PowerShell] Publish-Script 명령어로 배포시 Failed to generate the compressed file for script 'Cannot index into a null array.' 오류 현상Shell & CMD/PowerShell 2023. 3. 25. 23:23
파워셸로 스크립트를 배포하는 과정에서 Failed to generate the compressed file for script 'Cannot index into a null array.' 가 발생했다. 원인은 배포할 스크립트를 Publish-Script가 배포할 스크립트를 nupkg 형식으로 만드는 과정에서 나타나는 오류인데, 스크립트를 잘못 작성해서 문제가 된게 아니라 OS가 영어가 아닌 다른 언어일 경우 나타나는 환경적인 이슈이다. 관련 내용은 Github issue 에서 확인 가능하다. https://github.com/PowerShell/PowerShellGetv2/issues/606#issuecomment-686660440 Failed to generate the compressed file f..