none
Удаление DHCP RRS feed

  • Вопрос

  • Забавная задача. Был старый домен с DHCP в VLAN0, сейчас поставили и настроили новый домен с кучкой субдоменов и т.д. С убдоменами всё нормально, они в отдельных VLAN и там DHCP поднято. Центральный домен в VLAN0. И там DHCP пока не поднять ибо старых клиентов штук 200.

    Цель следующая, заменить на всех старых компах DHCP на static с сохранением IP.

    Скрипт отрабатывает в сетке и меняет IP и маску, но отказывается менять DNS и Getawey.

    Что я написал не так.

    On Error Resume Next
     
    strSubnetPrefix = "192.168.0."
    intBeginSubnet = 50
    intEndSubnet = 250
    arrSubnetMask = Array("255.255.255.0")
    arrDefaultGateways = Array("192.168.0.3")
    arrGatewayCostMetrics = Array(1)
    arrDNSServers = Array("192.168.0.4")
    For i = intBeginSubnet To intEndSubnet
      intEnableReturn = 1
      strComputer = strSubnetPrefix & i
      Set objShell = CreateObject("WScript.Shell")
      Set objScriptExec = objShell.Exec("ping -n 2 -w 1000 " & strComputer)
      strPingResults = LCase(objScriptExec.StdOut.ReadAll)
      If InStr(strPingResults, "ttl=128") Then
         Set objWMIService = GetObject("winmgmts:" _
         & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
        If Err = 0 Then
            Set colNicConfigs = objWMIService.ExecQuery _
           ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE " & _
           "IPEnabled = True")
          For Each objNicConfig In colNicConfigs
             If objNicConfig.DHCPEnabled Then
                arrIpAddress = Array ("192.168.0."&i)
       intEnableReturn = objNicConfig.EnableStatic(arrIpAddress, arrSubnetMask)
       intDNSServers = objNicConfig.SetDNSServerSearchOrder(arrDNSServers)
       intGatewaysReturn = objNicConfig.SetGateways(arrDefaultGateways,arrGatewayCostMetrics)
                WScript.Echo VbCrLf & "Network Adapter" & objNicConfig.Index & "xx" & intEnableReturn & "cc" & intGatewaysReturn & "vv" & intDNSServers 
             Else
                WScript.Echo "already static"
             End If
          Next
        Else
          Wscript.Echo "    Error: Unable to connect to WMI."
          Err.Clear
        End If
      Else
        WScript.Echo VbCrLf & "Host Name: " & strComputer & VbCrLf & "    Unable to connect."
      End If
    Next

    30 января 2014 г. 10:20

