개요
Write-Verbose, Write-Debug, Write-Warning, Write-Information은 각각 서로 다른 출력 스트림에 메시지를 보내는 진단용 cmdlet이다. 29장에서 Write-Host와 Write-Output의 차이를 다루며 스트림 개념을 처음 소개했는데, 이 장은 그 스트림 모델을 진단 목적의 나머지 네 스트림으로 확장한다. 58장에서 다룬 [CmdletBinding()]이 함수에 -Verbose/-Debug 매개변수를 자동으로 추가해 주는 이유가 바로 이 cmdlet들과 짝을 이루기 위해서다.
정신 모델은 “각 스트림은 서로 다른 청중을 위한 것"이라는 것이다 — Verbose는 “지금 무슨 일이 일어나고 있는지” 더 자세히 알고 싶은 사용자, Debug는 스크립트를 작성한 개발자, Warning은 치명적이지 않지만 주의가 필요한 상황, Information은 구조화된 로그 기록용이다.
사용법
| |
종류
| cmdlet | 스트림 번호 | 기본 표시 여부 | 켜는 방법 |
|---|---|---|---|
Write-Verbose | 4 | 숨김 | -Verbose 공통 매개변수 또는 $VerbosePreference = "Continue" |
Write-Debug | 5 | 숨김 | -Debug 공통 매개변수 또는 $DebugPreference = "Continue"(기본적으로 확인 프롬프트도 뜸) |
Write-Warning | 3 | 표시됨(노란 글씨) | -WarningAction SilentlyContinue로 숨길 수 있음(66장) |
Write-Information | 6 | 숨김(PowerShell 5+) | -InformationAction Continue 또는 $InformationPreference |
예시
| |
주의사항·함정
Write-Host(29장)와 이 네 cmdlet은 근본적으로 다른 목적을 가진다: Write-Host는 사용자에게 항상 보여줄 콘솔 전용 출력이고 리다이렉션이나 캡처가 안 되지만, 이 장의 네 cmdlet은 각자의 스트림 번호를 가져 *>&1처럼 다른 스트림으로 리다이렉션하거나 파일로 캡처할 수 있다. “디버깅 정보를 로그 파일에도 남기고 싶다"면 Write-Host가 아니라 Write-Verbose/Write-Debug를 쓰고 스트림을 리다이렉션해야 한다.
Write-Debug는 기본적으로 확인 프롬프트를 띄운다: -Debug를 붙이면 메시지가 나올 때마다 “계속하시겠습니까?” 같은 프롬프트가 뜬다 — 자동화 스크립트에서 예상치 못하게 이 프롬프트에 걸려 멈추는 경우가 있다. 프롬프트 없이 메시지만 보고 싶다면 -Debug:$false가 아니라 $DebugPreference = "Continue"로 설정해야 확인 없이 흘러간다.
공통 매개변수로 켠 스트림 표시는 그 명령 호출에만 적용되고, 그 명령이 내부에서 호출하는 다른 함수에는 자동으로 전파되지 않는다: Search-Log -Verbose를 호출해도, Search-Log 내부에서 호출하는 다른 사용자 정의 함수의 Write-Verbose까지 자동으로 표시되지는 않는다 — 그 함수도 [CmdletBinding()]을 갖추고 있어야 하며, -Verbose를 전달하려면 60장에서 배운 스플래팅이나 $PSBoundParameters를 활용해야 한다.
이식성: Bash·CMD는 표준 출력(stdout)과 표준 오류(stderr) 두 스트림만 기본 제공해, “상세 로그”·“경고”·“디버그"를 구분하려면 관례적으로 접두사를 붙이거나 별도 로깅 함수를 직접 구현해야 한다. PowerShell이 애초에 6개(성공·오류·경고·상세·디버그·정보)의 이름 붙은 스트림을 표준으로 제공하는 것은, 29장에서 강조한 “출력의 의도를 명확히 구분한다"는 설계 철학의 연장이다.
![Featured image of post [PowerShell] 68. Write-Verbose/Debug/Warning/Information](/post/powershell/write-verbose-debug-warning-powershell/wordcloud_hu_a60a9726da3161d.webp)
![[PowerShell] 66. $ErrorActionPreference와 -ErrorAction/-ErrorVariable](/post/powershell/erroractionpreference-command-powershell/wordcloud_hu_40455d890fde6e66.webp)
![[PowerShell] 67. trap — 레거시 예외 처리](/post/powershell/trap-legacy-exception-powershell/wordcloud_hu_2c888bc33eb7eff9.webp)
![[PowerShell] 68. Write-Verbose/Debug/Warning/Information](/post/powershell/write-verbose-debug-warning-powershell/wordcloud_hu_23130845671cc57.webp)
![[PowerShell] 69. Start-Transcript — 세션 기록](/post/powershell/start-transcript-session-record-powershell/wordcloud_hu_6bb3daff0bcf01e6.webp)
![[PowerShell] 70. Set-PSBreakpoint — 스크립트 디버깅](/post/powershell/set-psbreakpoint-debugging-powershell/wordcloud_hu_ce0529ad2ee3374e.webp)
![[PowerShell] 24. 자동 변수 총정리 — $_, $null, $Error, $LASTEXITCODE](/post/powershell/automatic-variables-powershell-underscore-null/wordcloud_hu_fed154e062984d25.webp)
![[PowerShell] 25. 공통 매개변수 — -Verbose/-Debug/-ErrorAction/-OutVariable](/post/powershell/common-parameters-verbose-debug-erroraction-powershell/wordcloud_hu_af2f3755ebbce019.webp)
![[PowerShell] 29. Write-Host vs Write-Output — 출력 스트림 오개념 정리](/post/powershell/write-host-vs-write-output-streams-powershell/wordcloud_hu_cd68b845a7c59766.webp)
![[PowerShell] 39. Test-Path — 경로 존재 확인](/post/powershell/test-path-command-check-path-powershell/wordcloud_hu_8b7ea3ad5761442.webp)