Solved

How to grab IP address and MAC address using existing VB Script

Posted on 2009-05-07
12
2,876 Views
Last Modified: 2013-11-26
The code below will allow me to grab the serial, express and model number from a computer and email the results. I am having a heck of time figuring out how to grab the IP Address and Mac Address though.

I don't know ANY VB, so, any help is appreciated.

Thanks
'strComputer = InputBox ("Enter your hostname")
 
	Dim WSHShell
	Dim objNTInfo
	Dim GetComputerName
 
	Set objNTInfo = CreateObject("WinNTSystemInfo")
	strComputer = lcase(objNTInfo.ComputerName)
 
strMyEmailAddr = InputBox("This script grabs your computer's serial, express code and model number. Please enter in your email address")
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
For Each objItem in colItems
strSerial = objItem.SerialNumber
Next
 
 
 
Function model(strComputer)
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48) 
For Each objItem in colItems
model = objItem.Model 
next
End Function
 
Function Base2Base(InputNumber,InputBase,OutputBase)
Dim J, K, DecimalValue, X, MaxBase, InputNumberLength
Dim NumericBaseData, OutputValue
NumericBaseData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MaxBase = Len(NumericBaseData)
if (InputBase > MaxBase) OR (OutputBase > MaxBase) then
Base2Base = "N/A"
Exit Function
end if
'Convert InputNumber to Base 10
InputNumberLength = Len(InputNumber)
DecimalValue = 0
for J = 1 to InputNumberLength
for K = 1 to InputBase
if mid(InputNumber, J, 1) = mid(NumericBaseData, K, 1) then
DecimalValue = DecimalValue+int((K-1)*(InputBase^(InputNumberLength-J))+.5)
end if
next
next
'Convert the Base 10 value (DecimalValue) to the desired output base
OutputValue = ""
while DecimalValue > 0
X = int(((DecimalValue/OutputBase)-int(DecimalValue/OutputBase))*OutputBase+1.5)
OutputValue = mid(NumericBaseData, X, 1)+OutputValue
DecimalValue = int(DecimalValue/OutputBase)
Wend
Base2Base = OutputValue
Exit Function
End Function
 
 
 
 
 
'==Message boxes.
 
WScript.Echo "Your Serial: " & strSerial
Wscript.Echo "Express code: " & Base2Base(strSerial, 36, 10)
WScript.Echo "Model:" &  model(strComputer)
WScript.Echo "IP Address:" &  objValue
 
MsgBox("Thank You")
 
 
'==Email
 
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Inventory Info"
objMessage.From = strMyEmailAddr
objMessage.To = "you@email.com"
objMessage.TextBody = "Your Serial: " & strSerial & vbCrLf & "Express Code: " & Base2Base(strSerial, 36, 10) &  vbCrLf & "Model:"  & model(strComputer)& vbCrLf & "IP Address:" & objValue
 
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
 
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"
 
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 
objMessage.Configuration.Fields.Update
 
'==End remote SMTP server configuration section==
 
objMessage.Send

Open in new window

0
Comment
Question by:UDDevHelp
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 2
12 Comments
 

Author Comment

by:UDDevHelp
ID: 24330194
I was using this code, but I couldn't get the IP Address or MAC to display with the Serial, Model and Express code.
'==Grabs MAC and IP Address.
 
On Error Resume Next
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_NetworkAdapterConfiguration")
 
For Each objItem In colItems
For Each objValue In objItem.IPAddress
If objValue <> "" Then
 
MsgBox(objValue)
 
WScript.Echo "Description -- " & objItem.Description & vbcrlf & "IPAddress -- " & objValue & vbcrlf & "MAC: " & objItem.MACAddress
 
MsgBox(objValue)
 
End If
Next
Next
 
 
'==End of Grab Mac and IP Address. 

Open in new window

0
 
LVL 2

Assisted Solution

by:sal-ee
sal-ee earned 500 total points
ID: 24335872
Here is a script that will give you the MAC, Host name, IP, Subnet, Default Gateway, as you would get from ipconfig in Command Prompt.

If you don't need to the other info, just the MAC and IP, you can easily edit this script to just give you those.

Hope it helps! :o)

' List IP Configuration Data
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
n = 1
WScript.Echo
 
