Microsoft MVP성태의 닷넷 이야기
스크립트: 19. Windows PowerShell의 NonInteractive 모드 [링크 복사], [링크+제목 복사],
조회: 15752
글쓴 사람
정성태 (techsharer at outlook.com)
홈페이지
첨부 파일
 

Windows PowerShell의 NonInteractive 모드

간단한 테스트를 위해 test.ps1 파일을 만들고,

mkdir vendor
cd vendor
echo "test" > test.txt
cd ..

if (Test-Path "vendor/") {
    rmdir vendor
}

이것을 "-NonInteractive" 옵션과 함께 실행하면 이런 오류가 발생합니다.

C:\temp> powershell -NonInteractive -File test.ps1

    Directory: C:\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020-03-20   오후 3:50                vendor                                                               
rmdir : Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available.
At C:\temp\test.ps1:8 char:5
+     rmdir vendor
+     ~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [Remove-Item], PSInvalidOperationException
    + FullyQualifiedErrorId : InvalidOperation,Microsoft.PowerShell.Commands.RemoveItemCommand

이유는 간단합니다, NonInteractive 모드는 사용자로부터 입력받을 수 없는 상황을 염두에 두고 만든 것이기 때문에 스크립트 실행 도중 "사용자로부터의 입력"을 받아야 하는 상황이 되면 무작정 기다리지 않고 그냥 실패를 하는 것입니다. 실제로 해당 스크립트를 NonInteractive 옵션 없이 실행하면 다음과 같이 사용자 입력을 받게 되는 상황이 있다는 것을 확인할 수 있습니다.

C:\temp> powershell -File test.ps1

    Directory: C:\temp

Mode                LastWriteTime         Length Name
----                -------------         ------ ----
d-----       2020-03-20   오후 3:52                vendor                                                               
Confirm
The item at C:\temp\vendor has children and the Recurse parameter
was not specified. If you continue, all children will be removed with the item. Are you sure you want to continue?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"):




NonInteractive 모드는, 예를 들어 빌드 스크립트 같은 곳에서 사용될 수 있습니다. 개발자가 소스 코드를 커밋하면 형상 관리 시스템에서 자동으로 최신 소스 코드에 대한 빌드를 수행하는데, 그 과정에 사용자로부터 입력을 기대하는 상황이 발생하면 어떻게 될까요?

만약 PowerShell 프로그램이 NonInteractive 모드를 지원하지 않는다면, 혹시나 개발자가 실수로 작성하는 스크립트에 입력 동작이 있는 경우 빌드 스크립트는 그냥 정지한 체로 멈추게 될 것입니다. 이런 상황에서 명시적으로 실패할 수 있도록 만든 것이 바로 NonInteractive 모드이고, 그에 대한 오류 메시지가 "Windows PowerShell is in NonInteractive mode. Read and Prompt functionality is not available."인 것입니다.

따라서, 만약 이런 오류를 만나게 된다면 해당 스크립트가 사용자 입력 없이 계속 실행되도록 명령을 보완해야 합니다. 가령, 이 글의 예제에서 다룬 스크립트라면 다음과 같이 바꾸면 됩니다.

mkdir vendor
cd vendor
echo "test" > test.txt
cd ..

if (Test-Path "vendor/") {
    rmdir -r vendor
}




[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]







[최초 등록일: ]
[최종 수정일: 4/2/2020]

Creative Commons License
이 저작물은 크리에이티브 커먼즈 코리아 저작자표시-비영리-변경금지 2.0 대한민국 라이센스에 따라 이용하실 수 있습니다.
by SeongTae Jeong, mailto:techsharer at outlook.com

비밀번호

댓글 작성자
 




... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...
NoWriterDateCnt.TitleFile(s)
11833정성태3/4/201921026개발 환경 구성: 431. Visual Studio 2019 - CMake를 이용한 공유/실행(so/out) 리눅스 프로젝트 설정파일 다운로드1
11832정성태3/4/201916963오류 유형: 524. Visual Studio CMake - rsync: connection unexpectedly closed
11831정성태3/4/201916777오류 유형: 523. Visual Studio 2019 - 새 창으로 뜬 윈도우를 닫을 때 비정상 종료
11830정성태2/26/201916471오류 유형: 522. 이벤트 로그 - Error opening event log file State. Log will not be processed. Return code from OpenEventLog is 87.
11829정성태2/26/201918208개발 환경 구성: 430. 마이크로소프트의 CoreCLR 프로파일러 예제 빌드 방법 - 리눅스 환경 [1]
11828정성태2/26/201926088개발 환경 구성: 429. Component Services 관리자의 RuntimeBroker 설정이 2개 있는 경우 [8]
11827정성태2/26/201919037오류 유형: 521. Visual Studio - Could not start the 'rsync' command on the remote host, please install it using your system package manager.
11826정성태2/26/201919223오류 유형: 520. 우분투에 .NET Core SDK 설치 시 패키지 의존성 오류
11825정성태2/25/201924411개발 환경 구성: 428. Visual Studio 2019 - CMake를 이용한 리눅스 빌드 환경 설정 [1]
11824정성태2/25/201918851오류 유형: 519. The SNMP Service encountered an error while accessing the registry key SYSTEM\CurrentControlSet\Services\SNMP\Parameters\TrapConfiguration. [1]
11823정성태2/21/201920610오류 유형: 518. IIS 관리 콘솔이 뜨지 않는 문제
11822정성태2/20/201918882오류 유형: 517. docker에 설치한 MongoDB 서버로 연결이 안 되는 경우
11821정성태2/20/201919644오류 유형: 516. Visual Studio 2019 - This extension uses deprecated APIs and is at risk of not functioning in a future VS update. [1]
11820정성태2/20/201922701오류 유형: 515. 윈도우 10 1809 업데이트 후 "User Profiles Service" 1534 경고 발생
11819정성태2/20/201921992Windows: 158. 컴퓨터와 사용자의 SID(security identifier) 확인 방법
11818정성태2/20/201920023VS.NET IDE: 131. Visual Studio 2019 Preview의 닷넷 프로젝트 빌드가 20초 이상 걸리는 경우 [2]
11817정성태2/17/201916417오류 유형: 514. WinDbg Preview 실행 오류 - Error : DbgX.dll : WindowsDebugger.WindowsDebuggerException: Could not load dbgeng.dll
11816정성태2/17/201919813Windows: 157. 윈도우 스토어 앱(Microsoft Store App)을 명령행에서 직접 실행하는 방법
11815정성태2/14/201918072오류 유형: 513. Visual Studio 2019 - VSIX 설치 시 "The extension cannot be installed to this product due to prerequisites that cannot be resolved." 오류 발생
11814정성태2/12/201916909오류 유형: 512. VM(가상 머신)의 NT 서비스들이 자동 시작되지 않는 문제
11813정성태2/12/201918301.NET Framework: 809. C# - ("Save File Dialog" 등의) 대화 창에 확장 속성을 보이는 방법
11812정성태2/11/201915569오류 유형: 511. Windows Server 2003 VM 부팅 후 로그인 시점에 0xC0000005 BSOD 발생
11811정성태2/11/201920711오류 유형: 510. 서버 운영체제에 NVIDIA GeForce Experience 실행 시 wlanapi.dll 누락 문제
11810정성태2/11/201918474.NET Framework: 808. .NET Profiler - GAC 모듈에서 GAC 비-등록 모듈을 참조하는 경우의 문제
11809정성태2/11/201920575.NET Framework: 807. ClrMD를 이용해 메모리 덤프 파일로부터 특정 인스턴스를 참조하고 있는 소유자 확인
11808정성태2/8/201921904디버깅 기술: 123. windbg - 닷넷 응용 프로그램의 메모리 누수 분석
... 76  77  78  79  80  81  82  83  [84]  85  86  87  88  89  90  ...