如何将 procesname 参数从 vbscript 传递给 powershell

howto pass procesname parameter to powershell form vbs script

如果创建了一个 VBS 脚本来监视新创建的进程,如 notepad.exe 或 calc.exe。当找到新的记事本或计算程序时,我喜欢在 powershell 中使用找到的程序名称做一些事情。

当我手动编辑 VBS 文件中的进程名称时,我创建的 VBS 脚本工作正常,但是当我尝试 use/pass 进程名称形式 vbs (strName) 到 powershell 时,我希望看到进程名称 (notepad.exe),但 powershell window 显示 "strName".

我在互联网上到处寻找解决方案,但没有结果。


我的 VBS 文件 [start.vbs]

来源: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: 

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

我的 Powershell 文件 [GetParamFromVBS.ps1]

来源: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

有人有什么想法吗?

变量不是字符串文字。

所以像这样

a = "a string literal" & aVariableContainingAString

变化:

WshShell.Run("Powershell.exe -file .\GetParamFromVBS.ps1 ""strName"" ")

收件人:

WshShell.Run("Powershell.exe -file .\GetParamFromVBS.ps1 '" & strName & "'")