1

I am trying to run a PowerShell script through a batch file. I want to do this because I want to have my echo to be able to turn on my computer. (there are probably easier ways to send a WOL request from a echo, but I want to do it through PowerShell for learning purposes)

For the WOL command I have this (I do have the correct values for the dashes, I just don't want to show them):

Send-WOL -mac ------- -ip --------

Then my .bat file contains this:

@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%script.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
pause

Now when I run the .bat file I would get this error:

Send-WOL : The term 'Send-WOL' is not recognized as the name of a cmdlet,
function, script file or operable program. Check the spelling of the name,
or if a path was included, verify that the path is correct and try again.
At C:\Users\hao\Desktop\WOL main\Script.ps1:1 char:1 
+ Send-WOL -mac █████████████████ -ip █████████████
+ ~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (send-WOL:String) [], CommandNotFoundExeption
    + FullyQualifiedErrorId : CommandNotFoundExeption

Press any key to continue . . .

even though when I put the exact same command within the script, manually into PowerShell it works perfectly.

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
ArandomDev
  • 125
  • 2
  • 10
  • PowerShell can't find `Send-WOL.ps1`, presumably because it's location is not in the PATH, or because you put the code from that script in your profile (which you told the batch file not to load). – Ansgar Wiechers Oct 09 '17 at 15:44
  • Why use `-Command` instead of `-File`? `@Powershell -NoProfile -ExecutionPolicy Bypass -File %~dp0script.ps1`, _and use quotes around your path if it contains spaces_. If you want to use `-Command` then there's no need to use the `.ps1` file at all! `@Powershell -NoProfile -ExecutionPolicy Bypass -Command "Send-WOL -mac ------- -ip --------"`. – Compo Oct 09 '17 at 15:46
  • Try adding `Set-Location $PSScriptRoot` to your script.ps1 file – TToni Oct 09 '17 at 15:46

2 Answers2

1

With all the help I finally figure it out.

@ECHO OFF
powershell -executionpolicy bypass -file "---------------"

this is in the .bat file (replace the dashes with the path of the power shell script below)

function Send-WOL
{
<# 
  .SYNOPSIS  
    Send a WOL packet to a broadcast address
  .PARAMETER mac
   The MAC address of the device that need to wake up
  .PARAMETER ip
   The IP address where the WOL packet will be sent to
  .EXAMPLE 
   Send-WOL -mac 00:11:32:21:2D:11 -ip 192.168.8.255 
#>

[CmdletBinding()]
param(
 [Parameter(Mandatory=$True,Position=1)]
 [string]$mac,
[string]$ip="255.255.255.255", 
[int]$port=9
)
 $broadcast = [Net.IPAddress]::Parse($ip)

$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
 $target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
$packet = (,[byte]255 * 6) + ($target * 16)

$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($broadcast,$port)
[void]$UDPclient.Send($packet, 102) 

}

send-wol -mac ------ -ip --------

this is contained within the power shell script (replace the dashes with the mac and IP of the target computer)

ArandomDev
  • 125
  • 2
  • 10
0

Assuming the external script is located at C:\Downloads, Try this in your batch file:

Powershell -File "C:\Downloads\Send-WOL.ps1" -mac ------- -ip --------
Sid
  • 2,586
  • 1
  • 11
  • 22