Page 1 of 1 1
Topic Options
#137193 - 2005-04-05 10:20 PM WMI Change DNS and WINS
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
I am trying to write some code to change dns / wins via WMI now I have this code

Code:

$objWMI = GetObject('winmgmts:{impersonationLevel=impersonate}!//' + $strItem + '/root/cimv2:Win32_NetworkAdapterConfiguration')



I just need to know what query to run to get the addresses and what to run to actually do the change...I am pretty new to WMI coding and am just looking for pointers in the right direction.

Thanks~!


Edited by supermanzdead (2005-04-06 10:41 PM)
_________________________
~Mwah

Top
#137194 - 2005-04-05 10:41 PM Re: WMI Get IP Info
Jose Offline
Seasoned Scripter
*****

Registered: 2001-04-04
Posts: 693
Loc: Buenos Aires - Argentina
Hi Super!
Use the search engine. Just found this thread wich might help you.

BTW. Your subject is pointing somewhere else from your need.
_________________________
Life is fine.

Top
#137195 - 2005-04-06 12:21 AM Re: WMI Get IP Info
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
PFFFTT!
That is neither my code nor is it WMI. A good start would be to use a WMI browser to actually mine the data out to see the methods and properties. Failing that, search for other code on the board and R&D (Reuse & Disguise) it.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#137196 - 2005-04-06 12:31 AM Re: WMI Get IP Info
maciep Offline
Korg Regular
*****

Registered: 2002-06-14
Posts: 947
Loc: Pittsburgh
Check out the WMI Reference

And in particular Win32_NetworkAdapterConfiguration

Btw les, i guess that's the last time you'll shorten my long lines for me.

Top
#137197 - 2005-04-06 04:26 AM Re: WMI Get IP Info
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
Jose: I read that thread, I am trying to avoid using the registry and just use WMI (teaching myself coding with just WMI)

I used a WMI Browser to find the WIN32_NetworkAdapterConfiguration, but I am confused on how you pull information and change information in WMI. I know its kinda like reading information from a database where you execute a query and the information in the query is put into an array, just finding out which query to execute escapes me. I know there is an IPAddress function(?) or method(?) which returns an array of strings which are the ip address. How would I change the information with in this array?

maciep: thanks for the msdn links, they give me more information, I think I just need to look at the examples more...not used to learning like this, I am usually reading a book or taking a class...thanks for all your feedback and assistance!

I found a great resource at http://www.microsoft.com/technet/scriptcenter/topics/networking/05_atnc_dns.mspx

I'd still like some kix examples if you could, I don't want anyone to code it for me just give me examples I can work with. Thanks.


Edited by supermanzdead (2005-04-06 04:34 AM)
_________________________
~Mwah

Top
#137198 - 2005-04-06 04:45 AM Re: WMI Get IP Info
Les Offline
KiX Master
*****

Registered: 2001-06-11
Posts: 12734
Loc: fortfrances.on.ca
Download the WMI Scriptomatic and R&D the VBS code from it. It is not that hard to convert most VBS code to KiX.
_________________________
Give a man a fish and he will be back for more. Slap him with a fish and he will go away forever.

Top
#137199 - 2005-04-06 01:58 PM Re: WMI Get IP Info
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
Code:

Function CommitOptionsWMI()
Dim $strDNS1, $strDNS2, $strWINS1, $strWINS2, $strComputers
$strDNS1 = $txtDNS1.text
$strDNS2 = $txtDNS2.text
$strWINS1 = $txtWINS1.text
$strWINS2 = $txtWINS2.text
$strComputers = $txtComputers.text
$astrComputers = Split($strComputers, @CRLF)
For Each $strItem in $astrComputers
$objWMIData = GetObject('winmgmts:{impersonationLevel=impersonate}!//' + $strItem + '/root/cimv2')
$colWMIData = $objWMIData.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For Each $strQuery in $colWMIData
$txtResults.appendtext($strQuery.WINSPrimaryServer + @CRLF)
$txtResults.appendtext($strQuery.WINSSecondaryServer + @CRLF)
For Each $strDNS in $strQuery.DNSServerSearchOrder
$txtResults.appendtext($strDNS + @CRLF)
Next
Next
Next
$frmOptions.Hide
EndFunction



That is what I have so far, displays the WINS servers and DNS servers, doing great so far...now I need to learn how to change this item....if you have any comments on the code or if there is something I am doing wrong let me know


Edited by supermanzdead (2005-04-06 02:12 PM)
_________________________
~Mwah

Top
#137200 - 2005-04-06 03:41 PM Re: WMI Get IP Info
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
Code:

