Jump to content

Querying WMI for IP Information


gleem
 Share

Recommended Posts

I am writting a script to export network information to a xml file but I am having trouble getting the ip address to show. In addition, I only want it to show network cards and not any other device such as a firewire device.

Ideally, I am trying to show what you would have if you did ipconfig /all but I do not want to deal with converting that to a string and splicing from there.

Below is what I have so far

; Connect to WMI

$wbemFlagReturnImmediately = 0x10

$wbemFlagForwardOnly = 0x20

$strComputer = "localhost"

$networkItems = ""

$Output=""

$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2")

$networkItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration", "WQL", _

$wbemFlagReturnImmediately + $wbemFlagForwardOnly)

; Network Information

$count = 1

If IsObj($networkItems) then

For $objItem In $networkItems

$Output = $Output & ' <networkdescription' & $count & '>' & $objItem.Description & '</networkdescription' & $count & '>' & @CRLF

$Output = $Output & ' <networkmacaddress' & $count & '>' & $objItem.MacAddress & '</networkmacaddress' & $count & '>' & @CRLF

$Output = $Output & ' <networkipaddress' & $count & '>' & $objItem.IPAddress($count) & '</networkipaddress' & $count & '>' & @CRLF

msgbox(0,"", $Output)

if $Output = "" then ExitLoop

$Output=""

$count = $count + 1

Next

Endif

Edited by gleem
Link to comment
Share on other sites

I wrote this a while ago to deal with adapters. It's been a long time, if I remember there was some small errors still to fix in it, but you can get the code from it for the IP info. Hope it helps.

#include <GUIConstants.au3>
#include <GuiIPAddress.au3>

;http://msdn2.microsoft.com/en-us/library/aa394217.aspx



_Singleton('awaaslkdfwelkjrnfwoidfslkndfoiwefrswnfdoiwewwwwiiiiiiiiiiii')
Opt("GUICloseOnESC", 0)         ;1=ESC  closes, 0=ESC won't close

If $CmdLine[0] > 1 Then
    If $CmdLine[2] <> '' Then   ;MAC required
        If $CmdLine[1] = StringUpper('DHCP') Then
            DHCPSet($CmdLine[2])
        ElseIf $CmdLine[1] = StringUpper('STATIC') Then
            Dim $strIPAddress[1],$strSubnetMask[1],$strGateway[1],$strGatewayMetric[1]
            if $CmdLine[3] <> '' and $CmdLine[4] <> '' Then
                If $CmdLine[0] > 4 Then
                    $strGateway[0] = $CmdLine[5]
                    $strGatewayMetric[0] = 1
                Else
                    $strGateway[0] = ''
                    $strGatewayMetric[0] = 1
                EndIf
                $strIPAddress[0] = $CmdLine[3]
                $strSubnetMask[0] = $CmdLine[4]
                StaticSet($CmdLine[2],$strIPAddress,$strSubnetMask,$strGateway,$strGatewayMetric)
            EndIf
        ElseIf $CmdLine[1] = StringUpper('SWITCH') Then
            $objWMIService = ObjGet("winmgmts:\root\cimv2")
            $colNetCards = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

            For $objNetCard in $colNetCards
                If $objNetCard.macaddress = $CmdLine[2] Then
                    If $objNetCard.DHCPEnabled = 0 then
                        Dim $strIPAddress[1],$strSubnetMask[1],$strGateway[1],$strGatewayMetric[1]
                        if $CmdLine[3] <> '' and $CmdLine[4] <> '' Then
                            If $CmdLine[0] > 4 Then
                                $strGateway[0] = $CmdLine[5]
                                $strGatewayMetric[0] = 1
                            Else
                                $strGateway[0] = ''
                                $strGatewayMetric[0] = 1
                            EndIf
                            $strIPAddress[0] = $CmdLine[3]
                            $strSubnetMask[0] = $CmdLine[4]
                            StaticSet($CmdLine[2],$strIPAddress,$strSubnetMask,$strGateway,$strGatewayMetric)
                        EndIf
                    Else
                        DHCPSet($CmdLine[2])
                    EndIf
                    ExitLoop
                EndIf
            Next
        EndIf
    EndIf
EndIf

