How can I retrieve the LAN adapter MAC address? - windows

I want to retrieve the MAC address from a Windows system, only for LAN Adapter. Can you suggest to me how I'd handle this in VBScript?
I'm currently using this VBScript for getting the MAC address, but this is giving me results for all adapters, while I only want the MAC address when I am connected with LAN Adapter.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery _
("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
If objItem.ServiceName <> "VMnetAdapter" AND isNull(objItem.MACAddress)=0 Then
Wscript.Echo objItem.MACAddress
Wscript.Echo objItem.ServiceName
End if
Next

Use the Win32_NetworkAdapter class instead of the Win32_NetworkAdapterConfiguration class. The latter doesn't have a property providing the adapter name.
adaptername = "LAN Adapter"
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_NetworkAdapter WHERE NetConnectionID = '" & adaptername & "'"
For Each adapter In wmi.ExecQuery(qry)
If Not IsNull(adapter.MACAddress) Then Wscript.Echo adapter.MACAddress
Next

how about this
way 1:
if possible try to exclude all not required adapters (excluded VmWare and VirtualBox). of couse, on some computers could be more specific adapters which you need to find out and exclude
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
if objItem.ServiceName <> "VMnetAdapter" and objItem.ServiceName <> "VBoxNetAdp" and objItem.ServiceName <> "" and isNull(objItem.MACAddress) = 0 Then
Wscript.Echo objItem.ServiceName & vbCrLf & objItem.MACAddress
End if
Next
way 2:
find all adapters which has specific gateway
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapterConfiguration")
For Each objItem in colItems
if objItem.ServiceName <> "VMnetAdapter" and objItem.ServiceName <> "VBoxNetAdp" and objItem.ServiceName <> "" and isNull(objItem.MACAddress) = 0 Then
For Each strIP in objItem.DefaultIPGateway
If strIP = "192.168.1.1" Then
Wscript.Echo objItem.ServiceName & vbCrLf & objItem.MACAddress
End If
Next
End if
Next
https://msdn.microsoft.com/en-us/library/aa394217(v=vs.85).aspx

Try this,you will get MAC Address of LAN Adapter Only,
Set wmi = GetObject("winmgmts://./root/cimv2")
qry = "SELECT * FROM Win32_NetworkAdapter WHERE (NetConnectionID like '%Local Area Connection%')"
For Each adapter In wmi.ExecQuery(qry)
If Not IsNull(adapter.MACAddress) Then Wscript.Echo adapter.MACAddress
Next
I am using this code only and its working fine.

Related

Getting Volume Serial Number Of Script Location

here is a vbscript which shows all drives' volume serial numbers. But I need to customize to return only the volume serial number of the drive which the script is running from.
How to do it?
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
str = ""
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk")
For Each objItem In colItems
str = str & objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber & vbCrlf & vbCrlf
Next
MsgBox str
This should do what you need:
' Get the drive designator...
With CreateObject("Scripting.FileSystemObject")
strDrive = .GetDriveName(WScript.ScriptFullName)
End With
strComputer = "."
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_LogicalDisk WHERE DeviceId='" & strDrive & "'")
' There should only be one item in the collection...
For Each objItem In colItems
MsgBox objItem.Name & " SerialNumber: " & objItem.VolumeSerialNumber
Next

Couple questions I have regarding my VB Script

I really didn't know what to set the title as. Sorry for being too vague. I have a couple questions specific to my VB Script.
First off, I have a piece that determines how much RAM is installed in my PC. I would like it to output as one amount. Currently, it outputs each slot in my computer. For example...
Capacity:, 1024, Speed:, 1333
Capacity:, 1024, Speed:, 1333
Capacity:, 1024, Speed:, 1333
Capacity:, 1024, Speed:, 1333
I want it to output as one line, combined (in my case, 4 GB). Here is my code:
'Finds the computer's RAM capacity and speed.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory")
For Each objItem in colItems
convertedResult = objItem.Capacity/1048576
MyFile.WriteLine ("Capacity:, " & convertedResult & ", Speed:, " & objItem.Speed)
Next
My next issue is I want to find out what network adapters are in my PC, and their MAC address. I simply want to find the physical LAN adapters and WLAN adapters. I do not want any virtual adapters.
'Finds the computer's network adapters' name and MAC address (this includes virtual adapters).
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_NetworkAdapter")
For Each objItem in colItems
MyFile.WriteLine ("Name:, " & objItem.Name & ", MAC Address:, " & objItem.MACAddress)
Next
For the memory, try this:
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_PhysicalMemory")
dim total
For Each objItem in colItems
convertedResult = objItem.Capacity/1048576
total = total + convertedResult
Next
MyFile.WriteLine ("Capacity:, " & total )

vbscript WMI Using Public Member Functions (nVidia NV2)

I am trying to use some nVidia functions in there WMI API (attached, it is a txt file but should be renamed to chm for help file)
I am new to vbscript so can be doing something wrong.
My code is as follows:
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20
arrComputers = Array(".")
For Each strComputer In arrComputers
WScript.Echo
WScript.Echo "=========================================="
WScript.Echo "Computer: " & strComputer
WScript.Echo "=========================================="
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
Set colItems = objWMIService.ExecQuery("SELECT * FROM SyncTopology", "WQL", _
wbemFlagReturnImmediately + wbemFlagForwardOnly)
For Each objItem In colItems
NodeID = objItem.id
WScript.Echo "id: " & NodeID
WScript.Echo
Next
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2\NV")
Set SyncClass = objWMIService.Get("Sync")
if (SyncClass.toggleSource()) Then
wscript.echo "done!"
End if
Next
I am actually trying to use a different function but this one is the easiest and takes no arguments.
The class is 'Sync' the function is toggleSource, should be too easy!
I get an error on line:
if (SyncClass.toggleSource()) Then
stating:
C:\Users\User\Desktop\test3.vbs(28, 2) SWbemObjectEx: Invalid method Parameter(s)
I can query attributes in the class just can run methods :(
I can use these methods in Powershell so they should work, just can't get it working in vbscript!! AHHH...

Using VBScript how can I check if the Spooler service is started and if not start it?

I'd like to use VBScript to check if the Spooler service is started and if not start it, the code below checks the service status but I need some help modifying this so I can check if it is started.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("Select * from Win32_Service")
For Each objService in colRunningServices
Wscript.Echo objService.DisplayName & VbTab & objService.State
Next
Many thanks
Steven
How about something like this. This command will start it if it isn't already running. No need to check in advance.
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run "NET START spooler", 1, false
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colRunningServices = objWMIService.ExecQuery _
("select State from Win32_Service where Name = 'Spooler'")
For Each objService in colRunningServices
If objService.State <> "Running" Then
errReturn = objService.StartService()
End If
Next
Note you can also use objService.started to check if its started.
Just for the completeless, here's an alternative variant using the Shell.Application object:
Const strServiceName = "Spooler"
Set oShell = CreateObject("Shell.Application")
If Not oShell.IsServiceRunning(strServiceName) Then
oShell.ServiceStart strServiceName, False
End If
Or simply:
Set oShell = CreateObject("Shell.Application")
oShell.ServiceStart "Spooler", False ''# This returns False if the service is already running

How get real time voltage?

Is there a way to retrieve the voltage being supplied to a computer in real time?
You can use WMI to get some of that information. I used the WMI Code Creator to come up with this VB script which returns the Win32_Processor CurrentVoltage value. If you have a laptop, there is also some battery information available in WMI.
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\CIMV2")
Set colItems = objWMIService.ExecQuery( _
"SELECT * FROM Win32_Processor",,48)
For Each objItem in colItems
Wscript.Echo "-----------------------------------"
Wscript.Echo "Win32_Processor instance"
Wscript.Echo "-----------------------------------"
Wscript.Echo "CurrentVoltage: " & objItem.CurrentVoltage
Next

Resources