Reputation: 45
If have created a VBS script that watches for new created processes like notepad.exe or calc.exe. When a new notepad or calc proces is found i like to do something in powershell with the found procesname.
The VBS script i created works fine when I manually edit the procesname in the VBS file, but when i try to use/pass the procesname form vbs (strName) to powershell I expect to see the procesname (notepad.exe), but instead the powershell window shows "strName".
I've been all over the internet to find a solution, without result.
source: http://blogs.technet.com/b/heyscriptingguy/archive/2009/11/02/hey-scripting-guy-november-1-2009.aspx
arrProcesses = Array("calc.exe","notepad.exe")
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
i = 0
Set colMonitoredProcesses = objWMIService. ExecNotificationQuery _
("Select * From __InstanceCreationEvent Within 5 Where TargetInstance ISA 'Win32_Process'")
Do While i = 0
Set objLatestProcess = colMonitoredProcesses.NextEvent
strProcess = LCase(objLatestProcess.TargetInstance.Name)
For Each strName in arrProcesses
If strName = strProcess Then
''Run Powershell file
'source: https://stackoverflow.com/questions/19156178/passing-arguments-to-powershell
Set WshShell = CreateObject("WScript.Shell")
''this works great
WshShell.Run ("Powershell.exe -file .\GetParamFromVBS.ps1 ""notepad.exe"" ")
'this doesnt
WshShell.Run ("Powershell.exe -file .\GetParamFromVBS.ps1 ""strName"" ")
'OR
'Trying to work arround it, with no result :(
RUNPSVAR="(" + """" +"Powershell.exe -file .\GetParamFromVBS.ps1 & """"" +strName +"""""" +""& " " +"""" +")"
wscript.Echo RUNPSVAR
WshShell.Run ("Powershell.exe -file .\GetParamFromVBS.ps1 ""RUNPSVAR"" ")
End If
Next
Loop
source: How to pass command-line arguments to a PowerShell ps1 file
param($p1, $p2, $p3, $p4)
$Script:args=""
write-host "Num Args: " $PSBoundParameters.Keys.Count
foreach ($key in $PSBoundParameters.keys) {
$Script:args+= "`$$key=" + $PSBoundParameters["$key"] + " "
}
write-host $Script:args
pause
Anyone any ideas?
Upvotes: 1
Views: 2011
Reputation: 22861
Change:
WshShell.Run("Powershell.exe -file .\GetParamFromVBS.ps1 ""strName"" ")
To:
WshShell.Run("Powershell.exe -file .\GetParamFromVBS.ps1 '" & strName & "'")
Upvotes: 2
Reputation: 1
Variables aren't string literals.
So something like this
a = "a string literal" & aVariableContainingAString
Upvotes: 0