|
I've cleaned the scripts a bit, and attached them
as txt - rename vbs & edit domain-specific details to use. You need to
use either GetIPList.vbs or GetIPListXP.vbs, depending on client type you want
to query for MACs. (2k or XP/2k3) Put those in the same dir on the machine
that will gather your master MAC list, and edit wakeup.vbs to get the list from
there. Schedule GetIPList.vbs and/or GetIPListXP.vbs to run as often as
you would like to gather new MACs. You'll have to clean up the MAClist
manually if you want to, since I didn't bother to put that in there: it just
adds new machines/MACs when it sees them. From: Derek Harris Sent: Tuesday, October 11, 2005 6:57 PM To: '[email protected]' Subject: RE: [ActiveDir] WOL I use their wolcom.dll with a couple of _vbscript_s
-- works pretty nicely. From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Thommes, Michael M. Sent: Tuesday, October 11, 2005 4:04 PM To: [email protected] Subject: RE: [ActiveDir] WOL Does this help? http://www.depicus.com/wake-on-lan/wake-on-lan-gui.aspx
Mike Thommes
-----Original
Message-----
Is there any utility built into any version of Windows that can be used to send a WOL magic packet? If not, can someone direct me to a small utility that would enable waking a machine by pinging it?
Thanks,
Dan DeStefano
|
Option Explicit On Error Resume Next Dim oFso,WShell,strPath,strInfileName,strInfile,strOutfileName,strOutfile,strTempfileName Dim strTempfile,return,oInfile,oOutfile,oTempfile,arrIn,arrOut,intInCount,intOutCount Dim i,j,testIn,testOut,sPrev
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set oFso = createobject("scripting.filesystemobject")
Set WShell = CreateObject("WScript.Shell")
strPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) -
(Len(WScript.ScriptName) + 1)))
strInfileName = "MAClist.csv"
strInfile = oFso.BuildPath(strPath, strInfileName)
strOutfileName = "MAC.csv"
strOutfile = oFso.BuildPath(strPath, strOutfileName)
strTempfileName = "MACtemp.csv"
strTempfile = oFso.BuildPath(strPath, strTempfileName)
If oFso.FileExists(strInfile) Then
return = SortFile(strInfile)
If return <> 0 Then
WScript.Quit
End If
If oFso.FileExists(strOutfile) Then
return = SortFile(strOutfile)
If return <> 0 Then
WScript.Quit
End If
set oInfile = oFso.OpenTextFile(strInfile,ForReading)
set oOutfile = oFso.OpenTextFile(strOutfile,ForReading)
If Not oFso.FileExists(strTempfile) Then
set oTempfile = oFso.CreateTextFile(strTempfile,true)
oTempfile.Close
set oTempfile = Nothing
End If
set oTempfile = oFso.OpenTextFile(strTempfile,ForWriting)
arrIn = Split(oInfile.readall, vbcrlf)
arrOut = Split(oOutfile.readall, vbcrlf)
intInCount = UBound(arrIn)
intOutCount = UBound(arrOut)
i = 0
j = 0
sPrev = ""
Do While i < intInCount AND j < intOutCount
testIn = Split(arrIn(i),",",1)
testOut = Split(arrOut(j),",",1)
If testIn(0) = sPrev Then
i = i+1
ElseIf testOut(0) = sPrev Then
j = j+1
ElseIf testIn(0) = testOut(0) Then
oTempfile.WriteLine arrIn(i)
sPrev = testIn(0)
i=i+1
j=j+1
ElseIf testIn(0) < testOut(0) Then
oTempfile.WriteLine arrIn(i)
sPrev = testIn(0)
i=i+1
ElseIf testIn(0) > testOut(0) Then
oTempfile.WriteLine arrOut(j)
sPrev = testOut(0)
j=j+1
End If
Loop
oInfile.close
oOutfile.close
oTempfile.close
oFso.CopyFile strTempfile, strOutfile, True
Else
oFso.CopyFile strInfile, strOutfile, False
End If
WShell.LogEvent 4,WScript.ScriptName & " Finished parsing MAC addresses.
Results are in " & strOutfileName
Else
'Log error in event log
WShell.LogEvent 1,WScript.ScriptName & " Couldn't find an input file."
End If
If oFso.FileExists(strTempfile) Then
oFso.DeleteFile(strTempfile)
End If
Set ofso = Nothing
'>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Function SortFile(strSortName)
Dim oFile,DataList,arrLine,sortedFile
Const adVarChar = 200
Const MaxCharacters = 255
Set oFile = oFso.OpenTextFile(strSortName,ForReading)
Set DataList = CreateObject("ADOR.Recordset")
DataList.Fields.Append "Name", adVarChar, MaxCharacters
DataList.Fields.Append "MAC", adVarChar, MaxCharacters
DataList.Fields.Append "IP", adVarChar, MaxCharacters
DataList.Fields.Append "Mask", adVarChar, MaxCharacters
DataList.Open
Do Until oFile.AtEndOfStream
arrLine = Split(oFile.ReadLine,",")
DataList.AddNew
DataList("Name") = arrLine(0)
DataList("MAC") = arrLine(1)
DataList("IP") = arrLine(2)
DataList("Mask") = arrLine(3)
DataList.Update
Loop
oFile.Close
DataList.Sort = "Name"
DataList.MoveFirst
Set sortedFile = oFso.OpenTextFile(strSortName,ForWriting)
Do Until DataList.EOF
sortedFile.WriteLine DataList.Fields.Item("Name") & "," &
DataList.Fields.Item("MAC") & "," & DataList.Fields.Item("IP") & "," &
DataList.Fields.Item("Mask")
DataList.MoveNext
Loop
sortedFile.Close
Set oFile = Nothing
Set DataList = Nothing
Set sortedFile = Nothing
SortFile = 0
End Function 'SortFileOn Error Resume Next
Dim
strPath,strOutfileName,strOutfile,strIp,strMask,strPortNum,strPrompt,strCompToWake,strLine,i
Dim oFso,WShell,WakeOnLan,oMACs,strMaster,strSource
Const ForReading = 1, ForWriting = 2, ForAppending = 8
set oFso = createobject("scripting.filesystemobject")
Set WShell = CreateObject("WScript.Shell")
strSender = "XXXXXX" 'Machine with wolcom.dll registered
strMaster = "\\XXXXX\c$\scripts\wol" 'Location of master MAC list
strPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) -
(Len(WScript.ScriptName) + 1)))
strOutfileName = "MAC.csv"
strOutfile = oFso.BuildPath(strPath, strOutfileName)
strPortNum = "7"
'Copy the MAC file from the machine that updates it to current dir
strSource = oFso.BuildPath(strMaster, strOutfileName)
oFso.CopyFile strSource, strOutfile, True
If oFso.FileExists(strOutfile) Then
strPrompt = "Enter the computer name to wake," & vbCrlf & "or " & Chr(34) &
"all"_
& Chr(34) & " to wake all available." & vbCrlf & "Leave blank to
exit."
strCompToWake = InputBox(strPrompt,"Wakeup")
If strCompToWake <> "" Then
Set oMACs = oFso.OpenTextFile(strOutfile,ForReading)
strCompToWake = UCase(strCompToWake)
arrOut = Split(oMACs.readall, vbcrlf)
intOutCount = UBound(arrOut)
i = 0
If strCompToWake = "ALL" Then
Do While i < intOutCount
arrComp = Split(arrOut(i),",")
sMac = arrComp(1)
sMac = Replace(sMac,":","")
sIp = arrComp(2)
sMask = arrComp(3)
If sIp = "" Then
sIp = "255.255.0.0" 'Set broadcast range & mask
sMask = sIp
End If
result = SendMagicPkt(sMac,sIp,sMask,strPortNum)
'wscript.echo sMac & " " & sIp & " " & sMask & " " & result
'^^^^^^^^^^^^Debug
i = i+1
arrComp = ""
sMac = ""
sIp = ""
sMask = ""
Loop
WShell.Popup "Magic pkt sent to " & strCompToWake & ".",6,"Wakeup"
Else
wSuccess = 0
Do While i < intOutCount
arrComp = Split(arrOut(i),",")
If arrComp(0) = strCompToWake Then
sMac = arrComp(1)
sMac = Replace(sMac,":","")
sIp = arrComp(2)
sMask = arrComp(3)
result = SendMagicPkt(sMac,sIp,sMask,strPortNum)
'wscript.echo sMac & " " & sIp & " " & sMask & " " & result
'^^^^^^^^^^^^Debug
wSuccess = 1
Exit Do
End If
i = i+1
arrComp = ""
Loop
If wSuccess = 1 Then
WShell.Popup "Magic pkt sent to " & strCompToWake &
".",6,"Wakeup"
Else
WShell.Popup "No MAC found for " & strCompToWake & " -
quiting.",6,"Wakeup"
End If
End If
Else
WShell.Popup "No computername specified or cancelled -
quiting.",6,"Wakeup"
End If
Else
WShell.Popup "Cannot find MAC.csv - quiting.",6,"Wakeup"
End If
Function SendMagicPkt(ByVal strMac,ByVal strIp,ByVal strMask,strPort)
Err.Clear
set WakeOnLan = createobject("WolCom.Wol",strSender)
WakeOnLan.TheMacAddress(strMac)
WakeOnLan.TheIpNumber(strIp)
WakeOnLan.TheSubnetMask(strMask)
WakeOnLan.ThePortNumber(strPort)
WakeOnLan.WakeMeUp
SendMagicPkt = Err.Number & " " & Err.Description
End Function'This script gathers the MAC, ip, and subnet mask from active computers in the
specified domain,
'writes them to a comma-delimited file, and calls a parsing script to sort the
results into
'another file, that is used by a wake-on-lan script.
'This can take a looooong time on large domains...
Option Explicit
On Error Resume Next
Dim
objFSO,objFile,objConnection,objCommand,objRecordSet,objWMIService,NetAdapterSet,WShell,objFail
Dim
strName,strPath,strFileName,strFullName,strAdapter,intButton,strMessage,strParse,strEngine,strScriptName
Dim
mSuccess,iDebug,strFailName,strFullFail,NetAdapter,strTest,intIndex,strMAC,IPConfigSet,IPConfig
Dim strIp,strMask,strAddress,strSubnet
Const ADS_SCOPE_SUBTREE = 2
Const ForAppending = 8
Const ForWriting = 2
strScriptName = WScript.ScriptName
Set WShell = CreateObject("WScript.Shell")
'Create the file to hold the computer names
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) -
(Len(WScript.ScriptName) + 1)))
strFileName = "MAClist.csv"
strFullName = objFSO.BuildPath(strPath, strFileName)
strFailName = "Failures.csv"
strFullFail = objFSO.BuildPath(strPath, strFailName)
'If the files don't exist, create them
If Not objFSO.FileExists(strFullName) Then
Set objFile = objFSO.CreateTextFile(strFullName)
objFile.Close
End If
If Not objFSO.FileExists(strFullFail) Then
Set objFail = objFSO.CreateTextFile(strFullFail)
objFail.Close
End If
'get the computer names in the domain
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name FROM 'LDAP://DC=YOURDOMAIN,DC=com' WHERE
objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Set objFile = objFSO.OpenTextFile(strFullName, ForWriting)
Set objFail = objFSO.OpenTextFile(strFullFail, ForAppending)
'write the time of these failures
objFail.WriteLine Now
iDebug = 0
If Not ChkEngine="wscript.exe" Then
iDebug = 1
End If
'loop thru the machines and write them to the file
Err.Clear
Do Until objRecordSet.EOF
strName = objRecordSet.Fields("Name").Value
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}\\"
& strName & "\root\cimv2")
If Err.Number = 0 Then 'it's alive
Set NetAdapterSet = objWMIService.ExecQuery("select
Index,Name,PNPDeviceID,MACAddress from Win32_NetworkAdapter WHERE AdapterType =
'Ethernet 802.3'")
for each NetAdapter in NetAdapterSet
mSuccess = 0 'set flag to change after I've gathered all info from
this one - so I don't test any more adapters on this machine
strTest = NetAdapter.PNPDeviceID
intIndex = NetAdapter.Index
If InStr(1,strTest,"PCI\VEN",1) > 0 Then 'works with PCI network
cards - easiest filter
strMAC = NetAdapter.MACAddress
If Not strMAC = "" Then
Set IPConfigSet = objWMIService.ExecQuery("Select
IPAddress, IPSubnet from Win32_NetworkAdapterConfiguration Where Index=" &
intIndex)'IPEnabled=TRUE AND
For Each IPConfig in IPConfigSet
If Not IsNull(IPConfig.IPAddress) Then
For Each strAddress in IPConfig.IPAddress
strIp = strAddress
Next
For Each strSubnet in IPConfig.IPSubnet
strMask = strSubnet
Next
mSuccess = 1
End If
Next
objFile.WriteLine strName & "," & strMAC & "," & strIp &
"," & strMask
If iDebug = 1 Then
WScript.Echo strName & " - " & strMAC & " - " & strIp &
" - " & strMask '^^^^^^^^^^^^^^^^^Debug
End If
Set IPConfigSet = Nothing
End If
End If
strMAC = ""
strIp = ""
strMask = ""
If mSuccess = 1 Then Exit For
next
End If
Err.Clear
If mSuccess = 0 Then
objFail.WriteLine strName
'wscript.echo strName
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Debug
End If
Set objWMIService = Nothing
Set NetAdapterSet = Nothing
strName = ""
objRecordSet.MoveNext
mSuccess = 0
Loop
objFail.WriteLine
objFail.Close
objFile.Close
objConnection.Close
strMessage = "Done gathering MACs." & vbCrlf & "Press Cancel to prevent
parsing."
strParse = objFSO.BuildPath(strPath, "parseiplist.vbs")
strEngine = WScript.FullName
WShell.LogEvent 4,strScriptName & " Completed gathering MAC addresses."
intButton = WShell.popup(strMessage,4,"Get MAC",1 & 68)
If intButton = 2 Then
WScript.Quit
Else
WShell.Run(strEngine & " " & strParse)
End If
Function ChkEngine()
ON ERROR RESUME NEXT
strEngine=Wscript.FullName
if Err.Number <>0 then
wscript.echo "Error!"
wscript.echo "Error (" & Err.Number & ") Description: " &
Err.Description
wscript.quit
end if
PosX=InStrRev(strEngine,"\",-1,vbTextCompare)
ChkEngine=LCase(Mid(strEngine,PosX+1))
End Function
On Error Resume Next
Dim
objFSO,objFile,objConnection,objCommand,objRecordSet,objWMIService,NetAdapterSet,WShell,objFail
Dim
strName,strPath,strFileName,strFullName,strAdapter,intButton,strMessage,strParse,strEngine,strScriptName
Dim mSuccess
Const ADS_SCOPE_SUBTREE = 2
Const ForAppending = 8
Const ForWriting = 2
strScriptName = WScript.ScriptName
Set WShell = CreateObject("WScript.Shell")
'Create the file to hold the computer names
Set objFSO = CreateObject("Scripting.FileSystemObject")
strPath = Left(WScript.ScriptFullName,(Len(WScript.ScriptFullName) -
(Len(WScript.ScriptName) + 1)))
strFileName = "MAClist.csv"
strFullName = objFSO.BuildPath(strPath, strFileName)
strFailName = "Failures.csv"
strFullFail = objFSO.BuildPath(strPath, strFailName)
'If the files don't exist, create them
If Not objFile.FileExists(strFullName) Then
Set objFile = objFSO.CreateTextFile(strFullName)
objFile.Close
End If
If Not objFile.FileExists(strFullFail) Then
Set objFail = objFSO.CreateTextFile(strFullFail)
objFail.Close
End If
'get the computer names in the domain
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = "SELECT Name FROM 'LDAP://DC=YOURDOMAIN,DC=com' WHERE
objectClass='computer'"
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Timeout") = 30
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
Set objFile = objFSO.OpenTextFile(strFullName, ForWriting)
Set objFail = objFSO.OpenTextFile(strFullFail, ForAppending)
'write the time of these failures
objFail.WriteLine Now
'loop thru the machines and write them to the file
'On Error Resume Next
Do Until objRecordSet.EOF
strName = objRecordSet.Fields("Name").Value
Set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}")._
ExecQuery("select * from Win32_PingStatus where address = '" & strName
& "'")
For Each objStatus in objPing
If objStatus.StatusCode = 0 Then
Set objWMIService = GetObject("winmgmts:\\" & strName &
"\root\cimv2")
If Err.Number = 0 Then
Set NetAdapterSet = objWMIService.ExecQuery("select
Name,PNPDeviceID,MACAddress from Win32_NetworkAdapter WHERE AdapterType =
'Ethernet 802.3'")
for each NetAdapter in NetAdapterSet
strTest = NetAdapter.PNPDeviceID
If InStr(1,strTest,"PCI\VEN",1) > 0 Then
'strAdapter = NetAdapter.Name
'^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Debug
strMAC = NetAdapter.MACAddress
'wscript.echo strName & " - " & strMAC & " - " &
strAdapter 'Debug
If Not strMAC = "" Then
objFile.WriteLine strName & "," & strMAC
mSuccess = 1
End If
End If
strMAC = ""
If mSuccess = 1 Then Exit For
next
End If
Err.Clear
End If
Next
If mSuccess = 0 Then
objFail.WriteLine strName
End If
strName = ""
objRecordSet.MoveNext
mSuccess = 0
Loop
objFail.WriteLine
objFail.Close
objFile.Close
strMessage = "Done gathering MACs." & vbCrlf & "Press Cancel to prevent
parsing."
strParse = objFSO.BuildPath(strPath, "parseiplist.vbs")
strEngine = WScript.FullName
WShell.LogEvent 4,strScriptName & " Completed gathering MAC addresses."
intButton = WShell.popup(strMessage,4,"Get MAC",1 & 68)
If intButton = 2 Then
WScript.Quit
Else
WShell.Run(strEngine & " " & strParse)
End If
