#137193 - 2005-04-05 10:20 PM
WMI Change DNS and WINS
|
supermanzdead
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
|
|
|
|
#137196 - 2005-04-06 12:31 AM
Re: WMI Get IP Info
|
maciep
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
|
|
|
|
#137200 - 2005-04-06 03:41 PM
Re: WMI Get IP Info
|
supermanzdead
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
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
|
|
|
|
#137203 - 2005-05-22 05:11 PM
Re: WMI Get IP Info
|
supermanzdead
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
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
1 registered
(Allen)
and 1607 anonymous users online.
|
|
|