Option Explicit

' THE EPIC DHCP/DNS CONFIGURATION VB SCRIPT
' BY RATTLE 
' REMEMBER: BASIC PROGRAMMERS
'  USE CAPSLOCK
' USAGE: JUST CLICK IT.

Dim objWMIService,colNicConfigs
Dim strComputer,strPrompt,strIP,strAD,i,j,nic

Dim strTitle
strTitle = "dhcp-switch"


Function ErrorCode(j)
  Select Case j
    Case 64 ErrorCode = "Method not supported on this platform"
    Case 65 ErrorCode = "Unknown failure"
    Case 66 ErrorCode = "Invalid subnet mask"
    Case 67 ErrorCode = "An error occurred while processing an Instance that was returned"
    Case 68 ErrorCode = "Invalid input parameter"
    Case 69 ErrorCode = "More than 5 gateways specified"
    Case 70 ErrorCode = "Invalid IP  address"
    Case 71 ErrorCode = "Invalid gateway IP address"
    Case 72 ErrorCode = "An error occurred while accessing the Registry for the requested information"
    Case 73 ErrorCode = "Invalid domain name"
    Case 74 ErrorCode = "Invalid host name"
    Case 75 ErrorCode = "No primary/secondary WINS server defined"
    Case 76 ErrorCode = "Invalid file"
    Case 77 ErrorCode = "Invalid system path"
    Case 78 ErrorCode = "File copy failed"
    Case 79 ErrorCode = "Invalid security parameter"
    Case 80 ErrorCode = "Unable to configure TCP/IP service"
    Case 81 ErrorCode = "Unable to configure DHCP service"
    Case 82 ErrorCode = "Unable to renew DHCP lease"
    Case 83 ErrorCode = "Unable to release DHCP lease"
    Case 84 ErrorCode = "IP not enabled on adapter"
    Case 85 ErrorCode = "IPX not enabled on adapter"
    Case 86 ErrorCode = "Frame/network number bounds error"
    Case 87 ErrorCode = "Invalid frame type"
    Case 88 ErrorCode = "Invalid network number"
    Case 89 ErrorCode = "Duplicate network number"
    Case 90 ErrorCode = "Parameter out of bounds"
    Case 91 ErrorCode = "Access denied"
    Case 92 ErrorCode = "Out of memory"
    Case 93 ErrorCode = "Already exists"
    Case 94 ErrorCode = "Path, file or object not found"
    Case 95 ErrorCode = "Unable to notify service"
    Case 96 ErrorCode = "Unable to notify DNS service"
    Case 97 ErrorCode = "Interface not configurable"
    Case 98 ErrorCode = "Not all DHCP leases could be released/renewed"
   Case 100 ErrorCode = "DHCP not enabled on adapter"
  Case Else ErrorCode = "Unknown"
  End Select
End Function

Function Input(m,d)
  If InStr(LCase(WScript.FullName),"cscript") Then
    Dim tmp
    WScript.StdOut.Write m
    WScript.StdOut.Write " [" & d & "]: "
    tmp = WScript.StdIn.ReadLine
    WScript.StdOut.Write vbCRLF
    If tmp = "" Then tmp = d
    Input = tmp
  Else
    Input = InputBox(m,strTitle,d)
  End If
End Function
    
Sub Output(m,w)
  If InStr(LCase(WScript.FullName),"cscript") Then
    WScript.StdOut.WriteLine m
  Else
    MsgBox m,w,strTitle
  End If
End Sub

Function Question(m,d)
  If InStr(LCase(WScript.FullName),"cscript") Then
    Dim tmp
    tmp = ""
    While tmp = ""
      tmp = Input(m & " (Y/N)",d)
      If UCase(tmp) = "Y" Then
        Question = True 
      ElseIf UCase(tmp) = "N" Then
        Question = False
      Else 
      	tmp = ""
      End If
    Wend
  Else
    If MsgBox(m,vbYesNo+vbQuestion,strTitle)=vbYes Then Question = True Else _
    	 Question = False
  End If
End Function
  
