this question的答案解决了我问题的第一部分。
[不幸的是,当我安排作业以隐藏Powershell窗口时,不会显示我的脚本发送的任何吐司通知。该脚本使用该脚本显示吐司通知(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窗口。
我如何安排隐藏Powershell窗口但显示吐司通知的任务或作业?
Powershell.exe是一个控制台应用程序。进程启动时,操作系统会自动创建控制台窗口。因此,在打开控制台窗口(即刷新)之后,将执行处理-WindowStyle隐藏的powershell.exe代码。我喜欢使用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"
您现在可以运行任务而不会看到任何闪烁的窗口。
您可以在后台将其作为作业运行,这样就不会弹出窗口,并且显示您的消息,
注意:在Start-Job外部声明的任何变量都需要像$using:variable
一样使用。如果$ pid在Start-Job之外声明,则将使用$ 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窗口,并为您提供通知窗口。
powershell -File myScript.ps1 -WindowStyle Hidden