Function CommitOptionsWMI()
Dim $strDNS1, $strDNS2, $strWINS1, $strWINS2, $strComputers, $astrDNS[1]
$strDNS1 = $txtDNS1.text
$strDNS2 = $txtDNS2.text
$strWINS1 = $txtWINS1.text
$strWINS2 = $txtWINS2.text
$astrDNS[0] = $strDNS1
$astrDNS[1] = $strDNS2
$strComputers = $txtComputers.text
$astrComputers = Split($strComputers, @CRLF)
For Each $strItem in $astrComputers
$objWMIData = GetObject('winmgmts:{impersonationLevel=impersonate}!//' + $strItem + '/root/cimv2')
$colWMIData = $objWMIData.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For Each $strQuery in $colWMIData
$SetDNS = $strQuery.SetDNSServerSearchOrder($astrDNS)
If $SetDNS = 0
$txtResults.appendtext("Success" + @CRLF)
EndIF
$txtResults.appendtext($strQuery.WINSPrimaryServer + @CRLF)
$txtResults.appendtext($strQuery.WINSSecondaryServer + @CRLF)
For Each $strDNS in $strQuery.DNSServerSearchOrder
$txtResults.appendtext($strDNS + @CRLF)
Next
Next
Next
$frmOptions.Hide
EndFunction



Above is the code to change the DNS servers via WMI but when you list them right after the change they don't refresh, yet on the machine they refresh, which is cool cause it works, only way I have to check that it worked is to check the error code it returns...


Edited by supermanzdead (2005-04-07 04:36 AM)
_________________________
~Mwah

Top
#137201 - 2005-04-06 04:58 PM Re: WMI Get IP Info
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
Code:

Function CommitOptionsWMI()
Dim $strDNS1, $strDNS2, $strWINS1, $strWINS2, $strComputers, $astrDNS[1]
$strDNS1 = $txtDNS1.text
$strDNS2 = $txtDNS2.text
$strWINS1 = $txtWINS1.text
$strWINS2 = $txtWINS2.text
$astrDNS[0] = $strDNS1
$astrDNS[1] = $strDNS2
$strComputers = $txtComputers.text
$astrComputers = Split($strComputers, @CRLF)
For Each $strItem in $astrComputers
$txtResults.appendtext("Changing DNS and WINS on " + $strItem + "..." + @CRLF)
$objWMIData = GetObject('winmgmts:{impersonationLevel=impersonate}!//' + $strItem + '/root/cimv2')
If @Error = 0
$colWMIData = $objWMIData.ExecQuery('SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True')
For Each $strQuery in $colWMIData
$SetDNS = $strQuery.SetDNSServerSearchOrder($astrDNS)
$SetWINS = $strQuery.SetWINSServer($strWINS1, $strWINS2)
Select
Case $SetDNS = 0 and $SetWINS = 0
$txtResults.appendtext(" " + $strItem + " has had a successful change" + @CRLF)
Case $SetDNS = 1 or $SetDNS = 1
$txtResults.appendtext(" " + $strItem + " requires a restart!" + @CRLF)
Case 1
$txtResults.appendtext(" " + $strItem + " has other problems and needs research!" + @CRLF)
EndSelect
Next
Else
$txtResults.appendtext(" " + $strItem + " needs to have WMI checked!")
EndIf
Next
$frmOptions.Hide
EndFunction



My final UDF, just have to add some error checking as far as the format of the addresses and computer name

Thanks everyone for your help, I would like some feedback if you have time to look at the code.


Edited by supermanzdead (2005-04-07 04:36 AM)

Top
#137202 - 2005-05-18 09:37 PM Re: WMI Get IP Info
Jim_Gordon Offline
Fresh Scripter

Registered: 2004-03-11
Posts: 5
Loc: PA, USA
This is good stuff. I am looking to do something very similar. I want to be able to update various settings of a NIC, but how could I make use of this UDF?
I have several virtual servers(!) with multiple NICS. I want to update DNS for all server with IPEnabled=True. Can this get it done? what do I have to pass this to make it work? Do I have to give it all the vars?
Sorry for all the questions, but I am still getting my mind wrapped around it.


Edited by Jim_Gordon (2005-05-18 09:41 PM)

Top
#137203 - 2005-05-22 05:11 PM Re: WMI Get IP Info
supermanzdead Offline
Getting the hang of it

Registered: 2005-03-09
Posts: 50
You should be able to get it done by modifying the UDF a bit, I wrote this for a gui interface (kixforms)...
_________________________
~Mwah

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 1607 anonymous users online.
Newest Members
Viginette, ManuvdWielNL, Sir_Barrington, batdk82, StuTheCoder
17888 Registered Users

Generated in 0.163 seconds in which 0.125 seconds were spent on a total of 12 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org