Solved Make a drop down list to assign IP address using VBS and HTA

December 14, 2018 at 10:51:34
Specs: Windows 7
I'm trying to assign IP address for stations 1-8. I found HTA script to make a drop down list and based on the choice assign the IP address. I need to leave the gateway and DNS blank.
The code load the form with drop list but I get an error when trying to assign the IP address. Please Help
code below for WorkstationID3.HTA 
<HEAD>
<TITLE>Set Workstation Name and IP address</TITLE>
<HTA:APPLICATION ID="oMyApp"
APPLICATIONNAME="Set Workstation Name and IP address"
BORDER="Dialog"
CAPTION="Yes"
SCROLL="NO"
SHOWINTASKBAR="yes"
SINGLEINSTANCE="yes"
SYSMENU="Yes"
'WINDOWSTATE="maximize">
</HEAD>
<BODY BGCOLOR="#D4D0C8">
<SCRIPT LANGUAGE="VBScript">

Sub Window_OnLoad
Window.StationID.Focus
window.resizeTo 400,300
End Sub


Sub btn01_OnClick

Dim strIPAddress
Dim strSubnetMask

Set WshShell = WScript.CreateObject("WScript.Shell")
If WScript.Arguments.Length = 0 Then
Set ObjShell = CreateObject("Shell.Application")
ObjShell.ShellExecute "wscript.exe" _
, """" & WScript.ScriptFullName & """ RunAsAdministrator", , "runas", 1
WScript.Quit
End if

oElements = Window.StationID.SelectedIndex
strProduct = Window.StationID.Options(oElements).Text
strSubnetMask = "255.255.255.0"

Select Case strProduct
Case "Station01"
strIPAddress = "192.168.100.101"

Case "Station02"
strIPAddress = "192.168.100.102"

Case "Station03"
strIPAddress = "192.168.100.103"

Case "Station04"
strIPAddress = "192.168.100.104"

Case "Station05"
strIPAddress = "192.168.100.105"

Case "Station06"
strIPAddress = "192.168.100.106"

Case "Station07"
strIPAddress = "192.168.100.107"

Case "Station08"
strIPAddress = "192.168.100.108"

End Select


Set objShell = WScript.CreateObject("Wscript.Shell")
objShell.Run "netsh interface ip set address name=""Local Area Connection"" static " & strIPAddress & " " & strSubnetMask

Set objShell = Nothing

End Sub

Sub btn02_OnClick
Window.Close
End Sub
</SCRIPT>

<H2>Set Workstation ID</H2>

Please select the Workstation:
<SELECT NAME="StationID">
<OPTION>Station01</OPTION>
<OPTION>Station02</OPTION>
<OPTION>Station03</OPTION>
<OPTION>Station04</OPTION>
<OPTION>Station05</OPTION>
<OPTION>Station06</OPTION>
<OPTION>Station07</OPTION>
<OPTION>Station08</OPTION>

</SELECT>





<Input Type = "Button" Name = "btn01" VALUE = "Set Ip">
<Input Type = "Button" Name = "btn02" VALUE = "CLOSE">



</BODY>


See More: Make a drop down list to assign IP address using VBS and HTA

Reply ↓  Report •

✔ Best Answer
December 16, 2018 at 17:50:52
-2147024891 = 0x80070005 = E_ACCESSDENIED implies I messed up the elevation. I didn't catch it because I don't use UAC on my main machine. The Elevate() sub should be fixed now. I say "should be" because I'm still not going to turn on UAC to test.

How To Ask Questions The Smart Way



#1
December 15, 2018 at 23:58:21
Your big problem is trying to use WScript. The script isn't running in the WScript engine, so you can't use WScript objects. It's running in Internet Explorer. Also, you're better off using WMI to set the IP addresses. If your NETSH command fails for any reason, you won't get feedback, whereas you will with WMI.

