Jump to content

Need to get mac address and Computer name


 Share

Recommended Posts

Hello,

**** I am not a scripter or programer****

I am having a little issue. I need the mac address and computer name off each of our computers and into an excel file. I was able to do this but now they are telling me they need the output to excel with headers and all the mac addresses and names on each row. I can't figure it out :ermm:  Below is the code that I have.

Oh P.S. each of these machines don't have excel installed.

J

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Icon=C:\Program Files\Microsoft Office\Office15\Groove\ToolIcons\OUTGOING.ICO
#AutoIt3Wrapper_Res_Description=Capture System Mac address's and Serial number
#AutoIt3Wrapper_Res_Fileversion=V.2
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****

 ProgressOn("Progress Bar","Copying Mac Address(s)","Copying...")
For $i = 1 To 100
   Sleep(10)
   ProgressSet($i)
Next
Dim $macaddress[4]
Local $file = FileOpen("System.xls.", 1)
$compname = @ComputerName
; Check if file opened for writing OK
If $file = -1 Then
    MsgBox(0, "Error", "Unable to open file.")
    Exit
EndIf

RunWait(@ComSpec & " /c ipconfig /all > ipconfig.txt", "", @SW_HIDE)

If FileExists("ipconfig.txt") Then

$nics = 1

$macfile = FileOpen("ipconfig.txt", 0)
FileWriteLine($file, "Mac Address" & @CRLF)
While $nics < 5
    $macline = FileReadLine($macfile)
    If @error = -1 Then ExitLoop
    $macresult = StringInStr($macline, "Physical Address")
    If $macresult <> "0" Then
    $macline = StringStripWS($macline, 8)
    $macline = StringTrimLeft($macline, 25)
                $macarray = StringSplit($macline, "-")
    $macaddress[$nics] = $macarray[1] & $macarray[2] & $macarray[3] & _
                            $macarray[4] & $macarray[5] & $macarray[6]

    FileWriteLine($file, $macaddress[$nics] & @CRLF)
 EndIf

Wend
FileWriteLine($file, "Computer Name" & @CRLF)
FileWriteLine($file, $compname & @CRLF)
Send ("{Tab}"   )
FileWriteLine($file, "")
FileClose($macfile)
FileDelete("ipconfig.txt")
EndIf
ProgressSet(100, "Complete!")
Sleep(750)
ProgressOff()
Link to comment
Share on other sites

Check out >this script and modify it to your needs if it does what you want. It doesn't grab the MAC address, but it does get a lot of other information. I'm sure the MAC address can be obtained from WMI. As a matter of fact, this script, that I got from running the A3_Scriptomatic script, gives the MAC address of the network adapters. It also will list a lot of network connections that aren't real network cards, but you can filter for that by looking to see if it returns a MAC address.

;~ #RequireAdmin
; Generated by AutoIt Scriptomatic

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20
$colItems = ""
$strComputer = "."

$Output=""
$Output = $Output & "Computer: " & $strComputer  & @CRLF
$Output = $Output & "==========================================" & @CRLF
$objWMIService = ObjGet("winmgmts:\\" & $strComputer & "\ROOT\CIMV2")
$colItems = $objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapter", "WQL", _
                                          $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

