Vbs accesses the WMI read IP address and sets it to a static IP address.

Source: Internet
Author: User

Recently, the sub-IP address of the machine is unstable. During the convenience period, all the sub-IP addresses are set to static. If there are many machines, write a vbs script:

strComputer = "."Set objWMIService = GetObject( _    "winmgmts:\\" & strComputer & "\root\cimv2")Set colNics = objWMIService.ExecQuery _    ("Select * From Win32_NetworkAdapter " _        & "Where NetConnectionID = " & _        "'Local Area Connection'") For Each objNic in colNics    Set colNicConfigs = objWMIService.ExecQuery _      ("ASSOCIATORS OF " _          & "{Win32_NetworkAdapter.DeviceID='" & _      objNic.DeviceID & "'}" & _      " WHERE AssocClass=Win32_NetworkAdapterSetting")    For Each objNicConfig In colNicConfigs        For Each strIPAddress in objNicConfig.IPAddress            strIPAddress1 = strIPAddress        Exit For                Next     For Each strGateway in objNicConfig.DefaultIPGateway              strGateway1 = strGateway        Exit For        Next    For Each strSubnet in objNicConfig.IPSubnet               strSubnet1 = strSubnet         Exit For        Next    NextNextstrComputer1 = "."Set objWMIService1 = GetObject( _    "winmgmts:\\" & strComputer1 & "\root\cimv2")Set colNetAdapters = objWMIService1.ExecQuery _    ("Select * from Win32_NetworkAdapterConfiguration " _        & "where IPEnabled=TRUE")strGatewaymetric = Array(1)aSubnet = Array(strSubnet1)aIPAddress = Array(strIPAddress1)aGateway = Array(strGateway1)For Each objNetAdapter in colNetAdapters    errEnable = objNetAdapter.EnableStatic( _        aIPAddress , aSubnet)    errGateways = objNetAdapter.SetGateways(_        aGateway, strGatewaymetric)Next

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.