Global $subItemCount = 0
Global $MainItemArray=0
Global $treeview, $ContextRefreshitem, $ContextDHCPitem , $ContextStaticitem , $ContextCopyClipitem
Global $objWMIService
Global $colNetCards


;;;;;;;;;;;; create ipinputgui
$IPguiWidth = 280
$IPguiHeight = 170
$IPInputGui = GUICreate("Input IP Address",$IPguiWidth,$IPguiHeight)
GUICtrlCreateLabel('IP Address',10,13)
GUICtrlCreateLabel('Subnet'    ,10,53)
GUICtrlCreateLabel('Gateway'   ,10,93)
$IPinput      = _GUICtrlIpAddressCreate ($IPInputGui, $IPguiWidth-10-175, 10, 175, 30);, $WS_THICKFRAME)
$Subnetinput  = _GUICtrlIpAddressCreate ($IPInputGui, $IPguiWidth-10-175, 50, 175, 30);, $WS_THICKFRAME)
$Gatewayinput = _GUICtrlIpAddressCreate ($IPInputGui, $IPguiWidth-10-175, 90, 175, 30);, $WS_THICKFRAME)
$IPOKBtn      = GUICtrlCreateButton("OK"    , $IPguiWidth/2-10-50,$IPguiHeight-20-10,50,20)
$IPCancelBtn  = GUICtrlCreateButton("Cancel", $IPguiWidth/2+10   ,$IPguiHeight-20-10,50,20)
;;;;;;;;;;;; END create ipinputgui

;;;;;;;;;;;; create treeview gui
$guiWidth = 600
$guiHeight = 400
$TreeViewGui = GUICreate("TCP Enabled Network Adapters", $guiWidth,$guiHeight)

$RefreshBtn     = GUICtrlCreateButton("&Refresh",  80, $guiHeight -30, 70, 20)
$statebutton    = GUICtrlCreateButton("- / +"   ,  10, $guiHeight -30, 70, 20)
$cancelbutton   = GUICtrlCreateButton("E&xit"   , $guiWidth-10-70, $guiHeight -30, 70, 20)
$DHCPBtn        = GUICtrlCreateButton("Set &DHCP", $guiWidth/2-80, $guiHeight -30, 80, 20)
$StaticBtn      = GUICtrlCreateButton("Set &Static", $guiWidth/2, $guiHeight -30, 80, 20)

$OneMomentLabel = GUICtrlCreateLabel("One Moment Please", $guiWidth/2-120,$guiHeight/2-32,240,25);LTWH
GUICtrlSetFont(-1,16)
GUICtrlSetState(-1,$GUI_HIDE)

setTreeData()
;;;;;;;;;;;; END create treeview gui

GUISetState ()

