You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Bat脚本中内嵌执行PowerShell气球通知代码?

问题原因及解决办法

你的问题核心是bat脚本中双引号的转义冲突:
当你在bat里用powershell -Command "..."包裹PowerShell代码时,内层PowerShell代码里的双引号会被cmd解析为外层引号的闭合标记,导致PowerShell收到的代码丢失了字符串的引号包裹,进而触发一系列语法错误(比如把System.Windows.Forms当成未定义的表达式而非字符串参数)。

解决方式(二选一即可)

方式1:将PowerShell代码中的双引号替换为单引号

PowerShell中单引号和双引号均可用于定义字符串,这里不需要变量展开,单引号完全适用,且不会和bat的外层双引号冲突:

powershell -Command "& {[void][reflection.assembly]::loadwithpartialname('System.Windows.Forms'); [reflection.assembly]::loadwithpartialname('System.Drawing'); $notify = new-object system.windows.forms.notifyicon; $notify.icon = [System.Drawing.SystemIcons]::Information; $notify.visible = $true; $notify.showballoontip(10,'Copied!','Map names have been copied to the clipboard',[system.windows.forms.tooltipicon]::None);}"

方式2:对PowerShell代码中的双引号进行转义

在bat脚本中,用``"`(PowerShell的转义符,bat中无需额外转义)来转义内层双引号,确保cmd不会提前闭合外层引号:

powershell -Command "& {[void][reflection.assembly]::loadwithpartialname(`"System.Windows.Forms`"); [reflection.assembly]::loadwithpartialname(`"System.Drawing`"); $notify = new-object system.windows.forms.notifyicon; $notify.icon = [System.Drawing.SystemIcons]::Information; $notify.visible = $true; $notify.showballoontip(10,`"Copied!`",`"Map names have been copied to the clipboard`",[system.windows.forms.tooltipicon]::None);}"

额外优化(可选)

loadwithpartialname属于PowerShell中已弃用的API,建议改用Add-Type加载程序集,代码更简洁且符合现代PowerShell规范:

powershell -Command "& {Add-Type -AssemblyName System.Windows.Forms, System.Drawing; $notify = New-Object System.Windows.Forms.NotifyIcon; $notify.Icon = [System.Drawing.SystemIcons]::Information; $notify.Visible = $true; $notify.ShowBalloonTip(10,'Copied!','Map names have been copied to the clipboard',[System.Windows.Forms.ToolTipIcon]::None);}"

内容的提问来源于stack exchange,提问作者genericuser234

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.06 05:40:25