If IsObj($colItems) then
   For $objItem In $colItems
      $Output = $Output & "AdapterType: " & $objItem.AdapterType & @CRLF
      $Output = $Output & "AdapterTypeId: " & $objItem.AdapterTypeId & @CRLF
      $Output = $Output & "AutoSense: " & $objItem.AutoSense & @CRLF
      $Output = $Output & "Availability: " & $objItem.Availability & @CRLF
      $Output = $Output & "Caption: " & $objItem.Caption & @CRLF
      $Output = $Output & "ConfigManagerErrorCode: " & $objItem.ConfigManagerErrorCode & @CRLF
      $Output = $Output & "ConfigManagerUserConfig: " & $objItem.ConfigManagerUserConfig & @CRLF
      $Output = $Output & "CreationClassName: " & $objItem.CreationClassName & @CRLF
      $Output = $Output & "Description: " & $objItem.Description & @CRLF
      $Output = $Output & "DeviceID: " & $objItem.DeviceID & @CRLF
      $Output = $Output & "ErrorCleared: " & $objItem.ErrorCleared & @CRLF
      $Output = $Output & "ErrorDescription: " & $objItem.ErrorDescription & @CRLF
      $Output = $Output & "GUID: " & $objItem.GUID & @CRLF
      $Output = $Output & "Index: " & $objItem.Index & @CRLF
      $Output = $Output & "InstallDate: " & WMIDateStringToDate($objItem.InstallDate) & @CRLF
      $Output = $Output & "Installed: " & $objItem.Installed & @CRLF
      $Output = $Output & "InterfaceIndex: " & $objItem.InterfaceIndex & @CRLF
      $Output = $Output & "LastErrorCode: " & $objItem.LastErrorCode & @CRLF
      $Output = $Output & "MACAddress: " & $objItem.MACAddress & @CRLF
      $Output = $Output & "Manufacturer: " & $objItem.Manufacturer & @CRLF
      $Output = $Output & "MaxNumberControlled: " & $objItem.MaxNumberControlled & @CRLF
      $Output = $Output & "MaxSpeed: " & $objItem.MaxSpeed & @CRLF
      $Output = $Output & "Name: " & $objItem.Name & @CRLF
      $Output = $Output & "NetConnectionID: " & $objItem.NetConnectionID & @CRLF
      $Output = $Output & "NetConnectionStatus: " & $objItem.NetConnectionStatus & @CRLF
      $Output = $Output & "NetEnabled: " & $objItem.NetEnabled & @CRLF
      $strNetworkAddresses = $objItem.NetworkAddresses()
      $Output = $Output & "NetworkAddresses: " & $strNetworkAddresses & @CRLF
      $Output = $Output & "PermanentAddress: " & $objItem.PermanentAddress & @CRLF
      $Output = $Output & "PhysicalAdapter: " & $objItem.PhysicalAdapter & @CRLF
      $Output = $Output & "PNPDeviceID: " & $objItem.PNPDeviceID & @CRLF
      $strPowerManagementCapabilities = $objItem.PowerManagementCapabilities()
      $Output = $Output & "PowerManagementCapabilities: " & $strPowerManagementCapabilities & @CRLF
      $Output = $Output & "PowerManagementSupported: " & $objItem.PowerManagementSupported & @CRLF
      $Output = $Output & "ProductName: " & $objItem.ProductName & @CRLF
      $Output = $Output & "ServiceName: " & $objItem.ServiceName & @CRLF
      $Output = $Output & "Speed: " & $objItem.Speed & @CRLF
      $Output = $Output & "Status: " & $objItem.Status & @CRLF
      $Output = $Output & "StatusInfo: " & $objItem.StatusInfo & @CRLF
      $Output = $Output & "SystemCreationClassName: " & $objItem.SystemCreationClassName & @CRLF
      $Output = $Output & "SystemName: " & $objItem.SystemName & @CRLF
      $Output = $Output & "TimeOfLastReset: " & WMIDateStringToDate($objItem.TimeOfLastReset) & @CRLF
      if Msgbox(1,"WMI Output",$Output) = 2 then ExitLoop
      $Output=""
   Next
Else
   Msgbox(0,"WMI Output","No WMI Objects Found for class: " & "Win32_NetworkAdapter" )
Endif


Func WMIDateStringToDate($dtmDate)

    Return (StringMid($dtmDate, 5, 2) & "/" & _
    StringMid($dtmDate, 7, 2) & "/" & StringLeft($dtmDate, 4) _
    & " " & StringMid($dtmDate, 9, 2) & ":" & StringMid($dtmDate, 11, 2) & ":" & StringMid($dtmDate,13, 2))
EndFunc

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 Gude
How 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

Link to comment
Share on other sites

Format the output as an array in your script, with the column header text as the top row, then look in the help file at _ExcelWriteArray. If the file only has to be in an Excel readable format, I would look at writing a CSV file. Here are some nice >CSV UDFs.

Also, here are some functions that I used when I had a similar project.
 

Func _GetMACFromComputerName($sComputer = @ComputerName, $sSeparator = "")

    Return _GetMACFromIP(_ComputerNameToIP($sComputer), $sSeparator)

EndFunc   ;==>_GetMACFromComputerName

Func _GetMACFromIP($sIP = @IPAddress1, $sSeparator = ":")
    ;Part of the code is from LazyCat in AutoIt Forums.

    Local $MAC, $MACSize
    Local $i, $s, $r, $iIP

    ;Create the struct
    ;{
    ;    char    data[6];
    ;}MAC
    $MAC = DllStructCreate("byte[6]")

    ;Create a pointer to an int
    ;    int *MACSize;
    $MACSize = DllStructCreate("int")

    ;*MACSize = 6;
    DllStructSetData($MACSize, 1, 6)

    ;call inet_addr($sIP)
    $r = DllCall("Ws2_32.dll", "int", "inet_addr", "str", $sIP)
    $iIP = $r[0]

    ;Make the DllCall
    $r = DllCall("iphlpapi.dll", "int", "SendARP", "int", $iIP, "int", 0, "ptr", DllStructGetPtr($MAC), "ptr", DllStructGetPtr($MACSize))

    ;Format the MAC address into user readble format: 00:00:00:00:00:00
    $s = ""
    For $i = 0 To 5
        If $i Then $s = $s & $sSeparator ;Added Separator variable to modify output format
        $s = $s & Hex(DllStructGetData($MAC, 1, $i + 1), 2)
    Next

    ;Must free the memory after it is used
    $MAC = 0
    $MACSize = 0

    If $s = "00:00:00:00:00:00" Then Return SetError(1, 0, "")
    ;Return the user readble MAC address
    Return $s
EndFunc   ;==>_GetMACFromIP

Func _ComputerNameToIP($sComputer)
;~  TCPStartup()  ;Needed for TCPNameToIP.  Put at top of script main section.
    $sIPAddress = TCPNameToIP($sComputer)
    If @error Then Return SetError(@error, 0, $sIPAddress)
;~  TCPShutdown()  ;Need for each TCPStartup.  Put at the bottom of the script main section.
    Return $sIPAddress
EndFunc   ;==>_ComputerNameToIP

 
 
Adam
 

Edited by AdamUL
Link to comment
Share on other sites

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...