Sub UpdateSelf(search,newIPstr)
   Const ForReading = 1, ForWriting = 2
   Dim fso, f, data, p1, p2
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set f = fso.OpenTextFile(WScript.ScriptFullName, ForReading, True)
   data = f.ReadAll
   p1 = InStr(data,"str"&search&"=")+5+Len(search)
   p2 = InStr(p1,data,Chr(34))
   data = Left(data,p1-1)+newIPstr+Mid(data,p2)
   f.Close
   Set f = fso.OpenTextFile(WScript.ScriptFullName, ForWriting, False)
   f.Write data
   f.Close
End Sub

strIP="10.0.0.2/255.0.0.0/10.0.0.2"
strAD=""

strComputer = "."
strPrompt = "Network interfaces found:" & vbCrLf

Set objWMIService = GetObject( _ 
  "winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colNicConfigs = objWMIService.ExecQuery ( _ 
  "SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

i = 0
For each nic in colNicConfigs
  i = i + 1
  strPrompt = strPrompt & "[" & CStr(i) & "] " & nic.Caption & vbCRLF
Next 

strPrompt = strPrompt & vbCrLf & "Please select an interface."

j = i
While Not IsNumeric(j) Or j >= i
  j = Input(strPrompt, strAD)
  If j = "" Then WScript.Quit 0
  j = CInt(j)-1
Wend

UpdateSelf "AD", CStr(j+1)

i = 0

For each nic in colNicConfigs
  If i = j Then 
    Dim m,w,E
    m = nic.Description & ":" & vbCRLF
    w = vbInformation
    E = "Y"
    
    if nic.DHCPEnabled Then E = "N"

    If not Question("Do you want to enable DHCP on the interface?",E) Then
      Dim info(2), gate, fnd
      fnd = 0
      w = vbInformation

      While fnd = 0
        j = Input("Enter static IP information" & vbCrLf & _
                  "Format: <IP>/<SUBNET>/<GATEWAY>", strIP)  
        If j = "" Then
           WScript.Quit 0
        Else 
          gate = j
        End If
        For i = 0 To 1
          fnd = InStr(gate,"/")
          If fnd <>  0 Then
            info(i) = Left(gate,fnd-1)
            gate = Mid(gate,fnd+1)
          Else 
            fnd = 0
            Exit For
          End If
        Next
      Wend
      
      UpdateSelf "IP", j
      
      j = nic.EnableStatic( Array(info(0)), Array(info(1)) )
      m = m & "  EnableStatic(" & info(0) & "," & info(1) & "): "
      If j > 1 Then
        w = vbExclamation        
        m = m & "Error (" & ErrorCode(j) & ")"
      Else 
        m = m & "Success"
      End If
      
      j = nic.SetGateways( Array(gate) )
      m = m & vbCRLF & "  SetGateways(" & gate & "): "
      If j > 1 Then
        w = vbExclamation
        m = m & "Error (" & ErrorCode(j) & ")"
      Else 
        m = m & "Success"
      End If
      
      j = nic.SetDNSServerSearchOrder( Array(gate) )
      m = m & vbCRLF & "  SetDNSServerSearchOrder(" & gate & "): "
      If j > 1 Then
        m = m & "Error (" & ErrorCode(j) & ")"
        w = vbExclamation
      Else 
        m = m & "Success"
      End If
      
    Else

      If nic.DHCPEnabled Then
        Call Output("DHCP is already enabled.", vbInformation)
        Exit For
      End If
      
      j = nic.EnableDHCP()
      m = m & "  EnableDHCP(): "
      If j > 1 Then
        w = vbExclamation
        m = m & "Error (" & ErrorCode(j) & ")"
      Else
        m = m & "Success."
      End If   
      
      j = nic.SetDNSServerSearchOrder( Null )
      m = m & vbCRLF & "  SetDNSServerSearchOrder(Null): "
      If j > 1 Then
        w = vbExclamation
        m = m & "Error (" & ErrorCode(j) & ")"
      Else 
        m = m & "Success"
      End If     
    End If
    
    Output m, w
    Exit For
    
  End If
  
  i = i + 1  
Next

