Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用VBScript调用带参数的PowerShell文件_Powershell_Vbscript_System Tray - Fatal编程技术网

如何使用VBScript调用带参数的PowerShell文件

如何使用VBScript调用带参数的PowerShell文件,powershell,vbscript,system-tray,Powershell,Vbscript,System Tray,大家好,新年快乐 我两天前刚刚了解了PowerShell,它显示了我的能力。我试图制作自定义气球提示,但不知道如何为每个可能的事件制作单独的ps1脚本。我需要的是一个带有参数的ps1 我在这个网站上找到了以下代码: 我通过PS窗口成功地调用它: . .\Invoke-BalloonTip.ps1 Invoke-BalloonTip -Message 'Message' -Title 'Title' -MessageType Info 但是,我需要从VBScript调用它。我试过: Set

大家好,新年快乐

我两天前刚刚了解了PowerShell,它显示了我的能力。我试图制作自定义气球提示,但不知道如何为每个可能的事件制作单独的ps1脚本。我需要的是一个带有参数的ps1

我在这个网站上找到了以下代码:

我通过PS窗口成功地调用它:

 . .\Invoke-BalloonTip.ps1
Invoke-BalloonTip -Message 'Message' -Title 'Title' -MessageType Info
但是,我需要从VBScript调用它。我试过:

Set objShell = CreateObject("Wscript.shell")
objShell.run("powershell -command . c:\PowerShellTest\Invoke-BalloonTip.ps1" & Invoke-BalloonTip -Message 'Invoked' -Title 'Invoked' -MessageType Info)

还有一些结果不令人满意。只有这两个示例在PS窗口运行时没有显示错误。在这两个示例中,PS窗口将短暂显示,不会显示错误消息,但不会显示气球提示


我肯定这是一个语法问题,但我不知道它可能是什么。欢迎提出任何想法或建议。

我认为问题在于原始的
调用balloottip.ps1
,因为它试图使用
System.Windows.Forms.ToolTipIcon
在可用之前键入(通过加载
System.Windows.Forms
)。在命令的前面添加它应该可以解决问题(尽管它看起来有点粗糙):


Invoke balloottip.ps1
可以在
PowerShell ISE
中直接工作,因为默认情况下已经加载了正确的程序集,但在
PowerShell.exe
VSCode

中就不是这样了,如果您的邮件或标题中有空格,则此操作无效。为了解决这个问题,我用单引号将字符串括起来:

objShell.run("powershell.exe -command ""& {Add-Type -AssemblyName System.Windows.Forms;. C:\PowerShellTest\Invoke-BalloonTip.ps1; Invoke-BalloonTip -Message ""'Message with Space'"" -Title ""'Title With Space:'"" -MessageType info}""")
您可以使用此代码

Dim strTitle, srtContext, strCommang

strTitle = "Title"
srtContext = "Text"

strCommang =    "powershell.exe -executionpolicy bypass -command " & _
                "[reflection.assembly]::loadwithpartialname('System.Windows.Forms')" & vbNewLine & _
                "[reflection.assembly]::loadwithpartialname('System.Drawing')" & vbNewLine & _
                "$notify = new-object system.windows.forms.notifyicon" & vbNewLine & _
                "$notify.icon = [System.Drawing.SystemIcons]::Information" & vbNewLine & _
                "$notify.visible = $true" & vbNewLine & _
                "$notify.showballoontip(10,'" & strTitle & "','" & srtContext & "',[system.windows.forms.tooltipicon]::None)"

Set objShell = CreateObject("Wscript.shell")
objShell.Run strCommang, 0
Set objShell = Nothing

-command
参数前面加上
-noexit
使窗口保持打开状态,这样您就可以看到它可能抛出的任何错误:
“powershell-noexit-command”“…”
谢谢,我会记住的。先生(女士?)是个天才!它工作得很好,我真的很感谢你的解释。现在进入下一期!可能要使用vbcrlf而不是vbNewLine。
objShell.run("powershell.exe -command ""& {Add-Type -AssemblyName System.Windows.Forms;. C:\PowerShellTest\Invoke-BalloonTip.ps1; Invoke-BalloonTip -Message ""'Message with Space'"" -Title ""'Title With Space:'"" -MessageType info}""")
Dim strTitle, srtContext, strCommang

strTitle = "Title"
srtContext = "Text"

strCommang =    "powershell.exe -executionpolicy bypass -command " & _
                "[reflection.assembly]::loadwithpartialname('System.Windows.Forms')" & vbNewLine & _
                "[reflection.assembly]::loadwithpartialname('System.Drawing')" & vbNewLine & _
                "$notify = new-object system.windows.forms.notifyicon" & vbNewLine & _
                "$notify.icon = [System.Drawing.SystemIcons]::Information" & vbNewLine & _
                "$notify.visible = $true" & vbNewLine & _
                "$notify.showballoontip(10,'" & strTitle & "','" & srtContext & "',[system.windows.forms.tooltipicon]::None)"

Set objShell = CreateObject("Wscript.shell")
objShell.Run strCommang, 0
Set objShell = Nothing