WMI (VBScript) learning 2: Getting and modifying local connection configuration information

Source: Internet
Author: User

Main functions: Switch the gateway address between "192.168.0.120" and "192.168.0.8"

 

Note the following:
1. A function with a return value needs to declare a variable to receive its return value. Otherwise, if objitem. enablestatic (stripaddress, strsubnetmask) is not returned

Compiler error: parentheses are not allowed during the call process (sub ).

(Why ?)

 

2. There are three access methods for data whose data type is array:
Method 1: traverse the array in a loop

'For each stripaddress in objitem. IPaddress
'Wscript. Echo stripaddress
'Next

Method 2: Use the upper and lower mark of the array as the cyclic variable value range Traversal

For I = lbound (objitem. IPaddress )_
To ubound (objitem. IPaddress)

...

Next
Method 3: directly specify the array subscript, as shown in figure

Wscript. Echo strgateway (0)

 

Program defects:

The code is repeated before and after the local connection property is displayed. It can be replaced by a function.

 

'####################################### ### <Br/> '<case: find IP Address by macaddress, set static IP addres, S and change gateway between "192.168.0.120" and "192.168.0.8" <br/> 'connect to a local machine only, support can be found from MS-help: // Ms. msdnqtr. v90.en/wmisdk/WMI/wmi_tasks1_networking.htm <br/> 'for remote connection, please see ms-help: // Ms. msdnqtr. v90.en/wmisdk/WMI/connecting_to_wmi_on_a_remote_computer.htm <br/> 'Be careful, when we call WMI methods, if it has return, we must have a variable for recieving the returns <br/> strcomputer = ". "<br/> stripaddress = array (" 192.168.0.131 ") <br/> strsubnetmask = array (" 255.255.255.0 ") <br/> strgateway1 = array (" 192.168.0.120 ") <br/> strgateway2 = array ("192.168.0.8") <br/> strgatewaymetric = array (1) <br/> set ob1_miservice = GetObject ("winmgmts: // "& strcomputer &"/root/cimv2 ") <br/> set colitems = ob1_miservice. execquery ("select * From win32_networkadapter where" _ <br/> & "netconnectionid = 'local connection '") <br/> for each objitem in colitems <br/> strmacaddress = objitem. macaddress <br/> wscript. echo strmacaddress <br/> next <br/> set colitems = ob1_miservice. execquery ("select * From win32_networkadapterconfiguration") <br/> for each objitem in colitems <br/> 'because the local machine has two win32_networkadapterconfiguration objects with the same MAC address, therefore, you need to add whether it is null to determine <br/> 'because when IPaddress is empty, the loop for each stripaddress in objitem. IPaddress will prompt an error: <br/> 'runtime error: the object is not a collection <br/> if not isnull (objitem. IPaddress) Then <br/> If objitem. macaddress = strmacaddress then <br/> 'Method 1: traverse arrays cyclically <br/>' for each stripaddress in objitem. IPaddress <br/> 'wscript. echo stripaddress <br/> 'Next <br/> 'Method 2: traverse by array boundary <br/> for I = lbound (objitem. IPaddress) _ <br/> to ubound (objitem. IPaddress) <br/> for J = lbound (objitem. ipsubnet) _ <br/> to ubound (objitem. ipsubnet) <br/> for k = lbound (objitem. defaultipgateway) _ <br/> to ubound (objitem. defaultipgateway) <br/> wscript. echo "IPaddress:" & objitem. IPaddress (I) & vbnewline _ <br/> & "ipsubnet:" & objitem. ipsubnet (j) & vbnewline _ <br/> & "defaultipgateway:" & objitem. defaultipgateway (k) <br/> If objitem. defaultipgateway (k) = "192.168.0.8" then <br/> strgateway = strgateway1 <br/> else strgateway = strgateway2 <br/> end if </P> <p> method 3: directly specify the array subscript <br/> 'for L = lbound (strgateway) to ubound (strgateway) <br/> wscript. echo strgateway (0) <br/> 'Next <br/> next </P> <p> 'setgateways method only works when the network interface card (NIC) is in the static IP Mode. <br/> errenable = objitem. enablestatic (_ <br/> stripaddress, strsubnetmask) <br/> errgateways = objitem. setgateways (_ <br/> strgateway, strgatewaymetric) </P> <p> end if <br/> next </P> <p> wscript. echo vbnewline & "after rest IP Address:" & vbnewline <br/> set colitems = ob1_miservice. execquery ("select * From win32_networkadapterconfiguration") <br/> for each objitem in colitems <br/> if not isnull (objitem. IPaddress) Then <br/> If objitem. macaddress = strmacaddress then <br/> for I = lbound (objitem. IPaddress) _ <br/> to ubound (objitem. IPaddress) <br/> for J = lbound (objitem. ipsubnet) _ <br/> to ubound (objitem. ipsubnet) <br/> for k = lbound (objitem. defaultipgateway) _ <br/> to ubound (objitem. defaultipgateway) <br/> wscript. echo "IPaddress:" & objitem. IPaddress (I) & vbnewline _ <br/> & "ipsubnet:" & objitem. ipsubnet (j) & vbnewline _ <br/> & "defaultipgateway:" & objitem. defaultipgateway (k) <br/> If objitem. defaultipgateway (k) = "192.168.0.8" then <br/> strgateway = strgateway1 <br/> else strgateway = strgateway2 <br/> end if <br/> for L = lbound (strgateway) to ubound (strgateway) <br/> wscript. echo strgateway (l) <br/> next </P> <p> end if <br/> next <br/> '/case> <br/> '########################### ############## <br/>

 

 

Of course, there are simpler command line Methods

@ Echo off <br/> netsh interface IP show address | find "192.168.0.120" <br/> If % errorlevel % = 0 (<br/> Goto: to8 <br/>) else (<br/> netsh interface IP show address | find "192.168.0.8" & Goto: to120 <br/>) </P> <p>: to8 <br/> echo change gateway from 192.168.0.120 to 192.168.0.8 <br/> netsh interface IP Set address "Local Connection" static 192.168.0.131 255.255.255.0 192.168.0.8 1 <br/> pause <br/> GOTO: EOF </P> <p>: to120 <br/> echo change gateway from 192.168.0.8 to 192.168.0.120 <br/> netsh interface IP Set address "Local Connection" static 192.168.0.131 255.255.255.0 192.168.0.120 1 <br/> pause <br/> GOTO: EOF

Related Article

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.