【问题标题】:howto pass procesname parameter to powershell form vbs script如何从vbs脚本将进程名称参数传递给powershell
【发布时间】:2015-05-29 02:32:19
【问题描述】:

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

当我手动编辑 VBS 文件中的过程名称时,我创建的 VBS 脚本工作正常,但是当我尝试使用/传递过程名称形式 vbs (strName) 到 powershell 时,我希望看到过程名称 (notepad.exe),而是 powershell 窗口显示“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: 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

我的 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

有人有什么想法吗?

【问题讨论】:

    标签: powershell vbscript parameters


    【解决方案1】:

    变量不是字符串字面量。

    像这样的

    a = "a string literal" & aVariableContainingAString
    

    【讨论】:

      【解决方案2】:

      变化:

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

      收件人:

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

      【讨论】:

        猜你喜欢
        • 2014-01-07
        • 2020-11-19
        • 2011-08-01
        • 2015-06-07
        • 1970-01-01
        • 2012-05-05
        • 2016-07-08
        • 1970-01-01
        相关资源
        最近更新 更多