4W4K3
4W4K3

Reputation: 23

Launch VBScript from HTA and pass variables

I'm fairly new to javascript and HTAs but here's what I have so far:

  <HTML> 
  <HEAD> 
  <title>Create Drive</title>
    <HTA:APPLICATION
     APPLICATIONNAME="CreateDrive"
     VERSION="1.0"
     SINGLEINSTANCE="yes"/>
    <SCRIPT language="JavaScript">
    window.resizeTo(400,300)
      function WriteFile() {
        var fso  = new ActiveXObject("Scripting.FileSystemObject"); 
        var fh = fso.CreateTextFile("output.txt", true); 
        fh.WriteLine(userinfo.UN.value + ' ' + userinfo.FN.value + ' ' + userinfo.LN.value);
        fh.Close(); 
      }
    </SCRIPT>
  </HEAD>
  <BODY>
    <FORM name="userinfo">
      <P>User Name: <INPUT name="UN" type="text"></P>
      <P>First Name: <INPUT name="FN" type="text"></P>
      <P>Last Name: <INPUT name="LN" type="text"></P>
      <P><INPUT type="button" value="Create Drive" onclick="WriteFile();"></P>
    </FORM>
    </BODY>
</HTML>

My goal is to use this HTA to create network drives for new user accounts. Previously, to accomplish this, a .bat was used that referenced a VBScript and the user entered three values (user, first, and last name) which were passed along to the .vbs and created the drive:

cscript /nologo newdrive.vbs q:\users\%1 /DFS:yes /server:server1 /server2:server2 /userfirstname:%2 /userlastname:%3 /quiet:no

I would like to access this VBScript directly from my HTA and pass the values of UN, FN, and LN. I'm currently saving the values entered into a text file, but I appreciate any info on completing this a better way.

Thanks for your assistance.

Upvotes: 2

Views: 4202

Answers (1)

Bond
Bond

Reputation: 16311

I'm using VBScript for this example. You could just as easily do this in JavaScript, however.

Sub WriteFile()

    ' Retrieve the form values to be passed as args...
    a1 = userinfo.UN.value
    a2 = userinfo.FN.value
    a3 = userinfo.LN.value

    With CreateObject("WScript.Shell")

        ' Run the script, passing along the form values...
        .Run "wscript.exe newdrive.vbs " & a1 & " " & a2 & " " & a3

    End With

End Sub

In your newdrive.vbs script, you would retrieve these values using the WScript.Arguments collection:

strUser  = WScript.Arguments(0)
strFirst = WScript.Arguments(1)
strLast  = WScript.Arguments(2)

Upvotes: 1

Related Questions