Ответы

  • On Error Resume Next 
     
    ' Get argument 
    Set colArgs = WScript.Arguments 
    strDHCP = LCase(colArgs(0)) 
    Select Case strDHCP 
    Case "true" blnDHCP = True 
    Case "false" blnDHCP = False 
     
    Case Else WScript.Echo "Please enter the script name followed by a " & _ 
    "space and true for DHCP or false for Static IP" & _ 
    VbCrLf & "For Static IP you must enter false then a space," & _ 
    VbCrLf & "the ip address then a space, and the gateway address" _ 
    : WScript.Quit 
    End Select 
     
    Set objWMIService = GetObject("winmgmts:") 
    'Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
    Set colNicConfigs = objWMIService.ExecQuery _ 
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
    ' If the office uses DHCP, call SetDHCP. 
    If blnDHCP = True Then 
    WScript.Echo "You are disconnecting from the restaurant" & _ 
    VbCrLf & "Enabling DHCP on all network adapters ..." 
    SetDHCP 
     
    ' If the office uses static IP, call SetStaticIP. 
    Else 
    WScript.Echo "You are connecting to " & colArgs(1) & _ 
    VbCrLf & "Enabling static IP addressing on all network adapters ..." 
    SetStaticIP 
    End If 
     
     
    '****************************************************************************** 
    Sub SetDHCP 
     
     
     
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & "Network Adapter " & objNicConfig.Index & _ 
    VbCrLf & "  " & objNicConfig.Description & VbCrLf 
    If Not objNicConfig.DHCPEnabled Then 
    WScript.Echo "  Attempting to enable DHCP ..." 
    intReturn = objNicConfig.EnableDHCP 
    If intReturn = 0 Then 
    WScript.Echo "  DHCP enabled." 
    Else 
    WScript.Echo "  Unable to enable DHCP." & " Error #:" & intReturn 
    End If 
    intDNS = objNicConfig.SetDNSServerSearchOrder(null) 
    Err.Clear 
    Else 
    WScript.Echo "  DHCP already enabled" & VbCrLf & _ 
    "  DHCP Server:             " & objNicConfig.DHCPServer 
    Err.Clear 
    End If 
     
    Next 
    GetSettings 
     
    End Sub 
     
    '****************************************************************************** 
    Sub SetStaticIP 
     
     
    arrIPAddresses = Array(colArgs(1)) 
    arrSubnetMasks = Array("255.255.255.0") 
    arrDefaultGateways = Array(colArgs(2)) 
    arrGatewayCostMetrics = Array(1) 
    arrDNSServers = Array("10.5.0.8") 
     
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & "Network Adapter " & objNicConfig.Index & VbCrLf & _ 
    "  " & objNicConfig.Description & VbCrLf 
    'If objNicConfig.DHCPEnabled Then 
    WScript.Echo "  Attempting to disable DHCP and enable static IP ..." 
    intStaticReturn = objNicConfig.EnableStatic(arrIPAddresses, arrSubnetMasks) 
    If intStaticReturn = 0 Then 
    WScript.Echo "  DHCP disabled, static IP address enabled." 
    Set objNicChanged = objWMIService.Get _ 
    ("Win32_NetworkAdapterConfiguration.Index=" & objNicConfig.Index) 
    intGatewaysReturn = objNicChanged.SetGateways(arrDefaultGateways, _ 
    arrGatewayCostMetrics) 
    intDNSServers = objNicChanged.SetDNSServerSearchOrder(arrDNSServers) 
    If intGatewaysReturn = 0 Then 
    WScript.Echo "    Assigned new default gateways." 
    Else 
    WScript.Echo "    Unable to assign default gateways." 
    End If 
    If intDNSServers = 0 Then 
    WScript.Echo "    Assigned new DNS servers." 
    Else 
    WScript.Echo "    Unable to assign DNS servers." 
    End If 
    Err.Clear 
    Else 
    WScript.Echo "  Unable to enable static IP address." & "Error #:" & intStaticReturn 
    Err.Clear 
    End If 
    'Else 
    'WScript.Echo "  Static IP address already enabled." 
    'End If 
    Next 
    GetSettings 
     
    End Sub 
     
    '****************************************************************************** 
    Sub GetSettings 
     
    Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM " & _ 
    "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
     
    WScript.Echo VbCrLf & "Settings after operation:" 
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & _ 
    "Network Adapter " & objNicConfig.Index & VbCrLf & _ 
    "  " & objNicConfig.Description & VbCrLf & VbCrLf & _ 
    "  DHCP Enabled:            " & objNicConfig.DHCPEnabled 
    strIPAddresses = "" 
    If Not IsNull(objNicConfig.IPAddress) Then 
    For Each strIPAddress In objNicConfig.IPAddress 
    strIPAddresses = strIPAddresses & VbCrLf & _ 
    "                           " & strIPAddress 
    Next 
    End If 
    WScript.Echo "  IP Addresses: " & strIPAddresses 
    strIPSubnets = "" 
    If Not IsNull(objNicConfig.IPSubnet) Then 
    For Each strIPSubnet In objNicConfig.IPSubnet 
    strIPSubnets = strIPSubnets & VbCrLf & _ 
    "                           " & strIPSubnet 
    Next 
    End If 
    WScript.Echo "  Subnet Masks: " & strIPSubnets 
    strDefaultIPGateways = "" 
    If Not IsNull(objNicConfig.DefaultIPGateway) Then 
    For Each strDefaultIPGateway In objNicConfig.DefaultIPGateway 
    strDefaultIPGateways = strDefaultIPGateways & VbCrLf & _ 
    "                           " & strDefaultIPGateway 
    Next 
    End If 
    WScript.Echo "  Default Gateways: " & strDefaultIPGateways 
    strGatewayCostMetrics = "" 
    If Not IsNull(objNicConfig.GatewayCostMetric) Then 
    For Each strGatewayCostMetric In objNicConfig.GatewayCostMetric 
    strGatewayCostMetrics = strGatewayCostMetrics & VbCrLf & _ 
    "                           " & strGatewayCostMetric 
    Next 
    End If 
    WScript.Echo "  Gateway Cost Metrics: " & strGatewayCostMetrics 
    strDNSServerSearchOrder = "" 
    If Not IsNull(objNicConfig.DNSServerSearchOrder) Then 
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder 
    strDNSServerSearchOrder = strDNSServerSearchOrder & VbCrLf & _ 
    "                           " & strDNSServer 
    Next 
    End If 
    WScript.Echo "  DNS Server Search Order: " & strDNSServerSearchOrder 
    Next 
     
    End Sub


    Have a nice day !!!

    • Помечено в качестве ответа Zhenya P. Zverev 13 марта 2014 г. 11:34
    5 февраля 2014 г. 6:02

