locked
請問如何解決使用Script在靜態IP位址和DHCP之間切換產生的問題 RRS feed

  • 問題

  • 當使用下列Script在Vista環境中,在靜態IP位址和DHCP之間切換時,

    當由static IP改為DHCP後再改為static IP時,檢視IP設定內容,其IP Addresssubnet mask

    都是空白,但在其他OS則正常。

     

    請問何故,該如何解決?

    On Error Resume Next
     
    ' Get argument to find out what office we're in and set a switch based on it.
    Set colArgs = WScript.Arguments
    strOffice = LCase(colArgs(0))
    Select Case strOffice
      Case "boston" blnDHCP = True
      Case "miami" blnDHCP = False
      Case "seattle" blnDHCP = True
      Case "dogpatch" blnDHCP = False
      Case Else WScript.Echo "Please enter the script name followed by a " & _
       "space and the name of a branch office." & _
       VbCrLf & "Valid names are: Boston, Miami, Seattle and Dogpatch." _
       : WScript.Quit
    End Select
     
    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 in the " & colArgs(0) & " branch office." & _
       VbCrLf & "Enabling DHCP on all network adapters ..."
      SetDHCP
    ' If the office uses static IP, call SetStaticIP.
    Else
      WScript.Echo "You are in the " & colArgs(0) & " branch office." & _
       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."
        End If
        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("192.168.0.12")
    arrSubnetMasks = Array("255.255.255.0")
    arrDefaultGateways = Array("192.168.0.1")
    arrGatewayCostMetrics = Array(1)
    arrDNSServers = Array("192.168.0.1")
     
    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."
          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
    
    2008年7月9日 上午 03:09