While 1
    Sleep(50)
    
    $msg = GUIGetMsg(1)
    Select
        Case ($msg[0] = $cancelbutton And $msg[1] = $TreeViewGui) Or ($msg[0] = $GUI_EVENT_CLOSE And $msg[1] = $TreeViewGui)
            Exit
            ;ExitLoop
        Case $msg[0] = $statebutton
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1    ;adjust to affect main item if on sub items
                $hItem = GUICtrlGetHandle($item)
                GuiCtrlSendMsg($treeview, $TVM_EXPAND, $TVE_TOGGLE, $hItem)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
        Case $msg[0] = $RefreshBtn
            GUICtrlDelete($treeview)
            setTreeData()
        Case $msg[0] = $DHCPBtn
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1+2  ;adjust for reading MAC
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                $text = StringTrimLeft($text,14)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
            
            GUICtrlDelete($treeview)
            
            DHCPSet($text)
            
            setTreeData()
            
        Case $msg[0] = $StaticBtn
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1 +2;adjust for MAC
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                $text = StringTrimLeft($text,14)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
            
            For $objNetCard in $colNetCards
                If $objNetCard.macaddress = $text Then
                    ;show static gui
                    ;fill the ip box's
                    $x=$objNetCard.IPAddress
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($IPinput,$x[0])
                    $x=$objNetCard.IPSubnet
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($Subnetinput,$x[0])
                    $x=$objNetCard.DefaultIPGateway
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($Gatewayinput,$x[0])
                    ExitLoop
                EndIf
            Next
            ;show the gui
            GUISetState(@SW_DISABLE ,$TreeViewGui)
            GUISwitch($IPInputGui)
            GUISetState(@SW_SHOW,$IPInputGui)
            GUISetState(@SW_ENABLE,$IPInputGui)
            
        Case $msg[0] = $IPOKBtn and $msg[1]= $IPInputGui
            ;show the gui
            GUISetState(@SW_DISABLE ,$IPInputGui)
            GUISetState(@SW_HIDE ,$IPInputGui)
            GUISwitch($TreeViewGui)
            GUISetState(@SW_SHOW,$TreeViewGui)
            GUISetState(@SW_ENABLE,$TreeViewGui)
            GUICtrlDelete($treeview)
            
            Dim $strIPAddress[1],$strSubnetMask[1],$strGateway[1],$strGatewayMetric[1]
            $strIPAddress[0] = _GUICtrlIpAddressGet($IPinput)
            $strSubnetMask[0] = _GUICtrlIpAddressGet($Subnetinput)
            $strGateway[0] = _GUICtrlIpAddressGet($Gatewayinput)
            $strGatewayMetric[0] = 1
                    
            StaticSet($text,$strIPAddress,$strSubnetMask,$strGateway,$strGatewayMetric)
            
            setTreeData()
        Case $msg[0] = $IPCancelBtn and $msg[1] = $IPInputGui
            ;show the gui
            GUISetState(@SW_DISABLE ,$IPInputGui)
            GUISetState(@SW_HIDE ,$IPInputGui)
            GUISwitch($TreeViewGui)
            GUISetState(@SW_SHOW,$TreeViewGui)
            GUISetState(@SW_ENABLE,$TreeViewGui)
            ContinueLoop
        Case $msg[0] = $ContextRefreshitem
            GUICtrlDelete($treeview)
            setTreeData()
        Case $msg[0] = $ContextDHCPitem 
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1+2  ;adjust for reading MAC
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                $text = StringTrimLeft($text,14)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
            
            GUICtrlDelete($treeview)
            
            DHCPSet($text)
            
            setTreeData()
        Case $msg[0] = $ContextStaticitem 
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1 +2;adjust for MAC
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                $text = StringTrimLeft($text,14)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
            
            For $objNetCard in $colNetCards
                If $objNetCard.macaddress = $text Then
                    ;show static gui
                    ;fill the ip box's
                    $x=$objNetCard.IPAddress
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($IPinput,$x[0])
                    $x=$objNetCard.IPSubnet
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($Subnetinput,$x[0])
                    $x=$objNetCard.DefaultIPGateway
                    If not IsArray($x) then dim $x[1]
                    _GUICtrlIpAddressSet($Gatewayinput,$x[0])
                    ExitLoop
                EndIf
            Next
            ;show the gui
            GUISetState(@SW_DISABLE ,$TreeViewGui)
            GUISwitch($IPInputGui)
            GUISetState(@SW_SHOW,$IPInputGui)
            GUISetState(@SW_ENABLE,$IPInputGui)
        Case $msg[0] = $ContextCopyClipitem
            $item = GUICtrlRead($treeview)
            If $item > 0 Then
                $item = Int(($item-$treeview-1)/$subItemCount)*$subItemCount+$treeview+1 +2;adjust for MAC
                $text = GUICtrlRead($item, 1) ; Get the text of the treeview item
                $text = StringTrimLeft($text,14)
            Else
                MsgBox(0,'Highlight Network Adapter','No Adapter Selected')
                ContinueLoop
            EndIf
            
            ClipPut($text)
    EndSelect
WEnd

GUIDelete()
Exit

