Parsing then Copying Mac address to clipboard

September 3, 2010 at 09:53:41
Specs: Windows 7
I am wondering about an easy way to copy both a desktop or a laptop's physical address of the network card (not the wireless card for a notebook) to the clipboard so it can easily be pasted into our registration page.

We currently use a VB script to output it and manually type it in based on the output that looks like this...

Option Explicit

Dim strCPU, strRAM, strHDD, strOS, strNET, strUSER, colItems, strMsgBoxString, strComputer, objWMIService, objItem

'****************************** CRITICAL WMI OPERATIONS ******************************
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
'****************************** CRITICAL WMI OPERATIONS ******************************

'************************************************************************************************************************
'****************************************** MAIN ************************************************
'************************************************************************************************************************
Sub Main()
strMsgBoxString = ""
strMsgBoxString = strMsgBoxString & _
" USER " & Chr(13) & _
strUSER & _
" OS " & Chr(13) & _
strOS & _
" CPU " & Chr(13) & _
strCPU & _
" RAM " & Chr(13) & _
strRAM & _
" HDD " & Chr(13) & _
strHDD & _
" NET " & Chr(13) & _
strNET

End Sub

'************************************************************************************************************************
'****************************************** MAIN ************************************************
'************************************************************************************************************************

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' CPU
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
Sub GetCPU()
strCPU = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_Processor")
On Error Resume Next
For Each objItem in colItems
strCPU = strCPU & "CPU Speed : " & objItem.MaxClockSpeed & "Mhz" & Chr(13)
Next
'MsgBox strCPU,,"CPU"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' RAM Bytes/1048576 -->MB
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**

Sub GetRAM()
Dim intRAM
strRAM = ""
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory")
On Error Resume Next
For Each objItem in colItems
intRAM = intRAM + CDbl(objItem.Capacity)
Next

intRAM = intRAM\1048576
strRAM = "System RAM : " & CStr(intRAM) & " MB" & Chr(13)
'MsgBox strRAM,,"RAM"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' HDD SIZE Bytes/1048576 -->MB ****** Bytes/1073741824 -->GB
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**

Sub GetHDD()
strHDD = ""
Set colItems = objWMIService.ExecQuery ("Select * from Win32_DiskDrive")
On Error Resume Next
For each objItem in colItems
strHDD = strHDD & "Size : " & Cint(Cdbl(objItem.Size)/1073741824) & " GB" & Chr(13)
strHDD = strHDD & "Partitions : " & objItem.Partitions & Chr(13)
Next
'MsgBox strHDD,,"HDD"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' USER
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
Sub GetUser()

Dim objItem
strUSER = ""
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem")
On Error Resume Next
For Each objItem in colItems
strUSER = strUSER & "Computer Name : " & objItem.Name & Chr(13)
strUSER = strUSER & "User Name : " & objItem.UserName & Chr(13)
strUSER = strUSER & "Domain : " & objItem.Domain & Chr(13)
Next
'MsgBox strUSER,,"User"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' OS
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**

Sub GetOS()
strOS = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem")
On Error Resume Next
For Each objItem in colItems
strOS = strOS & "OS : " & objItem.Caption & " SP " & objItem.ServicePackMajorVersion & "." & _
objItem.ServicePackMinorVersion & " Version: " & objItem.Version & Chr(13)
Next

Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_ComputerSystem",,48)
For Each objItem in colItems
strOS = strOS & "Manufacturer : " & objItem.Manufacturer & Chr(13)
strOS = strOS & "Model : " & objItem.Model & Chr(13)
' strOS = strOS & "Model: " & objItem.Model & Chr(13)
' strOS = strOS & "Name: " & objItem.Name & Chr(13)
' strOS = strOS & "Total Memory: ~" & objItem.TotalPhysicalMemory/1024000 & " mb" & Chr(13)
Next


Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_BIOS")
For Each objItem in colItems
strOS = strOS & "Serial # : " & objItem.SerialNumber & Chr(13)
'strOS = strOS & "Version: " & objItem.Version & Chr(13)
Next
'MsgBox strOS,,"OS"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' NETWORK
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**

Sub GetNetwork()
Dim LoopDNS,i,Mac_Var,ipgateway, intAdapterNum
strNET = ""
intAdapterNum = 0
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration where IPEnabled = TRUE")
On Error Resume Next
For Each objItem in colItems
intAdapterNum = intAdapterNum + 1
strNET = strNET & "Network Adapter " & intAdapterNum & " : " & objItem.Description & Chr(13)
If Not IsNull(objItem.IPAddress) Then
For i = 0 To UBound(objItem.IPAddress)
strNET = strNET & "Current IP address : " & objItem.IPAddress(i) & Chr(13)
Next
End If
MAC_Var = objItem.MACAddress
strNET = strNET & "MAC address : " & MAC_Var & Chr(13)
strNET = strNET & "DHCP enabled : " & objItem.DHCPEnabled & Chr(13)
For each ipgateway in objItem.DefaultIPGateway
strNET = strNET & "Gateway : " & ipgateway & Chr(13)
next
For LoopDNS = 0 to Ubound(objItem.DNSServerSearchOrder)
strNET = strNET & "DNS Server : " & objItem.DNSServerSearchOrder(LoopDNS) & Chr(13)
Next
Next
'MsgBox strNET,,"NET"
End Sub