<html>
<head>
  <hta:application id="oHTA" applicationName="SetIP" border="Dialog" scroll="no" singleInstance="yes" />
  <title>Set Workstation Name and IP address</title>
  <script type="text/vbscript">
    Sub Elevate()
      CreateObject("Shell.Application").ShellExecute "mshta", oHTA.commandLine & " /elevated", "", "runas"
      self.close
    End Sub

    Sub DoLoad()
      If InStr(oHTA.commandLine, " /elevated") = 0 Then _
        Elevate

      window.resizeTo 400,300
      Set oSId = document.getElementById("stationID")
      oSId.Focus

      For i = 1 To 8
        Set opt = document.createElement("option")
        opt.value = "192.168.100." & CStr(i + 100)
        opt.innerHTML = "Station" & Right("00" & i, 2)
        oSId.appendChild opt
      Next 'i
    End Sub

    Sub DoWork()
      Dim status, statTxt, compRet : Set status = document.getElementById("status")
      status.innerHTML = "Setting IP . . ."
      statTxt = "IP return: " & SetIP() & "<br />"

      status.innerHTML = statTxt & "Setting Name . . ."
      compRet = SetName()
      statTxt = statTxt & "Name return: " & compRet
      If compRet = 0 Then _
        statTxt = statTxt & " (Reboot required)"
      status.innerHTML = statTxt
    End Sub

    Function SetIP()
      Dim newIP : newIP = document.getElementById("stationID").value
      For Each net In GetObject("winmgmts:").ExecQuery("SELECT * FROM Win32_NetworkAdapter " & _
       "WHERE NetConnectionID = 'Local Area Connection'")
        For Each conf In net.Associators_(, "Win32_NetworkAdapterConfiguration")
          SetIP = conf.EnableStatic(Array(newIP), Array("255.255.255.0"))
        Next 'conf
      Next 'net
    End Function

    Function SetName()
      With document.getElementById("stationID")
        For Each com In GetObject("winmgmts:").InstancesOf("Win32_ComputerSystem")
          SetName = com.Rename(.options(.selectedIndex).text)
        Next 'com
      End With
    End Function
  </script>
</head>
<body style="background-color: #D4D0C8;" onload="DoLoad">
  <H2>Set Workstation ID</H2>
  <form action="#" onSubmit="javascript:return false;">
    <label for="stationID">Please select the Workstation:</label>
    <select id="stationID" style="width: 8em"></select>
    <button type="submit" onclick="DoWork">Set Ip</button>
    <br />
    <button type="button" onclick="self.close">CLOSE</button>
  </form>
  <div id="status">Ready</div>
</body>
</html>

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

#2
December 16, 2018 at 09:35:16
Razer2.3
Thank you for the help. I ran the code you made but I get an error when I click on "Set IP" button.
error on line: 29, Char: 7, Error: Object required: "status'


Reply ↓  Report •

#3
December 16, 2018 at 11:03:03
Well, it was working last night. Change the "status" object to "stat". (Change reflected in the code above)

How To Ask Questions The Smart Way

message edited by Razor2.3


Reply ↓  Report •

Related Solutions

#4
December 16, 2018 at 14:48:33
Razor2.3
Thank you again. I ran your new code, this time no error occurred however it shows a Return code: -2147024891
and the IP address does not change regardless of which station ID I pick.

Reply ↓  Report •

#5
December 16, 2018 at 17:50:52
✔ Best Answer
-2147024891 = 0x80070005 = E_ACCESSDENIED implies I messed up the elevation. I didn't catch it because I don't use UAC on my main machine. The Elevate() sub should be fixed now. I say "should be" because I'm still not going to turn on UAC to test.

How To Ask Questions The Smart Way


Reply ↓  Report •

#6
December 21, 2018 at 13:21:47
Thank you so much. It worked, You are awesome :-)
Is there a way to append the code to also change the computer name to match the station Id selected.?

message edited by VetJunkie


Reply ↓  Report •

#7
December 22, 2018 at 09:27:45
Yeah, sure. HTA updated.

How To Ask Questions The Smart Way


Reply ↓  Report •

#8
December 22, 2018 at 12:40:04
Razor2.3
Thank you again. I tested the updated code and it worked like a charm. YOU ARE THE MAN.

Very grateful for your help. You are awesome.


Reply ↓  Report •

Ask Question