Func setTreeData()
    $objWMIService = ObjGet("winmgmts:\root\cimv2")
    $colNetCards = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


    $treeview = GUICtrlCreateTreeView(10,10,$guiWidth-20,$guiHeight-60, BitOr($TVS_HASBUTTONS, $TVS_HASLINES, $TVS_LINESATROOT, $TVS_DISABLEDRAGDROP, $TVS_SHOWSELALWAYS), $WS_EX_CLIENTEDGE)
    dim $subItemCount = 11
    dim $MainItemArray[$colNetCards.count][$subItemCount];set how many treeviewitems to create

    $i=0
    For $objNetCard in $colNetCards
        $MainItemArray[$i][0]    = GUICtrlCreateTreeViewitem($objNetCard.Caption, $treeview)
        GUICtrlSetColor(-1, 0x0000C0)
        GUICtrlSetState(-1, BitOr($GUI_EXPAND,$GUI_DEFBUTTON))    ; Expand the "General"-item and paint in bold
        $MainItemArray[$i][1] = GUICtrlCreateTreeViewItem('Description = ' & $objNetCard.description ,$MainItemArray[$i][0])
        $MainItemArray[$i][2] = GUICtrlCreateTreeViewItem('MAC Address = ' & $objNetCard.macaddress,$MainItemArray[$i][0])
        If $objNetCard.DHCPEnabled = 0 then
            $x = 'NO'
        Else
            $x = 'YES'
        EndIf
        $MainItemArray[$i][3] = GUICtrlCreateTreeViewItem('DHCP Enabled = ' & $x,$MainItemArray[$i][0])
        $y = $objNetCard.IPAddress
        If not IsArray($y) then dim $y[1]
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][4] = GUICtrlCreateTreeViewItem('IP Address(s) = ' & $x,$MainItemArray[$i][0])
        $y = $objNetCard.IPSubnet
        If not IsArray($y) then dim $y[1]
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][5] = GUICtrlCreateTreeViewItem('IP Subnet(s) = ' & $x,$MainItemArray[$i][0])
        $y = $objNetCard.DefaultIPGateway
        If Not IsArray($y) Then dim $y[1]
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][6] = GUICtrlCreateTreeViewItem('IP Gateway(s) = ' & $x,$MainItemArray[$i][0])
        $MainItemArray[$i][7] = GUICtrlCreateTreeViewItem('DHCP Server = ' & $objNetCard.DHCPServer ,$MainItemArray[$i][0])
        $y = $objNetCard.DNSServerSearchOrder
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][8] = GUICtrlCreateTreeViewItem('DNS Servers = ' & $x,$MainItemArray[$i][0])
        $x = $objNetCard.DHCPLeaseObtained
        if $x <> '' then $x = StringMid($x,1,4) & '/' & StringMid($x,5,2)& '/' & StringMid($x,7,2) & ' at ' & StringMid($x,9,2) & ':' & StringMid($x,11,2) & ':' & StringMid($x,13,2)
        $MainItemArray[$i][9] = GUICtrlCreateTreeViewItem('DHCP Lease Obtained = ' & $x ,$MainItemArray[$i][0])
        $x = $objNetCard.DHCPLeaseExpires
        if $x <> '' then $x = StringMid($x,1,4) & '/' & StringMid($x,5,2)& '/' & StringMid($x,7,2) & ' at ' & StringMid($x,9,2) & ':' & StringMid($x,11,2) & ':' & StringMid($x,13,2)
        $MainItemArray[$i][10] = GUICtrlCreateTreeViewItem('DHCP Lease Expires = ' & $x ,$MainItemArray[$i][0])
        $i = $i + 1
    Next
    
    $contextmenu    = GUICtrlCreateContextMenu ($treeview)
    $ContextRefreshitem = GUICtrlCreateMenuitem ("Refresh", $contextmenu)
    GUICtrlCreateMenuitem ("", $contextmenu);seperator
    $ContextDHCPitem = GUICtrlCreateMenuitem ("Set DHCP", $contextmenu)
    $ContextStaticitem = GUICtrlCreateMenuitem ("Set Static", $contextmenu)
    GUICtrlCreateMenuitem ("", $contextmenu);seperator
    $ContextCopyClipitem = GUICtrlCreateMenuitem ("Copy MAC to clipboard", $contextmenu)
    
EndFunc

