如何通过同一文件夹中的 .vbs 文件 运行 .ps1 文件?

How do I run a .ps1 file via a .vbs file in the same folder?

以下文件在同一文件夹中。
Testing.cmd
吐司通知。ps1
运行 [Toast 通知].vbs

当我 运行 Toast notification.ps1 时,它起作用了。但是,当我 运行 运行 [Toast notification].vbs 时,.ps1 文件不是 运行。
以下是 PowerShell 脚本:

[reflection.assembly]::loadwithpartialname("System.Windows.Forms")
[reflection.assembly]::loadwithpartialname("System.Drawing")
# notify.icon type: Information, Warning or Error.
$notify = new-object system.windows.forms.notifyicon
$notify.icon = [System.Drawing.SystemIcons]::Information
$notify.visible = $true
$notify.showballoontip(10,"", "The CPU is hot.", 
[system.windows.forms.tooltipicon]::None)
$notify.dispose()

以下为VBScript:

Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Item1 = Path & "Toast notification.ps1" 
Item2 = Path & "Testing.cmd" 
Set Action = CreateObject("Wscript.shell")
Action.run ("powershell -executionpolicy bypass -file ""& Item1 &""")
Action.run ("""" & Item2 & ""), 0

当我双击 VBScript 文件时,.ps1 文件不是 运行。 “Path”可用于 运行Testing.cmd,但我无法使用 .ps1 文件。我该如何解决这个问题?
以下是Testing.cmd文件:

(ncpa.cpl)

当您 运行 PowerShell 脚本时,它应该看起来更像这样:

Option Explicit

Dim Path : Path = split(wscript.scriptFullName, wscript.scriptname)(0)
Dim Item1 : Item1 = Path & "Toast notification.ps1" 
Dim Item2 : Item2 = Path & "Testing.cmd" 
Dim Action : Set Action = CreateObject("Wscript.shell")
Action.run "powershell -executionpolicy bypass -file " & chr(34) & Item1 & chr(34), 0, true
Action.run "cmd /c " & chr(34) & Item2 & chr(34), 0, true
Set Action = Nothing

chr(34) 表示引号,以防您的路径包含空格。我还建议进行更多的错误检查,并检查文件是否存在等。上面的示例也隐藏了 PowerShell 和 CMD windows...

您不需要 3 个文件来执行这些命令,只需创建一个 vbscript 文件即可。 所以,参考this,

你可以这样做:

Option Explicit
Dim Ws,Ret,ByPassPSFile,PSFile
Set Ws = CreateObject("wscript.Shell")
ByPassPSFile = "cmd /c PowerShell.exe -ExecutionPolicy bypass -noprofile -file "
Call WritePSFile("Warning","10","'The CPU is hot !'","'The CPU is hot '","'Warning'","10")
Ret = Ws.run(ByPassPSFile & PSFile,0,True)
Ret = Ws.run("cmd /c ncpa.cpl",0,True)
'------------------------------------------------------------------------------------------------------------
Sub WritePSFile(notifyicon,time,title,text,icon,Timeout) 
Const ForWriting = 2
Dim fso,ts,strText
PSFile = Left(Wscript.ScriptFullName, InstrRev(Wscript.ScriptFullName, ".")) & "ps1"
Set fso = CreateObject("Scripting.FileSystemObject")
Set ts = fso.OpenTextFile(PSFile,ForWriting,True)
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Windows.Forms') | Out-Null;" & VbCrlF
strText = strText & "[reflection.assembly]::loadwithpartialname('System.Drawing') | Out-Null;" & VbCrlF 
strText = strText & "$notify = new-object system.windows.forms.notifyicon;" & VbCrlF
strText = strText & "$notify.icon = [System.Drawing.SystemIcons]::"& notifyicon &";" & VbCrlF 
strText = strText & "$notify.visible = $true;" 
strText = strText & "$notify.showballoontip("& time &","& title &","& text &","& icon &");" & VbCrlF 
strText = strText & "Start-Sleep -s " & Timeout &";" & VbCrlF
strText = strText & "$notify.Dispose()"
ts.WriteLine strText
End Sub
'-----------------------------------------------------------------------------------------------------------

还有一个巧妙的技巧可以使用通用批处理 (CMD) 启动任何 PowerShell 脚本。诀窍是像 PowerShell 脚本一样命名批处理,例如MyPS_Script.cmdMyPS_Script.ps1。它甚至允许通过 batch

将参数传递给脚本

批次:

@ECHO OFF

REM *****
REM * Wrapper CMD to start the PowerShell script of the same name
REM * I.e. if the script is named ServiceRestart.ps1, then the 
REM * CMD needs to be named ServiceRestart.cmd
REM *****

PowerShell.exe -Command "& '%~dpn0.ps1' %1 %2 %3 %4 %5 %6 %7"

PAUSE

PS脚本演示

ForEach ($Arg In $Args) {
    Write-Host "Arguments from cmd" $Arg
}