For Each objAdapter in colAdapters
   WScript.Echo "Network Adapter " & n
   WScript.Echo "================="
   WScript.Echo "  Description: " & objAdapter.Description
 
   WScript.Echo "  Physical (MAC) address: " & objAdapter.MACAddress
   WScript.Echo "  Host name:              " & objAdapter.DNSHostName
 
   If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
         WScript.Echo "  IP address:             " & objAdapter.IPAddress(i)
      Next
   End If
 
   If Not IsNull(objAdapter.IPSubnet) Then
      For i = 0 To UBound(objAdapter.IPSubnet)
         WScript.Echo "  Subnet:                 " & objAdapter.IPSubnet(i)
      Next
   End If
 
   If Not IsNull(objAdapter.DefaultIPGateway) Then
      For i = 0 To UBound(objAdapter.DefaultIPGateway)
         WScript.Echo "  Default gateway:        " & _
             objAdapter.DefaultIPGateway(i)
      Next
   End If
 
Next

Open in new window

0
 

Author Comment

by:UDDevHelp
ID: 24338932
This works and I appreciate you posting the code. But, I need help implementing code to grab the IP and MAC address with my existing script.
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 2

Accepted Solution

by:
sal-ee earned 500 total points
ID: 24345172
OK, I have just simply added the snippet I previously posted into your script.

Check if this works, i.e. pulls the IP and MAC and we can then look at tidying the script up. Just check if it works first.

'strComputer = InputBox ("Enter your hostname")
 
        Dim WSHShell
        Dim objNTInfo
        Dim GetComputerName
 
        Set objNTInfo = CreateObject("WinNTSystemInfo")
        strComputer = lcase(objNTInfo.ComputerName)
 
strMyEmailAddr = InputBox("This script grabs your computer's serial, express code and model number. Please enter in your email address")
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
 
For Each objItem in colItems
strSerial = objItem.SerialNumber
Next
 
 
Function model(strComputer)
'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
 
For Each objItem in colItems
model = objItem.Model 
next
End Function
 
Function Base2Base(InputNumber,InputBase,OutputBase)
Dim J, K, DecimalValue, X, MaxBase, InputNumberLength
Dim NumericBaseData, OutputValue
NumericBaseData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MaxBase = Len(NumericBaseData)
if (InputBase > MaxBase) OR (OutputBase > MaxBase) then
Base2Base = "N/A"
Exit Function
end if
'Convert InputNumber to Base 10
InputNumberLength = Len(InputNumber)
DecimalValue = 0
for J = 1 to InputNumberLength
for K = 1 to InputBase
if mid(InputNumber, J, 1) = mid(NumericBaseData, K, 1) then
DecimalValue = DecimalValue+int((K-1)*(InputBase^(InputNumberLength-J))+.5)
end if
next
next
'Convert the Base 10 value (DecimalValue) to the desired output base
OutputValue = ""
while DecimalValue > 0
X = int(((DecimalValue/OutputBase)-int(DecimalValue/OutputBase))*OutputBase+1.5)
OutputValue = mid(NumericBaseData, X, 1)+OutputValue
DecimalValue = int(DecimalValue/OutputBase)
Wend
Base2Base = OutputValue
Exit Function
End Function
 
' Get IP and MAC Address
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
n = 1
 
For Each objAdapter in colAdapters
 
   strMACAddress = objAdapter.MACAddress
 
   If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
         objValue = objAdapter.IPAddress(i)
      Next
   End If
 
Next 
 
 
'==Message boxes.
 
WScript.Echo "Your Serial: " & strSerial
Wscript.Echo "Express code: " & Base2Base(strSerial, 36, 10)
WScript.Echo "Model:" &  model(strComputer)
WScript.Echo "IP Address:" &  objValue
WScript.Echo "MAC Address:" &  strMACAddress
 
MsgBox("Thank You")
 
 
'==Email
 
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Inventory Info"
objMessage.From = strMyEmailAddr
objMessage.To = "you@email.com"
objMessage.TextBody = "Your Serial: " & strSerial & vbCrLf & "Express Code: " & Base2Base(strSerial, 36, 10) &  vbCrLf & "Model:"  & model(strComputer)& vbCrLf & "IP Address:" & objValue & vbCrLf & "MAC Address:" & strMACAddress
 
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
 
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"
 
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 
objMessage.Configuration.Fields.Update
 
'==End remote SMTP server configuration section==
 