Func DHCPSet($text)
    GUISetCursor(15)
    GUICtrlSetState($OneMomentLabel,$GUI_SHOW)
    For $objNetCard in $colNetCards
        If $objNetCard.macaddress = $text Then
            ;'set here to release if its already dhcp
            If $objNetCard.DHCPEnabled <> 0 then
                ;'release ip
                $ret3 = $objNetCard.ReleaseDHCPLease()
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;    'this doesnt work
                MsgBox(0,'',$ret3)
                $ret3 = 0
            Else
                $ret3 = 0
            EndIf
            ;enable DHCP
            $ret = $objNetCard.EnableDHCP()
            ;if ret 0 ok
            ;if ret 1 ok but reboot
            ;if ret other --- error
            $ret2 = $objNetCard.SetDynamicDNSRegistration(True,True)
            ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;'this doesnt work
            ;info=====http://www.microsoft.com/technet/scriptcenter/resources/qanda/apr05/hey0425.mspx
            ;if ret 0 ok
            ;if ret 1 ok but reboot
            ;if ret other --- error
            If $ret > 1 or $ret2 > 1 or $ret3 > 1 Then
                MsgBox(16,'DHCP Set Error','There was an unknown error setting DHCP')
            ElseIf $ret = 1 or $ret2 = 1 or $ret3 = 1 Then
                MsgBox(48,'DHCP Set','DHCP was set on the adapter. Please reboot for all changes to take affect')
            Else
                ;no error
            EndIf
        
            ExitLoop
        EndIf
    Next
    
    ;'set here to check for if ip address exsists and do loop (with timeout) until ip is set
    $timer = TimerInit()
    Do
        $objWMIService = ObjGet("winmgmts:\root\cimv2")
        $colNetCards = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
        For $objNetCard in $colNetCards
            If $objNetCard.macaddress = $text Then
                $x = $objNetCard.IPAddress
                If not IsArray($x) then dim $x[1]
                If $x[0] <> '0.0.0.0' Then 
                    $x=0
                    ExitLoop 2
                EndIf
                ExitLoop
            EndIf
        Next
        
        ;clear objects
        $objNetCard=0
        $objWMIService=0
        $colNetCards=0
            
            
    Until TimerDiff($timer) > 20*1000
            
    GUISetCursor(2)
    GUICtrlSetState($OneMomentLabel,$GUI_HIDE)
EndFunc

Func StaticSet($text,$strIPAddress,$strSubnetMask,$strGateway,$strGatewayMetric)
    GUISetCursor(15)
    GUICtrlSetState($OneMomentLabel,$GUI_SHOW)
    For $objNetCard in $colNetCards
        If $objNetCard.macaddress = $text Then
            $z = $objNetCard.DNSServerSearchOrder;get the order as it will be wiped out
            $ret = $objNetCard.enablestatic($strIPAddress,$strSubnetMask)
            ;0ok;1reboot;othererror
            $ret2 = $objNetCard.SetGateways($strGateway,$strGatewayMetric)
            ;'.... metric is listed as optional parameter, and gateway is array
            ;0ok;1reboot;othererror
            If $ret > 1 or $ret2 > 1 Then
                MsgBox(16,'Static IP Set Error','There was an unknown error setting the static IP')
            ElseIf $ret = 1 or $ret2 = 1 Then
                MsgBox(48,'Static IP Set','Static IP was set on the adapter. Please reboot for all changes to take affect')
            Else
                ;no error
            EndIf
            $objNetCard.SetDNSServerSearchOrder($z);reenter the dns order
            ExitLoop
        EndIf
    Next
    GUISetCursor(2)
    GUICtrlSetState($OneMomentLabel,$GUI_HIDE)
EndFunc


; 3 removed button enable/disable
;       add check for ip 0.0.0.0
;       move DHCP and static set to seperate functions
;       move buttons around
;       check that gateway,ip,subnet when read result in array if not set blank array
;4  change to use the mac address instead
;5  context menu
;       release dhcp on dhcp
;6  add copy MAC to context
;   add cmdlines
Link to comment
Share on other sites

Thanks Stampy for the reply.. I started working on code that doesnt use wmi... This seems to work.. but what I need to do now is to have it loop until all adapters are picked out.

here is what I have so far.

---------

#include <Constants.au3>

$source = Run(@ComSpec & " /c ipconfig /all", @SystemDir, @SW_HIDE, $STDERR_CHILD + $STDOUT_CHILD)

$sourceread = StdoutRead($source)

; Get Description Name

$startnumber = StringInStr($sourceread,"Description . . . . . . . . . . . : ")

$endnumber = StringInStr($sourceread, "Physical Address")

$startnumber = $startnumber + 36

$endnumber = $endnumber - $startnumber

$descriptionresult = StringMid($sourceread,$startnumber,$endnumber)

