Monday, April 4, 2011

VB Script to Execute Wake on LAN on a different subnet

Wake on Lan packets won't travel over different subnets.

Below is a script which remotely executes the command on a host computer on the same subnet to send the WOL packet.

You will need
  • WOL capable computer you wish to wake.
  • A host machine on the same subnet as the comptuer you wish to wake.
  • WOL software http://sourceforge.net/projects/wake-on-lan/ extract the software to C:\temp\wol
  • MAC address of computer you wish to wake (you can get from DHCP) .
The script copies the WOL files from C:\temp\WOL to the host then remotely executes the command.

'Copy Wake on LAN files to remote computer then execute WOL
'Allows you to select remote computer on a different subnet as a host and wake another computerOption Explicit
Dim strHostComputer, strMAC, strIPComputer, objFSO
Dim objWMIService, objProcess, errReturn
Dim strComputerOn, Count, strCommand
Dim intProcessID, strComputer, colPingedComputers
Dim objComputer
strHostComputer =Inputbox("Wake Computer on another Subnet" & chr(10) & chr(13) & chr(10) & chr(13) & "Enter HOST PC Number on Remote Subnet","Input Required")
If strHostComputer = ""Then
 WScript.Echo"Host blank exiting script!"
 WScript.Quit
Endif
strMAC =Inputbox("Enter MAC of PC you want to WAKE" & chr(10) & chr(13) & chr(10) & chr(13) & "MAC can be obtained from DHCP Scope, Address Leases","Input Required")
If strMAC = ""Then
 WScript.Echo"MAC blank exiting script!"
 WScript.Quit
Endif
strIPComputer =Inputbox("Enter IP Address of Computer you want to WAKE","Input Required")
If strIPComputer = ""Then
 WScript.Echo"IP Blank will send request but can't check progress"
Endif

Const OverWriteFiles = TRUE
Set objFSO = CreateObject("Scripting.FileSystemObject")
If objFSO.FileExists("C:\temp\WOL\bin\wol.exe") Then
    objFSO.CopyFolder"C:\temp\WOL" , "\\" & strHostComputer & "\C$\temp\WOL" , OverWriteFiles
Else
    Wscript.Echo"Wake on Lan File C:\temp\WOL\bin\wol.exe not found. Download from http://sourceforge.net/projects/wake-on-lan"
    WScript.Quit
EndIf


strCommand = "C:\temp\WOL\bin\wol.exe " & strMAC

Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strHostComputer & "\root\cimv2")
Set objProcess = objWMIService.Get("Win32_Process")

errReturn = objProcess.Create(strCommand, null, null, intProcessID)

If errReturn = 0Then
'WScript.Echo strHostComputer &" " & strCommand & " executed with a process ID: " & intProcessID
Else
WScript.Echo strHostComputer & " " & strCommand & " not started due to error: " & errReturn
EndIf
If strIPComputer = ""Then
 WScript.Quit
Endif
' Verify Computer Availability
Wscript.Echo"Waking Remote Computer - usually takes about 1 minute"
Count = 0
DoWhile strComputerOn <> 1
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colPingedComputers = objWMIService.ExecQuery _
    ("Select * from Win32_PingStatus Where Address = '" & strIPComputer & "'")
ForEach objComputerin colPingedComputers
    If objComputer.StatusCode = 0Then
        Wscript.Echo"Remote Computer " & strIPComputer & " Powered On!!!"
        strComputerOn=1
    Else
        WScript.sleep20000
        Count = Count + 1
        If Count = 7Then
        Wscript.Echo"Can't Connect to Remote Machine WOL has failed :(" & chr(10) & chr(13) & chr(10) & chr(13) & "Or Remote Machine has new IP"
        WScript.Quit
        Endif
   EndIf
Next
Loop

4 comments:

  1. I would love to be able to get this to work

    When prompted for HOST PC I put in the host machine name

    MAC
    I put in the mac address of the machine I want to wake up

    IP
    I put in the IP of the machine I want to wake up

    then I get an error 0x80041003
    Script C:\TEMP\WOL.vbs
    Line: 29
    Char: 1

    at first it was
    line 25
    Char 1

    Any ideas?

    Thank you!

    ReplyDelete
    Replies
    1. Have you downloaded the WOL software http://sourceforge.net/projects/wake-on-lan and extracted it to C:\temp\wol? c:\temp\WOL\bin\wol.exe is the specific file required.

      Also check that you are local admin on the Host PC.

      Delete
  2. Script: C:\temp\WOL\run.vbs
    Line: 25
    Char: 5
    Error: Path not found
    Code: 800A004C
    Source: Microsoft VBScript runtime error

    Hi, I'm constantly getting this error message. The path is correct, WOL files are there ... no idea why it keeps saying path not found.

    ReplyDelete
  3. Ok I have modified the script.
    objFSO.CopyFolder "WOL" to objFSO.CopyFolder "C:\temp\WOL"
    It was assuming that you were running the script from c:\temp.
    Hopefully it works OK for you now?
    Cheers,
    Steve

    ReplyDelete