objMessage.Send

Open in new window

0
 

Author Comment

by:UDDevHelp
ID: 24356185
This works BUT it's grabbing the IP V6 IP Address, not the IPV4 IP
0
 
LVL 3

Expert Comment

by:ziad270
ID: 24356251
Hum... Have you heard about OCSInventory-NG ?
The opensource project ?
http://www.oscinventory-ng.org

You really should consider it.

Just deploying this agent you will get all the information you could dream of on all computers.
0
 

Author Comment

by:UDDevHelp
ID: 24356257
The link you posted doesn't work..
0
 

Author Comment

by:UDDevHelp
ID: 24356302
Thanks for posting the link, I corrected it above.

BUT, this script it near completion. Just need to grab the IP V4 address instead of the IP V6
0
 

Author Comment

by:UDDevHelp
ID: 24358313
It's funny, the first script sal-ee posted grabs the IP V4 Address and the IP V 6 address.....but, in the script that where sal-ee integrated his script with mine, it only grabs IP V6 address

Any ideas?

In your first script, it seems as though it loops through all the Network Adapters. But, the second integrated one only gets IP V6  info...
0
 

Author Comment

by:UDDevHelp
ID: 24423627
Anyone have any ideas? This script will do everything I need it to do, but, it only grabs IPV6 addresses and not the IPV4 address...
'strComputer = InputBox ("Enter your hostname")
 
        Dim WSHShell
        Dim objNTInfo
        Dim GetComputerName
 
        Set objNTInfo = CreateObject("WinNTSystemInfo")
        strComputer = lcase(objNTInfo.ComputerName)
 
strMyEmailAddr = InputBox("This script grabs your computer's serial, express code and model number. Please enter in your email address")
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
 
For Each objItem in colItems
strSerial = objItem.SerialNumber
Next
 
 
Function model(strComputer)
'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
 
For Each objItem in colItems
model = objItem.Model 
next
End Function
 
Function Base2Base(InputNumber,InputBase,OutputBase)
Dim J, K, DecimalValue, X, MaxBase, InputNumberLength
Dim NumericBaseData, OutputValue
NumericBaseData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MaxBase = Len(NumericBaseData)
if (InputBase > MaxBase) OR (OutputBase > MaxBase) then
Base2Base = "N/A"
Exit Function
end if
'Convert InputNumber to Base 10
InputNumberLength = Len(InputNumber)
DecimalValue = 0
for J = 1 to InputNumberLength
for K = 1 to InputBase
if mid(InputNumber, J, 1) = mid(NumericBaseData, K, 1) then
DecimalValue = DecimalValue+int((K-1)*(InputBase^(InputNumberLength-J))+.5)
end if
next
next
'Convert the Base 10 value (DecimalValue) to the desired output base
OutputValue = ""
while DecimalValue > 0
X = int(((DecimalValue/OutputBase)-int(DecimalValue/OutputBase))*OutputBase+1.5)
OutputValue = mid(NumericBaseData, X, 1)+OutputValue
DecimalValue = int(DecimalValue/OutputBase)
Wend
Base2Base = OutputValue
Exit Function
End Function
 
' Get IP and MAC Address
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colAdapters = objWMIService.ExecQuery _
    ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
n = 1
 
For Each objAdapter in colAdapters
 
   strMACAddress = objAdapter.MACAddress
 
   If Not IsNull(objAdapter.IPAddress) Then
      For i = 0 To UBound(objAdapter.IPAddress)
         objValue = objAdapter.IPAddress(i)
      Next
   End If
 
Next 
 
 
'==Message boxes.
 
WScript.Echo "Your Serial: " & strSerial
Wscript.Echo "Express code: " & Base2Base(strSerial, 36, 10)
WScript.Echo "Model:" &  model(strComputer)
WScript.Echo "IP Address:" &  objValue
WScript.Echo "MAC Address:" &  strMACAddress
 
MsgBox("Thank You")
 
 
'==Email
 
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Inventory Info"
objMessage.From = strMyEmailAddr
objMessage.To = "you@email.com"
objMessage.TextBody = "Your Serial: " & strSerial & vbCrLf & "Express Code: " & Base2Base(strSerial, 36, 10) &  vbCrLf & "Model:"  & model(strComputer)& vbCrLf & "IP Address:" & objValue & vbCrLf & "MAC Address:" & strMACAddress
 
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
 
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.domain.com"
 
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 
objMessage.Configuration.Fields.Update
 