$descriptionresult = StringStripWS($descriptionresult,2)

; Get MAC Address

$startnumber = StringInStr($sourceread,"Physical Address. . . . . . . . . : ")

$startnumber = $startnumber + 36

$macresult = StringMid($sourceread,$startnumber,17)

$macresult = StringStripWS($macresult,2)

; Get IP Address

$startnumber = StringInStr($sourceread,"IP Address. . . . . . . . . . . . : ")

$startnumber = $startnumber + 36

$ipresult = StringMid($sourceread,$startnumber,16)

$ipresult = StringStripWS($ipresult,2)

; Get Subnet Address

$startnumber = StringInStr($sourceread,"Subnet Mask . . . . . . . . . . . : ")

$startnumber = $startnumber + 36

$subnetresult = StringMid($sourceread,$startnumber,16)

$subnetresult = StringStripWS($subnetresult,2)

; Get Default Gateway Address

$startnumber = StringInStr($sourceread,"Default Gateway . . . . . . . . . : ")

$startnumber = $startnumber + 36

$defaultgatewayresult = StringMid($sourceread,$startnumber,16)

$defaultgatewayresult = StringStripWS($defaultgatewayresult,2)

; Get DHCP Server Address

$startnumber = StringInStr($sourceread,"DHCP Server . . . . . . . . . . . : ")

$startnumber = $startnumber + 36

$dhcpserverresult = StringMid($sourceread,$startnumber,16)

$dhcpserverresult = StringStripWS($dhcpserverresult,2)

; Get DNS Server Address

$startnumber = StringInStr($sourceread,"DNS Servers . . . . . . . . . . . : ")

$startnumber = $startnumber + 36

$dnsserverresult = StringMid($sourceread,$startnumber,16)

$dnsserverresult = StringStripWS($dnsserverresult,2)

; Output Results

msgbox(0,"",$descriptionresult & @CRLF & $macresult & @CRLF & $ipresult & @CRLF & $subnetresult & @CRLF & $defaultgatewayresult & @CRLF & $dhcpserverresult & @CRLF & $dnsserverresult)

Link to comment
Share on other sites

Here is the wmi code striped out of my earlier script with the gui and other info removed. The function below I created will give only the caption then the ip's and the subnets all in an array. You can adjust as you need.

Cheers

#include <array.au3>

$array = getIPs()


If IsArray($array) then _ArrayDisplay($array)

Func getIPs()
    $objWMIService = ObjGet("winmgmts:\root\cimv2")
    $colNetCards = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")


    dim $MainItemArray[$colNetCards.count][3]   ;set an array to hold ip's
    
    $i=0
    For $objNetCard in $colNetCards
        
        
        $MainItemArray[$i][0] = $objNetCard.Caption
        
        
        $y = $objNetCard.IPAddress
        If not IsArray($y) then dim $y[1]
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][1] = $x
        
        $y = $objNetCard.IPSubnet
        If not IsArray($y) then dim $y[1]
        $x = ''
        For $k = 0 to UBound($y)-1
            $x = $x & $y[$k] & ';  '
        Next
        $MainItemArray[$i][2] = $x
        $i=$i+1
    Next
    
    If Not IsArray($MainItemArray) Then Dim $MainItemArray[1][1]
    Return $MainItemArray
    
EndFunc
Link to comment
Share on other sites

  • 3 weeks later...

Hi Stampy,

nice one thnx

i'm trying to convert your script .. but there a re some small problem ..

could you tell me what is wrong ? or someone else

; Reference
; Microsoft : http://msdn2.microsoft.com/en-us/library/aa394217.aspx
; AutoIt forum : http://www.autoitscript.com/forum/index.php?showtopic=62219

Global $cI_CompName = @ComputerName
Global Const $cI_VersionInfo        = "00.03.08"
Global Const $cI_aName              = 0, _
             $cI_aDesc              = 4
Global  $wbemFlagReturnImmediately  = 0x10, _
$wbemFlagForwardOnly        = 0x20
Global  $ERR_NO_INFO                = "Array contains no information", _
        $ERR_NOT_OBJ                = "$colItems isnt an object"

Dim $NetworkInfoEx

_ComputerGetNetworkInfoExtended($NetworkInfoEx)

