Submitted By: Nenad Zdravkovic

Enables DHCP and assign DNS servers to a computer.

Visual Basic
Edit|Remove
'***********************************************************************
'! Script name: AutoIP.vbs
'! Created on:  22. Feb, 2005
'! Powered by:  Nenad Zdravkovic
'! Copyright (C) 2004 EEDSoft [Energy Engine Design Soft] Corporation
'! All rights reserved
'! Description: Network Managment script
'! Version:     v1.0.0
'***********************************************************************
' The variable declarations is mandatory
Option Explicit

Const conExitCodeNone = 0
Const conExitCodeError = 2

'> Function name:       EnableDHCP
'> Purpose:             Enable DHCP obtained address on client
Function EnableDHCP
  Dim objWMIService, objNicConfig, colNicConfigs
  Dim strComputer
  Dim intReturn
  
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colNicConfigs = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

  For Each objNicConfig In colNicConfigs
    If Not objNicConfig.DHCPEnabled Then
      intReturn = objNicConfig.EnableDHCP
      If intReturn = 0 Then
        WScript.Echo VbCrLf & "DHCP enabled."
      Else
        WScript.Echo VbCrLf & "Unable to set DHCP obtained address."
        WScript.Quit conExitCodeError
      End If
    Else
      WScript.Echo "DHCP already enabled" & VbCrLf & "DHCP Server: " & objNicConfig.DHCPServer
    End If
  Next
End Function

'> Function name:       ObtainDNSAutomatically
'> Purpose:             Set DNS servers on auto config thru registry
Function ObtainDNSAutomatically()
  'Default error trapping mechanism
  On Error Resume Next
  ' Script specific constants
  Const conKeyPath = "HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\Interfaces\"
  Dim objWMIService, objNicConfig, colNicConfigs, objShell
  Dim strComputer, strSuccess
  Dim strAdapterIdentifier
  Dim strKeyPath
  Dim intReturn
  strComputer = "."
  Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
  Set colNicConfigs = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
  For Each objNicConfig In colNicConfigs
    strAdapterIdentifier = objNicConfig.SettingID
    strKeyPath = conKeyPath & strAdapterIdentifier & "\NameServer"
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.RegWrite strKeyPath, "", "REG_SZ"
  Next
End Function

'*****************************
' Starting point of the script
'*****************************
' Call main procedures
EnableDHCP
ObtainDNSAutomatically
' Exit from the script. No errors
WScript.Quit conExitCodeNone