PowerShell 7에서 오래 실행되는 명령이 완료될 때 알림 받기
I’m happy to translate the article for you, but I’ll need the full text of the post (the content you’d like translated). Could you please paste the article’s body here? Once I have that, I’ll provide a Korean translation while preserving the source link, markdown formatting, and any code blocks or URLs unchanged.
작동 방식
Enter 키를 누르면 Enter가 기록됩니다. 명령이 끝난 후, 다음 프롬프트가 현재 시간을 저장된 타임스탬프와 비교합니다. 경과 시간이 임계값을 초과하면 BurntToast 모듈을 사용하여 토스트 알림이 트리거됩니다.
Source: …
단계
단계 1 – BurntToast 모듈 설치
Install-Module BurntToast
단계 2 – PowerShell 7 프로필 경로 찾기
$PROFILE
일반적인 출력:
C:\Users\<UserName>\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
단계 3 – 프로필을 편집기에서 열기
code $PROFILE
(원하는 편집기를 사용하세요.)
단계 4 – 모듈 가져오기
Import-Module BurntToast
참고: 이 가져오기 구문은 프로필의 가장 위에 있어야 합니다. 그렇지 않으면 아래 코드가 작동하지 않습니다.
단계 5 – 알림 로직 추가
Import-Module 라인 뒤에 다음 코드를 프로필 최상단에 붙여넣으세요:
$OriginalPrompt = (Get-Command prompt).ScriptBlock # 원래 프롬프트 캡처
# 알림에 로고가 필요합니다; PowerShell 아이콘을 사용합니다
$applogo = "C:\Program Files\PowerShell\7\assets\Powershell_av_colors.png"
$ExecutionNotifyThreshold = 3 # 초
$global:__commandStart = $null
function Format-Elapsed([TimeSpan]$timestamp) {
if ($timestamp.TotalSeconds -lt 60) {
return ('{0:0.0}s' -f $timestamp.TotalSeconds)
}
$parts = New-Object System.Collections.Generic.List[string]
if ($timestamp.Days -gt 0) { $parts.Add("{0}d" -f $timestamp.Days) }
if ($timestamp.Hours -gt 0) { $parts.Add("{0}h" -f $timestamp.Hours) }
if ($timestamp.Minutes -gt 0) { $parts.Add("{0}m" -f $timestamp.Minutes) }
$parts.Add("{0}s" -f $timestamp.Seconds)
return ($parts -join ' ')
}
function global:prompt {
if ($global:__commandStart) {
$elapsed = (Get-Date) - $global:__commandStart
if ($elapsed.TotalSeconds -ge $ExecutionNotifyThreshold) {
$lastCmd = (Get-History -Count 1).CommandLine
$text = @(
'PS7 Finished:'
"`$ $lastCmd"
("Took {0}" -f (Format-Elapsed $elapsed))
)
New-BurntToastNotification -Text $text -AppLogo $applogo
}
$global:__commandStart = $null
}
& $OriginalPrompt
}
$ExecutionNotifyThreshold(초)를 필요에 맞게 조정하세요.
단계 6 – Enter 키에서 시작 시간 기록
프로필 끝부분에 다음을 추가하여 다른 설정이 이를 덮어쓰지 않게 합니다:
Set-PSReadLineKeyHandler -Key Enter -ScriptBlock {
$global:__commandStart = Get-Date
[Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
파일을 저장하고 셸을 재시작하거나 다음을 실행합니다:
. $PROFILE
이제 임계값보다 오래 걸리는 명령을 실행하면 아래와 같은 토스트 알림이 표시됩니다:
Notes & Caveats
- This works fine with oh‑my‑posh, starship, or other prompt frameworks as long as you load the above code after the framework configures its prompt.
- It overrides the global
promptfunction. If you have another custom prompt, merge the logic accordingly. - The
Enterkey is hooked via PSReadLine. If you already have a customEnterhandler, combine the scripts.
Removing the behavior
If you decide you no longer want notifications, run:
Remove-PSReadLineKeyHandler -Key Enter
That’s it! You can now run long‑running commands without constantly watching the console. 🎉
