Link to home
Start Free TrialLog in
Avatar of vetjunkie
vetjunkie

asked on

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

I'm trying to assign IP address for stations 1-8. I have an 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 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>
<P>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><P>

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

</BODY>
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

What errors are you getting and where, is it when the netsh command runs or before?  Looks like the script is re-launching itself with run-as-admin if it isn't already so that shouldn't be an issue?

In which case does it work if you do the netsh command manually from cmd prompt, i.e. from above you'd have something like this... are all the interfaces named exactly the same like this?

netsh interface ip set address name="Local Area Connection" static 192.168.100.105 255.255.255.0
Avatar of vetjunkie
vetjunkie

ASKER

Hello Steve,
User generated imageI've attached a copy of the error: Object required:"WScript". It points to Line 27 Char 1 which seems to be this line :
Set WshShell = WScript.CreateObject("WScript.Shell")
the start of the run-as-admin. the error is generated when you click "SET IP" button after you select any of the workstation ID from the drop list.
I tested netsh interface ip set address name="Local Area Connection" static 192.168.100.105 255.255.255.0  command manually; It works only if you run the command with admin privlages
if I  comment out the run-as-admin routine
'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
It still generate the error: Object required:"WScript".
Ah OK.  remove the "wscript." bit out of this line as you don't have access to wscript when using HTA.

Set objShell = WScript.CreateObject("Wscript.Shell")

Set objShell = CreateObject("Wscript.Shell")

If there is anything else will look when on PC.

Steve
I removed it from the line. got the exact same  error:  Object required:"WScript".
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.