如何在后台 运行 一个 powershell 脚本并接收 toast 通知?
How to run a powershell script in the background and receive a toast notification?
this question 的答案解决了我问题的第一部分。
不幸的是,当我安排一个作业时 powershell window 被隐藏,我的脚本发送的任何 toast 通知都不会显示。
该脚本使用此脚本显示 toast 通知 (source):
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
当我以不隐藏的方式安排 powershell 脚本时,会显示通知,但每次触发脚本时,我都会短暂地看到一个 powershell window。
如何安排隐藏 powershell window 但显示 toast 通知的任务或作业?
Powershell.exe 是控制台应用程序。进程启动时,控制台 window 由 OS 自动创建。因此,处理 -WindowStyle Hidden 的 powershell.exe 代码在控制台 window 打开后执行,因此会出现闪光灯。我喜欢使用 VB 脚本启动 PowerShell 以确保完全隐藏的体验
中,说ps-run_hidden.vbs
放
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
然后用它来 运行 你想要的命令,例如来自Windows'这样的计划任务
wscript "C:\Path\To\ps-run_hidden.vbs" "C:\Other\Path\To\your-script.ps1"
您现在可以 运行 任务而不会看到任何闪烁 windows。
您可以 运行 它作为后台作业,这样就不会 window 弹出并且您会显示您的消息,
注意:在 Start-Job 之外声明的任何变量都需要像 $using:variable
一样使用。如果在 Start-Job 之外声明了 $pid,您将使用 $using:pid.
Start-Job -ScriptBlock {
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $using:pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
} | Wait-Job | Remove-Job
在 ps1 文件中包含上述代码后,只需调用以下行即可。它不会显示 powershell window 并向您提供通知 window.
powershell -File myScript.ps1 -WindowStyle Hidden
this question 的答案解决了我问题的第一部分。
不幸的是,当我安排一个作业时 powershell window 被隐藏,我的脚本发送的任何 toast 通知都不会显示。 该脚本使用此脚本显示 toast 通知 (source):
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
当我以不隐藏的方式安排 powershell 脚本时,会显示通知,但每次触发脚本时,我都会短暂地看到一个 powershell window。
如何安排隐藏 powershell window 但显示 toast 通知的任务或作业?
Powershell.exe 是控制台应用程序。进程启动时,控制台 window 由 OS 自动创建。因此,处理 -WindowStyle Hidden 的 powershell.exe 代码在控制台 window 打开后执行,因此会出现闪光灯。我喜欢使用 VB 脚本启动 PowerShell 以确保完全隐藏的体验
中,说ps-run_hidden.vbs
放
Set objShell = CreateObject("Wscript.Shell")
Set args = Wscript.Arguments
For Each arg In args
objShell.Run("powershell -windowstyle hidden -executionpolicy bypass -noninteractive ""&"" ""'" & arg & "'"""),0
Next
然后用它来 运行 你想要的命令,例如来自Windows'这样的计划任务
wscript "C:\Path\To\ps-run_hidden.vbs" "C:\Other\Path\To\your-script.ps1"
您现在可以 运行 任务而不会看到任何闪烁 windows。
您可以 运行 它作为后台作业,这样就不会 window 弹出并且您会显示您的消息,
注意:在 Start-Job 之外声明的任何变量都需要像 $using:variable
一样使用。如果在 Start-Job 之外声明了 $pid,您将使用 $using:pid.
Start-Job -ScriptBlock {
Add-Type -AssemblyName System.Windows.Forms
$global:balloon = New-Object System.Windows.Forms.NotifyIcon
$path = (Get-Process -id $using:pid).Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = [System.Windows.Forms.ToolTipIcon]::Warning
$balloon.BalloonTipText = 'What do you think of this balloon tip?'
$balloon.BalloonTipTitle = "Attention $Env:USERNAME"
$balloon.Visible = $true
$balloon.ShowBalloonTip(5000)
} | Wait-Job | Remove-Job
在 ps1 文件中包含上述代码后,只需调用以下行即可。它不会显示 powershell window 并向您提供通知 window.
powershell -File myScript.ps1 -WindowStyle Hidden