Dana L. Houseman Posted August 12, 2007 Share Posted August 12, 2007 (edited) I am using a script that returns my mac address of the current machine as $macaddress I need to read an external .txt file for the data that $macaddress represents and then return the next value in that file as a variable to my script, for now let's say I just want to display a msgbox "Your Workstation is" " "$WSVar Sample text: 0018DE872539, Workstation1 0018dF783243, Workstaiton2 I don't have any ideas at the script as of yet, but I assume it may include parsing the file somehow. When finished and executed this script should return the 2nd value of a comma deliminated text file based on the mac address of the machine. Thks in Advance! D~ Edited August 12, 2007 by Dana L. Houseman Link to comment Share on other sites More sharing options...
BugFix Posted August 12, 2007 Share Posted August 12, 2007 (edited) Hi, you can read out MAC-adress(es) from local machine with following script. All founded MAC's are in an array. Now you must only compare all entrys from the array with your text-file. Best, you load the file with _FileReadToArray(), than in a loop split entrys by ',' and compare. ; Generated by AutoIt Scriptomatic #include <array.au3> $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $strComputer = "localhost" Dim $arMAC[1]=[0] $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) then For $objItem In $colItems If $objItem.MACAddress <> '' Then ReDim $arMAC[UBound($arMAC)+1] $arMAC[UBound($arMAC)-1] = $objItem.MACAddress $arMAC[0] += 1 EndIf Next Endif _ArrayDisplay($arMAC) Edited August 12, 2007 by BugFix Best Regards BugFix Link to comment Share on other sites More sharing options...
Dana L. Houseman Posted August 13, 2007 Author Share Posted August 13, 2007 Hi, you can read out MAC-adress(es) from local machine with following script. All founded MAC's are in an array. Now you must only compare all entrys from the array with your text-file. Best, you load the file with _FileReadToArray(), than in a loop split entrys by ',' and compare. ; Generated by AutoIt Scriptomatic #include <array.au3> $wbemFlagReturnImmediately = 0x10 $wbemFlagForwardOnly = 0x20 $colItems = "" $strComputer = "localhost" Dim $arMAC[1]=[0] $objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\root\CIMV2") $colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _ $wbemFlagReturnImmediately + $wbemFlagForwardOnly) If IsObj($colItems) then For $objItem In $colItems If $objItem.MACAddress <> '' Then ReDim $arMAC[UBound($arMAC)+1] $arMAC[UBound($arMAC)-1] = $objItem.MACAddress $arMAC[0] += 1 EndIf Next Endif _ArrayDisplay($arMAC)</div> Thanks, but I have the MAC address, what I need is help on the array and the how to read the file to the array and then bring back into the script the "Workstation" Number which varies based on the MAC. 0018DE872539, Workstation1 0018dF783243, Workstaiton2 Thanks again! D~ Link to comment Share on other sites More sharing options...
enaiman Posted August 13, 2007 Share Posted August 13, 2007 _FileReadToArray - read the file into an arrayFor $i = 1 To Ubound(your array) - 1StringSplit - use "," as delimiterIf first part returned by StringSplit matches your MAC Then MsgBox (0, "Title", MAC and Identifier)Next SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
Dana L. Houseman Posted August 13, 2007 Author Share Posted August 13, 2007 (edited) I guess I'm still confused on how to parse the file for $macaddress and return the $wsnum (workstation number) from my log file. I get a message box that says my mac is and then lists it, then it does that 1 time for each line in my .log file. I'm also thinking I'm going to have to add an if statement so that if the macaddress is not found then a message is displayed, "workstation unknown" or something to that effect.I'm listing all my code, and attaching my log file. Help is appreciated, and learning is never ending! RunWait(@ComSpec & " /c net config workstation > " & @TempDir & "\mac.txt", "", @SW_HIDE) If FileExists(@TempDir & "\mac.txt") Then $macfile = FileOpen(@TempDir & "\mac.txt", 0) While 1 $macline = FileReadLine($macfile) If @error = -1 Then ExitLoop $macresult = StringInStr($macline, "NetBT_Tcpip_") If $macresult <> "0" Then $macline = StringStripWS($macline, 4) $macarray = StringSplit($macline, "(") $macline = StringStripWS($macarray[2], 8) $macarray = StringSplit($macline, ")") $macaddress = $macarray[1] ExitLoop EndIf Wend FileClose($macfile) EndIf #include <file.au3> Dim $aRecords _FileReadToArray("c:\error.log",$aRecords) For $i = 1 To Ubound($aRecords) -1 $array = StringSplit( $macaddress, ", ", 1) msgbox(0,"Title", "Your workstation is: " & $array[1]) ; If first part returned by StringSplit matches your MAC Then MsgBox (0, "Title", MAC and Identifier) Next Edited August 13, 2007 by Dana L. Houseman Link to comment Share on other sites More sharing options...
enaiman Posted August 13, 2007 Share Posted August 13, 2007 you were making a mistake in: $array = StringSplit( $macaddress, ", ", 1) here is the working code: expandcollapse popupRunWait(@ComSpec & " /c net config workstation > " & @TempDir & "\mac.txt", "", @SW_HIDE) If FileExists(@TempDir & "\mac.txt") Then $macfile = FileOpen(@TempDir & "\mac.txt", 0) While 1 $macline = FileReadLine($macfile) If @error = -1 Then ExitLoop $macresult = StringInStr($macline, "NetBT_Tcpip_") If $macresult <> "0" Then $macline = StringStripWS($macline, 4) $macarray = StringSplit($macline, "(") $macline = StringStripWS($macarray[2], 8) $macarray = StringSplit($macline, ")") $macaddress = $macarray[1] ExitLoop EndIf Wend msgbox(0,"Title", $macarray[1]) FileClose($macfile) EndIf #include <file.au3> Dim $aRecords Dim $found = 0 _FileReadToArray("c:\error.log",$aRecords) For $i = 1 To Ubound($aRecords) -1 $array = StringSplit( $aRecords[$i], ", ", 1) If $array[1] = $macaddress Then msgbox(0,"Title", "Your workstation is: " & $array[1]) $found = 1 EndIf ; If first part returned by StringSplit matches your MAC Then MsgBox (0, "Title", MAC and Identifier) Next If $found = 0 Then MsgBox(0, "Title", "Your MAC address was not found") SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script wannabe "Unbeatable" Tic-Tac-Toe Paper-Scissor-Rock ... try to beat it anyway :) Link to comment Share on other sites More sharing options...
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