'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
' OUTPUT
'**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**
GetCPU()

GetHDD()
GetUser()
GetOS()
GetNetwork()
GetRAM()
Main()
MsgBox strMsgBoxString,,"AHC-IS"

Can only the physical address of the wired network card be copied to the clipboard?

Would it be better to use a command such as

ipconfig /all > ipresult.txt and parse the Physical address of the Local Area Connection

The important thing is that if run on a laptop it has to ignore the wireless card physical address. I have no experience parsing a text file and copying the results to the clipboard. Any help from an expert would be greatly appreciated. Thank you for your time in advance.


See More: Parsing then Copying Mac address to clipboard

Report •


#1
September 3, 2010 at 13:00:32
Vista and Windows 7 come with a command line utility called "clip.exe" which copies a command to the clipboard.

Try using this:

@ECHO OFF

FOR /f "tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"') DO (
	SET PA=%%a
	IF NOT "%PA%"=="." GOTO Done
)

:Done

ECHO %PA%| CLIP
CLS
ECHO The Physical Address has been copied to the clipboard.
ECHO.
PAUSE
EXIT

I'm on xp at the moment so I couldn't test it.

Let me know!

Can I put a question here?


Report •

#2
September 3, 2010 at 13:07:39
If you're going to support WinXP, then there's no real easy way to do it. You could use WMI to get the MAC, and make sure the script runs though cscript, which will let you copy/paste the MAC.
If Not StdOutGood Then
  CreateObject("WScript.Shell").Run _
    "cmd /c cscript //nologo """ & WScript.ScriptFullName & """ &PAUSE"
  WScript.Quit
End If
WScript.StdOut.WriteLine "Ethernet MAC listing:"
For Each net In GetObject("winmgmts:").ExecQuery( _
                                       "SELECT * FROM Win32_NetworkAdapter " & _
                          "WHERE AdapterTypeID = 0 AND NetConnectionStatus = 2")
  WScript.StdOut.WriteLine net.Description & vbTab & net.MACAddress
Next 'net

Function StdOutGood
  On Error Resume Next
  WScript.StdOut.WriteLine ""
  StdOutGood = Not CBool(Err)
End Function


Report •

#3
September 3, 2010 at 13:45:08
Matt123-
This works perfectly in Vista and 7 if it is a desktop. On the laptop I tested it on, it grabs the physical address of the bluetooth adaptor. Any ideas on making it always grab the wired network adaptor physical address. This would always be plugged into the network at the time of running this, if that helps.

Report •

Related Solutions

#4
September 3, 2010 at 13:49:24
Razor 2.3-
That works, and gives me exactly what I need from both a laptop and a desktop running windows xp (which is our current supported Windows OS), but doesnt allow me to copy and paste the output. I am not sure if I didnt do something correct but I created a vbs script and ran it through command line with a command similar to...

cscript x.vbs


Report •

#5
September 3, 2010 at 14:23:18
It utlizes the Command Prompt, so you have to ( Right Click -> Mark -> Make your selection -> Right Click ) to copy text.

Report •

#6
September 3, 2010 at 14:33:26
Razor 2.3
Oh that makes sense. This is very helpful. Thank you so much for the assistance.

Report •

#7
September 3, 2010 at 17:02:47
@ECHO OFF

SET Number=1

FOR /f "tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"') DO (
SET PA=%%a

)

:Done

ECHO %PA%| CLIP
CLS
ECHO The Physical Address has been copied to the clipboard.
ECHO.
PAUSE
EXIT

Can I put a question here?


Report •

#8
September 3, 2010 at 17:04:32
@ECHO OFF

SET Number=1

FOR /f "tokens=12 delims= " %%a IN ('IPCONFIG /all^|FINDSTR "Physical Address"') DO (
	SET PA=%%a
	IF Number=3 GOTO Done
)

:Done

ECHO %PA%| CLIP
CLS
ECHO The Physical Address has been copied to the clipboard.
ECHO.
PAUSE
EXIT

Try that, on the laptop at least. I'm not sure if it grabs the right one on on the desktops.

Let me know.

Can I put a question here?


Report •


Ask Question