命令完成并且 Powershell 返回提示符时如何收到通知?

问题描述 投票:0回答:3

我正在做一些测试工作,其中一部分是定期向某些硬件写入文件。问题是,写入时间短则 20 秒,长则几分钟。盯着屏幕并等待它完成是一种巨大的时间浪费,所以我想知道是否有一种方法可以在命令已完成并且 Powershell 返回提示符?

powershell
3个回答
2
投票

您可以编辑您的

prompt
功能:

$function:prompt = @"
{0}
[console]::beep()
"@ -f $function:prompt.ToString()

如果您不介意分号分隔的命令,可以使用更简洁的版本(mklement0的赞美):

$function:prompt = "$function:prompt; [console]::beep()"

1
投票

命令完成时 PowerShell 发出警报 :

"Answer" | out-file response.txt
[console]::beep(500,300)

Powershell > [控制台]::嘟嘟声持续 1 分钟:

[console]::beep(500,60000)

另外 - 尝试使用“星号”警报:

[System.Media.SystemSounds]::Asterisk.Play()

0
投票

点来源:


# requires BurntToastNotification
# Install-Module -Name BurntToast
# https://github.com/Windos/BurntToast
# Make sure your windows notifications aren't snoozed

function prompt { 
    # .Link
    # https://go.microsoft.com/fwlink/?LinkID=225750
    # .ExternalHelp System.Management.Automation.dll-help.xml   
    if($global:PREEXEC_TIME){
        $script:runtime = ((date) - $global:PREEXEC_TIME).seconds
        if($script:runTime -gt 5){ 
            New-BurntToastNotification -Text "Finished command $$ Runtime: $runtime seconds."
        }
    }

    
    "PS (Runtime: $script:runTime ) $($executionContext.SessionState.Path.CurrentLocation)$('>' * ($nestedPromptLevel + 1)) ";
}

Set-PSReadLineKeyHandler Enter {    #  using this as a preexec hook
    $global:PREEXEC_TIME = date
    # Submit the command.
  [Microsoft.PowerShell.PSConsoleReadLine]::AcceptLine()
}
© www.soinside.com 2019 - 2024. All rights reserved.