'USAGE:
'cscript getmacinfo.vbs /s:<source file> /l:<log file>
'
'/s Enter the location of the source file containing computer names and/or IP addresses. For example; /s:"c:\Temp\my file.txt"
'/l Enter the location of the log file. For example; /l:"C:\Temp\my log file.txt"
'
'Example:
' cscript getmacinfo.vbs /s:"c:\Temp\my file.txt" /l:"C:\Temp\my log file.txt"
'
'COMMENTS:
'Features of this script;
' This script will open a file containing computer names and/or IP addresses.
' The script will run the command getmac against each line item from the file.
' A log file will be created with the output.
Dim colNamedArguments
Dim strSource
Dim strLog
'Check that the script is running under cscript
checkforcscript()
'Get arguments from command line
Set colNamedArguments = WScript.Arguments.Named
strSource = colNamedArguments.Item("s")
strLog = colNamedArguments.Item("l")
'Check for help key
if WScript.Arguments.Named.Exists("?") OR WScript.Arguments.Named.Exists("h") Then
FoundError("ArgumentNotFound")
wscript.quit 1
end if
if strSource = "" OR strLog = "" then
FoundError("ArgumentNotFound")
wscript.quit 1
end if
ReadFileAndExecuteScript()
Sub ReadFileAndExecuteScript()
'Open the source file and read in all the lines
Set objFSORead = CreateObject("scripting.filesystemobject")
Set objReadFile = objFSORead.OpenTextFile(strSource, 1)
strContents = objReadFile.ReadAll
arrLines = Split(strContents,vbCRLF)
objReadFile.Close
'Create a new file that will be the converted CSV file
Set objFSOWrite = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSOWrite.OpenTextFile(strLog, 2, True)
for each strTarget in arrLines
Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.exec("getmac /S " & strTarget & " /FO CSV /NH")
'wait for getmac to finish
do while WshExec.Status = 0
wscript.sleep 100
loop
'write output to log file
Do While Not(WshExec.StdOut.AtEndOfStream)
strResults = WshExec.StdOut.ReadLine
objFile.WriteLine """" & strTarget & """," & strResults
Loop
next
objFile.Close
End Sub
Sub checkforcscript()
'If not running under cscript, relaunch using command line
Set oShell = CreateObject("Wscript.Shell")
If Not WScript.FullName = wscript.Path & "\cscript.exe" Then
oShell.Popup "Launched using wscript. Relaunching...",3,"WSCRIPT"
oShell.Run "cmd.exe /k " & wscript.Path & "\cscript.exe //NOLOGO " & Chr(34) & wscript.scriptFullName & Chr(34) & " /?",1,False
wscript.quit
End If
End Sub
Sub FoundError(sCase)
'Error messages
select case sCase
case "ArgumentNotFound"
wscript.echo "cscript getmacinfo.vbs /s:<source file> /l:<log file>"
wscript.echo ""
wscript.echo "/s Enter the location of the source file containing computer names and/or"
wscript.echo " IP addresses."
wscript.echo " For example; /s:""c:\Temp\my file.txt"""
wscript.echo "/l Enter the location of the log file."
wscript.echo " For example; /l:""C:\Temp\my log file.txt"""
wscript.echo ""
wscript.echo "Example:"
wscript.echo " cscript getmacinfo.vbs /s:""c:\Temp\my file.txt"" /l:""C:\Temp\my log file.txt"""
end select
End Sub
Toggle HighlightingOpen in New WindowSelect All
|