Vba 使用参数从VBScript调用PowerShell

Vba 使用参数从VBScript调用PowerShell,vba,powershell,hta,Vba,Powershell,Hta,我有一个非常基本的HTA表单,带有一个复选框和一个按钮。我正在尝试使用HTA中的VBScript将复选框状态传递给PowerShell脚本,单击按钮时会调用该脚本。不幸的是,我无法传递参数的值。它总是让人觉得空荡荡的 HTA中的代码: <html> <head> <title>Test Form</title> <meta content="text/html; charset=utf-8" http-equiv=

我有一个非常基本的HTA表单,带有一个复选框和一个按钮。我正在尝试使用HTA中的VBScript将复选框状态传递给PowerShell脚本,单击按钮时会调用该脚本。不幸的是,我无法传递参数的值。它总是让人觉得空荡荡的

HTA中的代码:

<html>
  <head>
    <title>Test Form</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type" />
    <hta:application applicationname="Proof of concept version="1.0" />

    <script language="vbscript">
    Sub Resize()
      window.resizeTo 500,450
    End Sub

    Sub ExecutePowerShell()
      Dim oShell, scriptPath, appCmd, retVal, retData, isTestCheckBoxChecked

      'Collect value from input form
      isTestCheckBoxChecked = document.getElementByID("input_checkBoxTest").checked

      MsgBox isTestCheckBoxChecked

      Set oShell = CreateObject("Wscript.Shell")
      Set scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
      appCmd = "powershell.exe " & scriptPath
      retVal = oShell.Run(appCmd, 1, true)
      retData = document.parentwindow.clipboardData.GetData("text")

    End Sub
    </script>
  </head>

  <body onload="Resize()">
    <h1>Test Form:</h1>
        <div style="margin-top:10px; margin-bottom:30px;">
            The scipt does the following checks:
            <ul>
                <li><input name="input_checkBoxTest" type="checkbox" checked="checked"/> This is a test textbox</li>                
            </ul>
        </div>
    <br /><br />
    <input type="button" id="btn_execute" value="Execute" onclick="ExecutePowerShell()" />
    <br /><br />
  </body>
</html>
我得到的结果是:

"The value is ''" 
任何指导都将不胜感激。

三件事:

  • 不要在下面的语句中使用
    Set
    。它只是一个字符串,不是一个对象,所以在这里使用
    Set
    应该会抛出一个错误

    ”不正确
    Set scriptPath=“。\TestPowershell.ps1-isTestCheckBoxChecked”&isTestCheckBoxChecked
    “对
    scriptPath=“.\TestPowershell.ps1-isTestCheckBoxChecked”&isTestCheckBoxChecked
    
  • PowerShell中的
    Param
    语句将被注释掉(
    #Param
    )。也许这只是发布问题时的一个输入错误

  • 在取消对
    Param
    语句的注释后,您将得到一个关于从字符串转换为bool的错误。PowerShell分别为
    false/true
    0/1
    值接受格式为
    false/true
    的布尔值。因此,您有两种选择:

    ”在布尔值前面加一个“$”前缀
    scriptPath=“。\TestPowershell.ps1-isTestCheckBoxChecked$”&isTestCheckBoxChecked
    '或者,将布尔值转换为0/1(False/True)
    scriptPath=“.\TestPowershell.ps1-isTestCheckBoxChecked”&Abs(isTestCheckBoxChecked)
    
  • 三件事:

  • 不要在下面的语句中使用
    Set
    。它只是一个字符串,不是一个对象,所以在这里使用
    Set
    应该会抛出一个错误

    ”不正确
    Set scriptPath=“。\TestPowershell.ps1-isTestCheckBoxChecked”&isTestCheckBoxChecked
    “对
    scriptPath=“.\TestPowershell.ps1-isTestCheckBoxChecked”&isTestCheckBoxChecked
    
  • PowerShell中的
    Param
    语句将被注释掉(
    #Param
    )。也许这只是发布问题时的一个输入错误

  • 在取消对
    Param
    语句的注释后,您将得到一个关于从字符串转换为bool的错误。PowerShell分别为
    false/true
    0/1
    值接受格式为
    false/true
    的布尔值。因此,您有两种选择:

    ”在布尔值前面加一个“$”前缀
    scriptPath=“。\TestPowershell.ps1-isTestCheckBoxChecked$”&isTestCheckBoxChecked
    '或者,将布尔值转换为0/1(False/True)
    scriptPath=“.\TestPowershell.ps1-isTestCheckBoxChecked”&Abs(isTestCheckBoxChecked)
    

  • 纽带你真的是一条纽带。。。非常感谢你。在这里学习到,Bool值可以使用$作为前缀传递!纽带你真的是一条纽带。。。非常感谢你。在这里学习到,Bool值可以使用$作为前缀传递!
    "The value is ''"