Все ответы

  • On Error Resume Next 
     
    ' Get argument 
    Set colArgs = WScript.Arguments 
    strDHCP = LCase(colArgs(0)) 
    Select Case strDHCP 
    Case "true" blnDHCP = True 
    Case "false" blnDHCP = False 
     
    Case Else WScript.Echo "Please enter the script name followed by a " & _ 
    "space and true for DHCP or false for Static IP" & _ 
    VbCrLf & "For Static IP you must enter false then a space," & _ 
    VbCrLf & "the ip address then a space, and the gateway address" _ 
    : WScript.Quit 
    End Select 
     
    Set objWMIService = GetObject("winmgmts:") 
    'Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") 
    Set colNicConfigs = objWMIService.ExecQuery _ 
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
    ' If the office uses DHCP, call SetDHCP. 
    If blnDHCP = True Then 
    WScript.Echo "You are disconnecting from the restaurant" & _ 
    VbCrLf & "Enabling DHCP on all network adapters ..." 
    SetDHCP 
     
    ' If the office uses static IP, call SetStaticIP. 
    Else 
    WScript.Echo "You are connecting to " & colArgs(1) & _ 
    VbCrLf & "Enabling static IP addressing on all network adapters ..." 
    SetStaticIP 
    End If 
     
     
    '****************************************************************************** 
    Sub SetDHCP 
     
     
     
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & "Network Adapter " & objNicConfig.Index & _ 
    VbCrLf & "  " & objNicConfig.Description & VbCrLf 
    If Not objNicConfig.DHCPEnabled Then 
    WScript.Echo "  Attempting to enable DHCP ..." 
    intReturn = objNicConfig.EnableDHCP 
    If intReturn = 0 Then 
    WScript.Echo "  DHCP enabled." 
    Else 
    WScript.Echo "  Unable to enable DHCP." & " Error #:" & intReturn 
    End If 
    intDNS = objNicConfig.SetDNSServerSearchOrder(null) 
    Err.Clear 
    Else 
    WScript.Echo "  DHCP already enabled" & VbCrLf & _ 
    "  DHCP Server:             " & objNicConfig.DHCPServer 
    Err.Clear 
    End If 
     
    Next 
    GetSettings 
     
    End Sub 
     
    '****************************************************************************** 
    Sub SetStaticIP 
     
     
    arrIPAddresses = Array(colArgs(1)) 
    arrSubnetMasks = Array("255.255.255.0") 
    arrDefaultGateways = Array(colArgs(2)) 
    arrGatewayCostMetrics = Array(1) 
    arrDNSServers = Array("10.5.0.8") 
     
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & "Network Adapter " & objNicConfig.Index & VbCrLf & _ 
    "  " & objNicConfig.Description & VbCrLf 
    'If objNicConfig.DHCPEnabled Then 
    WScript.Echo "  Attempting to disable DHCP and enable static IP ..." 
    intStaticReturn = objNicConfig.EnableStatic(arrIPAddresses, arrSubnetMasks) 
    If intStaticReturn = 0 Then 
    WScript.Echo "  DHCP disabled, static IP address enabled." 
    Set objNicChanged = objWMIService.Get _ 
    ("Win32_NetworkAdapterConfiguration.Index=" & objNicConfig.Index) 
    intGatewaysReturn = objNicChanged.SetGateways(arrDefaultGateways, _ 
    arrGatewayCostMetrics) 
    intDNSServers = objNicChanged.SetDNSServerSearchOrder(arrDNSServers) 
    If intGatewaysReturn = 0 Then 
    WScript.Echo "    Assigned new default gateways." 
    Else 
    WScript.Echo "    Unable to assign default gateways." 
    End If 
    If intDNSServers = 0 Then 
    WScript.Echo "    Assigned new DNS servers." 
    Else 
    WScript.Echo "    Unable to assign DNS servers." 
    End If 
    Err.Clear 
    Else 
    WScript.Echo "  Unable to enable static IP address." & "Error #:" & intStaticReturn 
    Err.Clear 
    End If 
    'Else 
    'WScript.Echo "  Static IP address already enabled." 
    'End If 
    Next 
    GetSettings 
     
    End Sub 
     
    '****************************************************************************** 
    Sub GetSettings 
     
    Set colNicConfigs = objWMIService.ExecQuery("SELECT * FROM " & _ 
    "Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
     
    WScript.Echo VbCrLf & "Settings after operation:" 
    For Each objNicConfig In colNicConfigs 
    WScript.Echo VbCrLf & _ 
    "Network Adapter " & objNicConfig.Index & VbCrLf & _ 
    "  " & objNicConfig.Description & VbCrLf & VbCrLf & _ 
    "  DHCP Enabled:            " & objNicConfig.DHCPEnabled 
    strIPAddresses = "" 
    If Not IsNull(objNicConfig.IPAddress) Then 
    For Each strIPAddress In objNicConfig.IPAddress 
    strIPAddresses = strIPAddresses & VbCrLf & _ 
    "                           " & strIPAddress 
    Next 
    End If 
    WScript.Echo "  IP Addresses: " & strIPAddresses 
    strIPSubnets = "" 
    If Not IsNull(objNicConfig.IPSubnet) Then 
    For Each strIPSubnet In objNicConfig.IPSubnet 
    strIPSubnets = strIPSubnets & VbCrLf & _ 
    "                           " & strIPSubnet 
    Next 
    End If 
    WScript.Echo "  Subnet Masks: " & strIPSubnets 
    strDefaultIPGateways = "" 
    If Not IsNull(objNicConfig.DefaultIPGateway) Then 
    For Each strDefaultIPGateway In objNicConfig.DefaultIPGateway 
    strDefaultIPGateways = strDefaultIPGateways & VbCrLf & _ 
    "                           " & strDefaultIPGateway 
    Next 
    End If 
    WScript.Echo "  Default Gateways: " & strDefaultIPGateways 
    strGatewayCostMetrics = "" 
    If Not IsNull(objNicConfig.GatewayCostMetric) Then 
    For Each strGatewayCostMetric In objNicConfig.GatewayCostMetric 
    strGatewayCostMetrics = strGatewayCostMetrics & VbCrLf & _ 
    "                           " & strGatewayCostMetric 
    Next 
    End If 
    WScript.Echo "  Gateway Cost Metrics: " & strGatewayCostMetrics 
    strDNSServerSearchOrder = "" 
    If Not IsNull(objNicConfig.DNSServerSearchOrder) Then 
    For Each strDNSServer In objNicConfig.DNSServerSearchOrder 
    strDNSServerSearchOrder = strDNSServerSearchOrder & VbCrLf & _ 
    "                           " & strDNSServer 
    Next 
    End If 
    WScript.Echo "  DNS Server Search Order: " & strDNSServerSearchOrder 
    Next 
     
    End Sub


    Have a nice day !!!

    • Помечено в качестве ответа Zhenya P. Zverev 13 марта 2014 г. 11:34
    5 февраля 2014 г. 6:02
  • Спасибо. 
    5 февраля 2014 г. 6:09