'==End remote SMTP server configuration section==
 
objMessage.Send

Open in new window

0
 

Author Comment

by:UDDevHelp
ID: 24443358
Got it! A co-worker helped me figure it out!

This code is helpful for anyone who needs to take inventory of computers and users. It will grab the model number, serial number, express code, Mac Address and IP Address. Then, it will email the results to you.

Written in Visual Basic. Save file as "something.vbs"


'strComputer = InputBox ("Enter your hostname")
 
		Dim WSHShell
		Dim objNTInfo
		Dim GetComputerName
 
		Set objNTInfo = CreateObject("WinNTSystemInfo")
		strComputer = lcase(objNTInfo.ComputerName)
 
strMyEmailAddr = InputBox("This script grabs your computer's serial, express code and model number. Please enter in your email address")
 
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_BIOS",,48)
 
For Each objItem in colItems
strSerial = objItem.SerialNumber
Next
 
 
Function model(strComputer)
'Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
Set colAdapters = objWMIService.ExecQuery _
	("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") 
 
For Each objItem in colItems
model = objItem.Model 
next
End Function
 
Function Base2Base(InputNumber,InputBase,OutputBase)
Dim J, K, DecimalValue, X, MaxBase, InputNumberLength
Dim NumericBaseData, OutputValue
NumericBaseData = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
MaxBase = Len(NumericBaseData)
if (InputBase > MaxBase) OR (OutputBase > MaxBase) then
Base2Base = "N/A"
Exit Function
end if
'Convert InputNumber to Base 10
InputNumberLength = Len(InputNumber)
DecimalValue = 0
for J = 1 to InputNumberLength
for K = 1 to InputBase
if mid(InputNumber, J, 1) = mid(NumericBaseData, K, 1) then
DecimalValue = DecimalValue+int((K-1)*(InputBase^(InputNumberLength-J))+.5)
end if
next
next
'Convert the Base 10 value (DecimalValue) to the desired output base
OutputValue = ""
while DecimalValue > 0
X = int(((DecimalValue/OutputBase)-int(DecimalValue/OutputBase))*OutputBase+1.5)
OutputValue = mid(NumericBaseData, X, 1)+OutputValue
DecimalValue = int(DecimalValue/OutputBase)
Wend
Base2Base = OutputValue
Exit Function
End Function
 
' Get IP and MAC Address
 
strComputer = "."
 
Set objWMIService = GetObject("winmgmts:" _
	& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
 
Set colAdapters = objWMIService.ExecQuery _
	("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")
 
n = 1
 
ipcount = 0
For Each objAdapter in colAdapters
 
   strMACAddress = objAdapter.MACAddress
	
   If Not IsNull(objAdapter.IPAddress) Then
	  For i = 0 To UBound(objAdapter.IPAddress)
		 ReDim Preserve ipAddys(ipcount)
		 ipAddys(ipcount) = objAdapter.IPAddress(i)
		 ipcount = ipcount + 1
	  Next
   End If
 
 
 
 
Next 
 
 
'==Message boxes.
 
WScript.Echo "Your Serial: " & strSerial
Wscript.Echo "Express code: " & Base2Base(strSerial, 36, 10)
WScript.Echo "Model:" &  model(strComputer)
For i = 0 To ipcount-1
  WScript.Echo "IP Address:" &  ipAddys(i)
Next
 
WScript.Echo "MAC Address:" &  strMACAddress
 
MsgBox("Thank You")
 
 
'==Email
 
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = "Inventory Info"
objMessage.From = strMyEmailAddr
objMessage.To = "yourEmail@email.com"
objMessage.TextBody = "Your Serial: " & strSerial & vbCrLf & "Express Code: " & Base2Base(strSerial, 36, 10) &  vbCrLf & "Model:"  & model(strComputer)& vbCrLf & "IP Address:" & ipAddys(0) & vbCrLf & "MAC Address:" & strMACAddress
 
 
'==This section provides the configuration information for the remote SMTP server.
'==Normally you will only change the server name or IP.
 
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 
'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.emailServer.com"
 
'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 
objMessage.Configuration.Fields.Update
 
'==End remote SMTP server configuration section==
 
objMessage.Send

Open in new window

0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question