运行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.cmd
和MyPS_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