Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

access to the mac address

Status
Not open for further replies.

hannable80

Technical User
Apr 5, 2006
28
GB
Hi all,

Thanks for the help last time now i must ask for it again.
Can any one tell me where im going wrong trying to access the a string to use a statement to grab the mac address of a pc. keep getting an error.
Cheers

Dim objShell, strComputer, strInput
Dim strPing

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\computerlist.txt", ForReading)

Const ForReading = 1
Const ForAppending = 8

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("c:\output.txt") Then

Else
Set objFile = objFSO.CreateTextFile("c:\output.txt")
End If

set objFile = nothing

Set objFile = objFSO.OpenTextFile("c:\output.txt", ForAppending)

For Each strLine in arrFileLines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '"_
& strLine & "'")
Set objMac = ExecQuery _
("Select * From Win32_NetworkAdapterConfiguration Where objStatus.StatusCode=<>1)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objFile.WriteLine("Computer " & strLine & " is not reachable" & vbTab & objStatus.ProtocolAddress)
Else
objFile.WriteLine("Computer " & strLine & " is reachable *********************"& vbTab & objStatus.ProtocolAddress&
End If
Next
Next
objFile.Close
 
What error do you keep getting?

HarleyQuinn
---------------------------------
The most overlooked advantage to owning a computer is that if they foul up there's no law against wacking them around a little. - Joe Martin

Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 



Hi,

Maybe terminate the string...
Code:
    Set objMac = ExecQuery _
        ("Select * From Win32_NetworkAdapterConfiguration Where objStatus.StatusCode=<>1[red][b]"[/b][/red])

Skip,
[sup][glasses]Don't let the Diatribe...
talk you to death![tongue][/sup][sub]
[glasses]Just traded in my old subtlety...
for a NUANCE![tongue][/sub]
 
nope getting a syntax error on that one . I know i will have to output it i havent put that in yet. just want to get it running ...need to get the strcomputer into the
Set objMac = ExecQuery
and for it to iterate down! I think any ideas ?
 
> undetermined string const...
> nope getting a syntax error on that one
No! You're getting "Unterminated string const", and the compiler gives you the line number to help you find the offending line.

You need to fix it as Skip suggested (as well as the unterminated string in the last WrireLine) before you can compile the program, and begin getting "strcomputer" where you want it!

 
I modified your script a bit, but this will get you the MAC address. The script could be cleaned up if you like.

Dim objShell, strComputer, strInput
Dim strPing

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\computerlist.txt", ForReading)

Const ForReading = 1
Const ForAppending = 8

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("c:\output.txt") Then

Else
Set objFile = objFSO.CreateTextFile("c:\output.txt")
End If

set objFile = nothing

Set objFile = objFSO.OpenTextFile("c:\output.txt", ForAppending)

For Each strLine in arrFileLines
Set objWMIService = GetObject("winmgmts:\\" & strLine & "\root\CIMV2")
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '" & strLine & "'")
Set objMac = objWMIService.ExecQuery("Select * From Win32_NetworkAdapterConfiguration")
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objFile.WriteLine("Computer " & strLine & " is not reachable" & vbTab & objStatus.ProtocolAddress)
Else
For Each objState In objMac
If Not IsNull(objState.MACAddress) Then
objFile.WriteLine("Computer " & strLine & " is reachable *********************"& vbTab & objState.MACAddress)
End If
Next
End If
Next
Next
objFile.Close
 
The (op's) script is wrong in many details, not only the closing quotes! Some are critical, some not. In any case, macaddress is never extracted. There could be multiple returned value... decide what you want. Also the naming might become misleading neither, choose your preferred naming.
[tt]
For Each strLine in arrFileLines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
ExecQuery("select * from Win32_PingStatus where address = '"_
& strLine & "'")
[red]'[/red]Set objMac = ExecQuery _
[red]'[/red]("Select * From Win32_NetworkAdapterConfiguration Where objStatus.StatusCode=<>1)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objFile.WriteLine("Computer " & strLine & " is not reachable" & vbTab & objStatus.ProtocolAddress)
Else
[blue]Set objMac = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strLine & "\root\cimv2"). _
ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")[/blue]
[blue]For each oMac in objMac[/blue]
objFile.WriteLine("Computer " & strLine & " is reachable *********************"& vbTab & objStatus.ProtocolAddress [blue]& "====== MacAddress:" & oMac.MacAddress[/blue]
[blue]Next[/blue]
End If
Next
Next
objFile.Close [/tt]
 
Looks like it working fine when i put in on "error resume next" but the output is wrong it gives the name of a pc 4 times and then 4 different mac address's beside it..
Is this something to do with the ping and the "For Each objState In objMac". If i do not put in the on error resume next it bottoms out
 
[1] First, there is a typo that I'd not removed. The corresponding line should read like this.

[tt] objFile.WriteLine[highlight] [/highlight]"Computer " & strLine & " is reachable *********************"& vbTab & objStatus.ProtocolAddress & "====== MacAddress:" & oMac.MacAddress[/tt]

[2] Then it all depends on what you put inside the computerlist.txt. If you put ip address outside of your authority to read via wmi, you would have error even if pingstatus is fine. Such as these.
[tt]
yahoo.com
google.com
[/tt]
You would succeed in pinging them, but sure you won't have any authority to read their server's macaddress. Hence, if you have no control on the list, you would need some error control on the wmi connection to it (objMac). The win32_pingstatus will not give you a licence of access to everywhere via wmi.
 
OKAY remote server or machine is not available or does not exist getobject. This list is taken from AD and i am the admin so win32_pingstatus should be fine.. Its a head scratcher
<code>

Dim objShell, strComputer, strInput
Dim strPing

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("c:\computerlist.txt", ForReading)

Const ForReading = 1
Const ForAppending = 8

Dim arrFileLines()
i = 0
Do Until objFile.AtEndOfStream
Redim Preserve arrFileLines(i)
arrFileLines(i) = objFile.ReadLine
i = i + 1
Loop
objFile.Close

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists("c:\output.txt") Then

Else
Set objFile = objFSO.CreateTextFile("c:\output.txt")
End If

set objFile = nothing

Set objFile = objFSO.OpenTextFile("c:\output.txt", ForAppending)
For Each strLine in arrFileLines
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}"). _
ExecQuery("select * from Win32_PingStatus where address = '"_
& strLine & "'")
'Set objMac = ExecQuery _
'("Select * From Win32_NetworkAdapterConfiguration Where objStatus.StatusCode=<>1)
For Each objStatus in objPing
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objFile.WriteLine("Computer " & strLine & " is not reachable" & vbTab & objStatus.ProtocolAddress)
Else
Set objMac = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strLine & "\root\cimv2"). _
ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
For each oMac in objMac
objFile.WriteLine "Computer " & strLine & " is reachable *********************"& vbTab & objStatus.ProtocolAddress & "====== MacAddress:" & oMac.MacAddress
Next
End If
Next
Next
objFile.Close
</code>
 
[3] Sysadm is not the sufficient condition to get access.
[tt]
If IsNull(objStatus.StatusCode) or objStatus.StatusCode<>0 Then
objFile.WriteLine("Computer " & strLine & " is not reachable" & vbTab & objStatus.ProtocolAddress)
Else
[blue]on error resume next[/blue]
Set objMac = GetObject("winmgmts:{impersonationLevel=impersonate}\\" & strLine & "\root\cimv2"). _
ExecQuery("Select * From Win32_NetworkAdapterConfiguration Where IPEnabled = True")
[blue]if err.number<>0 then
objFile.WriteLine "Computer " & strLine & " wmi access is denied" & vbTab & objStatus.ProtocolAddress & vbtab & "&H" & hex(err.number) & vbtab & err.description
err.clear
else[/blue]
For each oMac in objMac
objFile.WriteLine "Computer " & strLine & " is reachable *********************"& vbTab & objStatus.ProtocolAddress & "====== MacAddress:" & oMac.MacAddress
Next
[blue]end if[/blue]
[blue]on error goto 0[/blue]
End If
[/tt]
[4] There are different reasons the access is denied, such as firewall (especially vista). That you've to look into after the printout.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top