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

以下文件位于同一文件夹中。
Testing.cmd
Toast notification.ps1
运行[Toast通知] .vbs

当我运行 Toast notification.ps1 时,它可以工作。但是,当我运行运行[Toast通知] .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文件未运行。 “ 路径”可用于运行 Testing.cmd ,但是我无法使其与.ps1文件一起使用。我该如何解决该问题?
以下是 Testing.cmd 文件:

(ncpa.cpl)
pwlsiynice 回答:如何通过同一文件夹中的.vbs文件运行.ps1文件?

运行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),true
Action.run "cmd /c " & chr(34) & Item2 & chr(34),true
Set Action = Nothing

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

,

您不需要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,True)
Ret = Ws.run("cmd /c ncpa.cpl",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。它甚至允许通过批处理将参数传递给脚本

批次:

@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
}
本文链接:https://www.f2er.com/3112797.html

大家都在问