1

I have a very basic HTA form with a checkbox and a button. I am trying to pass the checkbox status using VBScript in my HTA to a PowerShell script, which is called when the button is clicked. Unfortunately, I am unable to pass the value of the parameter through. It keeps coming across as empty.

Code in 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>

Powershell script:

#Param([Parameter(Mandatory=$true)][bool]$isTestCheckBoxChecked)

Write-host "The value is '$isTestCheckBoxChecked'"

The output I get is:

"The value is ''" 

Any guidance will be appreciated.

1 Answer 1

4

Three things:

  1. Don't use Set on the following statement. It's just a string, not an object, so using Set here should throw an error.

    ' Incorrect
    Set scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
    
    ' Correct
    scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & isTestCheckBoxChecked
    
  2. Your Param statement in PowerShell is commented out (#Param). Maybe this is just a typo when posting your question.

  3. After you uncomment your Param statement, you'll get an error about converting from a string to a bool. PowerShell accepts booleans in the format $false/$true or 0/1 for False/True values, respectively. So, you have two options:

    ' Prefix the boolean with a '$'
    scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked $" & isTestCheckBoxChecked
    
    ' Or, convert the boolean to a 0/1 (False/True)
    scriptPath = ".\TestPowershell.ps1 -isTestCheckBoxChecked " & Abs(isTestCheckBoxChecked)
    
Sign up to request clarification or add additional context in comments.

1 Comment

Bond! You are truely a bond... Thank you so much. Lessions learned here, Bool values can be passed using $ as the prefix!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.