If @error Then
    $error = @error
    $extended = @extended
    Switch $extended
        Case 1
            _ErrorMsg($ERR_NO_INFO)
        Case 2
            _ErrorMsg($ERR_NOT_OBJ)
    EndSwitch
EndIf

For $i = 1 To $NetworkInfoEx[0][0] Step 1
    MsgBox(0, "Test _ComputerGetNetworkCardsExtended", "Name: " & $NetworkInfoEx[$i][0] & @CRLF & _
            "Description: " & $NetworkInfoEx[$i][1]  & @CRLF & _
            "IPAddress: " & $NetworkInfoEx[$i][2] & @CRLF & _
            "Subnetmask: " & $NetworkInfoEx[$i][3] & @CRLF & _
            "Gateway: " & $NetworkInfoEx[$i][4] & @CRLF & _
            "DHCP: " & $NetworkInfoEx[$i][5] & @CRLF & _
            "DHCPServer: " & $NetworkInfoEx[$i][6] & @CRLF & _
            "DNSDomein: " & $NetworkInfoEx[$i][7] & @CRLF & _
            "DNSHostname: " & $NetworkInfoEx[$i][8] & @CRLF & _
            "DNSServer: " & $NetworkInfoEx[$i][9] & @CRLF & _
            "MacAdress: " & $NetworkInfoEx[$i][10] & @CRLF & _
            "Wins Primary: " & $NetworkInfoEx[$i][11] & @CRLF & _
            "Wins Secondary: " & $NetworkInfoEx[$i][12])
Next

Func _ComputerGetNetworkInfoExtended(ByRef $NetworkInfoEx)
    Local $colItems, $objWMIService, $objItem
    Dim $NetworkInfoEx[1][13], $i = 1

    $objWMIService = ObjGet("winmgmts:\\" & $cI_Compname & "\root\CIMV2")
    $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)
    
    If IsObj($colItems) Then
        For $objItem In $colItems
            ReDim $NetworkInfoEx[UBound($NetworkInfoEx) + 1][13]
            $NetworkInfoEx[$i][0]  = $objItem.Caption
            $NetworkInfoEx[$i][1]  = $objItem.description
            $NetworkInfoEx[$i][2]  = $objItem.IPAddress
            $NetworkInfoEx[$i][3]  = $objItem.IPSubnet
            $NetworkInfoEx[$i][4]  = $objItem.DefaultIPGateway 
            $NetworkInfoEx[$i][5]  = $objItem.DHCPEnabled 
            $NetworkInfoEx[$i][6]  = $objItem.DHCPServer 
            $NetworkInfoEx[$i][7]  = $objItem.DNSDomain 
            $NetworkInfoEx[$i][8]  = $objItem.DNSHostName
            $NetworkInfoEx[$i][9]  = $objItem.DNSServerSearchOrder
            $NetworkInfoEx[$i][10] = $objItem.MACAddress 
            $NetworkInfoEx[$i][11] = $objItem.WINSPrimaryServer 
            $NetworkInfoEx[$i][12] = $objItem.WINSSecondaryServer 
            $i += 1
        Next
        $NetworkInfoEx[0][0] = UBound($NetworkInfoEx) - 1
        If $NetworkInfoEx[0][0] < 1 Then
            SetError(1, 1, 0)
        EndIf
    Else
        SetError(1, 2, 0)
    EndIf
EndFunc ;_ComputerGetNetworkCardsExtended


Func _ErrorMsg($message, $time = 0)
    MsgBox(48 + 262144, "Error!", $message, $time)
EndFunc

Best regards,Emiel Wieldraaijer

Link to comment
Share on other sites

This is just a wild guess, but I think you should go back to stampy's code.

Instead of what you have in its place, use the original:

$objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

As for myself, I would never use ReDim in a loop like you did, and I would be very careful about its use, it's easy to screw up. But I do sometimes use ReDim once at the end of a For...Next loop.

Edited by Squirrely1

Das Häschen benutzt Radar

Link to comment
Share on other sites

@Squirrely1

Thnx for your comment ..

But my script retrieves the MACadress, the DHCP Server and DNS Domein .. why not the others

the original "$objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")" produces the same output

Edited by Emiel Wieldraaijer

Best regards,Emiel Wieldraaijer

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...