gpt4 book ai didi

powershell - 如何在后台运行 powershell 脚本并接收 toast 通知?

转载 作者:行者123 更新时间:2023-12-03 00:57:46 26 4
gpt4 key购买 nike

this question的答案解决我问题的第一部分。

不幸的是,当我安排一个隐藏 powershell 窗口的作业时,我的脚本发送的任何 toast 通知都不会显示。
该脚本使用此脚本 ( source ) 显示 toast 通知:

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 窗口但显示 toast 通知的任务或作业?

最佳答案

Powershell.exe 是一个控制台应用程序。进程启动时,操作系统会自动创建控制台窗口。因此,处理 -WindowStyle Hidden 的 powershell.exe 代码在控制台窗口打开后执行,因此是 flash。我喜欢使用 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"
您现在可以运行任务而不会看到任何闪烁的窗口。

关于powershell - 如何在后台运行 powershell 脚本并接收 toast 通知?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60416813/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com