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

1.4k views Asked by At

The following files are in the same folder.
Testing.cmd
Toast notification.ps1
Run [Toast notification].vbs

When I run Toast notification.ps1, it works. However, when I run Run [Toast notification].vbs, the .ps1 file is not run.
The following is the PowerShell script:

[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()

The following is the 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

When I double-click on the VBScript file, the .ps1 file is not run. The "Path" can be used to run Testing.cmd, but I cannot make it work with a .ps1 file. How can I fix the problem?
The following is the Testing.cmd file:

(ncpa.cpl)
3

There are 3 answers

0
Captain_Planet On BEST ANSWER

When you run the PowerShell script it should look more like this:

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

The chr(34) represents quotes in case your path contains spaces. I'd also recommend more error checking, and checking that the files exist etc. The above example hides the PowerShell and CMD windows too...

0
Hackoo On

You don't need 3 files to execute those commands, just, create one vbscript file. So, refer to this,

You can do something like 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
'-----------------------------------------------------------------------------------------------------------
4
Hel O'Ween On

There's also this nifty trick to start any PowerShell script with a generic batch (CMD). The trick is to name the batch like the PowerShell script, e.g. MyPS_Script.cmd and MyPS_Script.ps1. It even allows to pass arguments to the script via the batch

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 Script demonstration

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