dmkirkland Posted March 14, 2018 Author Posted March 14, 2018 I think I'm tracking a bit. But how does the script know which value to pull and subsequently which network adapter has an actual IP address assigned to it? I ask this because I have 4 network adapters but only 1 has its IP address shown at the completion of this script.
Bilgus Posted March 14, 2018 Posted March 14, 2018 The script doesn't 'know' it just gets values returned from the WMI object Now WMI is a whole can of worms just take it like this WMI exposes an OBJECT to you and you can use that OBJECT to get information you desire
Bilgus Posted March 15, 2018 Posted March 15, 2018 on that MSDN page I linked to (Yesterday?) check out some of the other interfaces WMI exposes to you I mean you can get all kinds of info You can also set things through the Methods exposed to you too.. https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx
Bilgus Posted March 15, 2018 Posted March 15, 2018 Do be aware though just because its in that list doesn't necessarily mean its on YOUR computer or that you have enough privileges to actually use it but we start to fall down a rabbit hole beyond this so...
dmkirkland Posted March 15, 2018 Author Posted March 15, 2018 Ok, I'm reading through the pages you (Bilgus) linked. While I'm trying to digest, I think I have one more question that I don't seem able to answer from all the material I have: is it WMI that the script uses to determine the "active" network adapter or is it something else in the script? I really want this to be the last question I ask but I'm afraid I might have more as I dig deeper and understand better.
dmkirkland Posted March 15, 2018 Author Posted March 15, 2018 Ok. So I've been analyzing as much as possible...I'm really trying here guys. So from what I've read about arrays, it seems Ubound "strips" out unnecessary/unused elements in a variable? Which I assume is what is being used to eliminate "non-entries" from the ObjGet("WMI Command")? I still don't understand the WMI side of things at all. I tried to figure it out. The hard part with that is that I can' seem to find a way to see what WMI/wmic does/how it works. I understand the concept of it, to a degree. But I can't figure out a way to just pop up a CMD window and try stuff out/see what happens. I think that's one of the bigger hangups I'm having. The other one is seeing in the script how it breaks down and displays only the IP Address for a connected and valid interface. I did play with the script and if I change the end of the Ubound line to - 0, I get what seems like the entire list of IP's (valid or not) from all the interfaces on my machine. Conversely, if I change the end of the same line to - 2 I only get my IPv4 address whereas before I got my IPv4 and IPv6 address (v4 address in one box, then v6 address in a subsequent box). I really wish this would just click for me. I hate bothering you guys with novice stuff.
Bilgus Posted March 15, 2018 Posted March 15, 2018 No UBound returns the UpperBound => number of elements in that array object we subtract 1 because the arrays are 0 based meaning the first element is 0 NOT 1 so if we try to call the element at $aARRAY[UBound($aARRAY)] we would be one past the end and it would throw an error Instead if we called $aARRAY[UBound($aArray) -1] that would return the last element in the array (assuming it is an array) You should always check with If isArray($aARRAY) Then ;Do some array accesses here....$aArray[0] Else Msgbox(0,"ERROR!", "NOT AN ARRAY") Endif Before you try to access an array element otherwise you will get an error that crashes the script when the item isn't an array
Bilgus Posted March 15, 2018 Posted March 15, 2018 (edited) On to your questions about WMI First I commented out the lines that are unneeded in the original example you should still get the same results ;Network Interfaces; Local $IPConfig $strComputer = "localhost" $ping = Ping($strComputer, 500) ;1. ping a computer ;If $ping Then ; 2. if you get a valid ping back, use WMI to get/set the network configuration $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") $IPConfigSet = $objWMIService.ExecQuery("Select IPAddress from Win32_NetworkAdapterConfiguration ") ; See Here https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx For $IPConfig In $IPConfigSet ; 3. loop through collection $IPConfigSet ; $IPConfig.IPAddress <> "" Then ;and if IP is not "" then do something. For $x = 0 To UBound($IPConfig.IPAddress) - 1 ; Data type: string array; so we need to go through the array to retrieve all the strings ;NOTE the -1 above this is because the arrays are 0 based MsgBox(0, "Current IP", "Your IP address is " & $IPConfig.IPAddress($x)) Next ;EndIf Next ;EndIf Next I have added a sample of what is possible with WMI First it creates an array with all the available classes on YOUR pc Next I Choose the Class Win32_BIOS and have WMI return all the properties in it, Finally I access each property and display its Value in the console window Then We do it again with Win32_NetworkAdapterConfiguration expandcollapse popup#include <Array.Au3> ;WMI EXAMPLE Local $strComputer = "localhost" Local $sClass = "" Local $sProperties = "" Local $sMethods = "" Local $aDisp Local $aTmp Local $vTmp ; Could be anything Local $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2") ; Same $objWMIService Object ; Display all available Classes in this object Local $sClasses = "" For $objclass In $objWMIService.SubclassesOf();<--WMI Method $sClasses &= ($objClass.Path_.Class) & @LF ;Build a string seperated by \n(@LF) Next _ArrayDisplay(StringSplit($sClasses, @LF), "AVAILABLE WMI CLASSES", "", 0, Default, "NAME") ;Split string at \n(@LF) place each into an array $sClass = "Win32_BIOS" ;<-The Class I've choosen ;Lets Try out one of the classes Local $objBios = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2:" & $sClass) For $objBiosProp In $objBios.Properties_() ;<-Another WMI Method For $obj In $objWMIService.ExecQuery("Select * from " & $sClass) ;<-Another WMI Method $sProperties &= $objBiosProp.Name & @CR $vTmp = $obj.Properties_($objBiosProp.Name).Value ;Use the Properties_ Method to call our desired property ;Below We try to determine what the item returned is so we can parse it If IsArray($vTmp) Then $sProperties &= _ArrayToString($vTmp, ", ") ElseIf IsObj($vTmp) Then $sProperties &= "[OBJECT]" ElseIf IsPtr($vTmp) Then $sProperties &= "[PTR]" ElseIf IsString($vTmp) Then $sProperties &= $vTmp ;Use the Properties_ Method to call our desired property ElseIf IsBool($vTmp) Then $sProperties &= $vTmp ? "True" : "False" ;Use the Properties_ Method to call our desired property Elseif $vTmp <> "" Then $sProperties &= Number($vTmp) EndIf $sProperties &= @LF Next Next $aDisp = StringSplit($sProperties, @LF) ;Split string at \n(@LF) place each into an array If IsArray($aDisp) Then _ArrayColInsert($aDisp, 1) For $i = 1 To $aDisp[0][0] ; String Split stores Count in the first element by default $aTmp = StringSplit($aDisp[$i][0], @CR) ;Split string at \r(@CR) place each into an array If IsArray($aTmp) Then $aDisp[$i][0] = $aTmp[0] > 0 ? $aTmp[1] : "?" ;Check if this element exists if not make it "?" $aDisp[$i][1] = $aTmp[0] > 1 ? $aTmp[2] : " " ;Check if this element exists if not make it " " EndIf Next _ArrayDisplay($aDisp, $sClass & " Properties", "", 0, Default, "NAME|VALUE") EndIf ;No methods available for this one but... ConsoleWrite("Methods:" & @CRLF & @CRLF) For $objBiosMethods In $objBios.Methods_ ConsoleWrite(@TAB & $objBiosMethods.Name & @CRLF) Next ;Lets Do it again for the network adapter Class $sProperties = "" $sClass = "Win32_NetworkAdapterConfiguration" ;<-The Class I've choosen ;Lets Try out one of the classes $objBios = ObjGet("winmgmts:\\" & $strComputer & "\root\cimv2:" & $sClass) ConsoleWrite($sClass & @CRLF & "------------------------------------------" & @CRLF) For $objBiosProp In $objBios.Properties_() ;<-Another WMI Method For $obj In $objWMIService.ExecQuery("Select * from " & $sClass) ;<-Another WMI Method $sProperties &= $objBiosProp.Name & @CR $vTmp = $obj.Properties_($objBiosProp.Name).Value If IsArray($vTmp) Then $sProperties &= _ArrayToString($vTmp, ", ") ElseIf IsObj($vTmp) Then $sProperties &= "[OBJECT]" ElseIf IsPtr($vTmp) Then $sProperties &= "[PTR]" ElseIf IsString($vTmp) Then $sProperties &= $vTmp ;Use the Properties_ Method to call our desired property ElseIf IsBool($vTmp) Then $sProperties &= $vTmp ? "True" : "False" ;Use the Properties_ Method to call our desired property Elseif $vTmp <> "" Then $sProperties &= Number($vTmp) EndIf $sProperties &= @LF Next Next $aDisp = StringSplit($sProperties, @LF) ;Split string at \n(@LF) place each into an array If IsArray($aDisp) Then _ArrayColInsert($aDisp, 1) For $i = 1 To $aDisp[0][0] ; String Split stores Count in the first element by default $aTmp = StringSplit($aDisp[$i][0], @CR) ;Split string at \r(@CR) place each into an array If IsArray($aTmp) Then $aDisp[$i][0] = $aTmp[0] > 0 ? $aTmp[1] : "?" ;Check if this element exists if not make it "?" $aDisp[$i][1] = $aTmp[0] > 1 ? $aTmp[2] : " " ;Check if this element exists if not make it " " EndIf Next _ArrayDisplay($aDisp, $sClass & " Properties", "", 0, Default, "NAME|VALUE") EndIf ConsoleWrite("Methods:" & @CRLF & @CRLF) For $objBiosMethods In $objBios.Methods_ ConsoleWrite(@TAB & $objBiosMethods.Name & @CRLF) Next This is a bit over your head atm but feel free to ask what something does if you can't figure it out Edited March 15, 2018 by Bilgus
dmkirkland Posted March 15, 2018 Author Posted March 15, 2018 Ok, I'll have a deeper look at the large script you've got there. Back to the smaller (original) one. I guess one question I have is the [variable].ExecQuery and [variable].IPAddress. Are these "sub commands" of some sort that AutoIT knows or are they part of WMI commands?
Bilgus Posted March 15, 2018 Posted March 15, 2018 NO NOT at all those are objects exposed to AutoIt by WMI Its essentially a way to call a DLL but without really knowing the intricate details of it https://www.autoitscript.com/autoit3/docs/intro/ComRef.htm
BrewManNH Posted March 15, 2018 Posted March 15, 2018 If you're curious about what's available in WMI to query, check out this thread on the AutoItScriptOMatic. The original file may not work in the latest version of AutoIt, but if you go through the thread there are updates to that people have posted in it. If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag GudeHow to ask questions the smart way! I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from. Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays. - ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script. - Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label. - _FileGetProperty - Retrieve the properties of a file - SciTE Toolbar - A toolbar demo for use with the SciTE editor - GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI. - Latin Square password generator
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now