我正在尝试通过批处理文件运行PowerShell脚本。我想这样做是因为我想让我的回声能够打开我的电脑。 (可能有更简单的方法从echo发送WOL请求,但我希望通过PowerShell进行学习以实现学习目的)
对于WOL命令,我有这个(我确实有破折号的正确值,我只是不想显示它们):
Send-WOL -mac ------- -ip --------
然后我的.bat文件包含:
@ECHO OFF
SET ThisScriptsDirectory=%~dp0
SET PowerShellScriptPath=%ThisScriptsDirectory%script.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& '%PowerShellScriptPath%'";
pause
现在,当我运行.bat文件时,我会收到此错误:
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 . . .
即使我在脚本中放入完全相同的命令,手动进入PowerShell也能完美运行。
答案 0 :(得分:0)
假设外部脚本位于C:\ Downloads,请在批处理文件中尝试:
Powershell -File "C:\Downloads\Send-WOL.ps1" -mac ------- -ip --------
答案 1 :(得分:0)
在最后的帮助下,我终于明白了。
@ECHO OFF
powershell -executionpolicy bypass -file "---------------"
这是在.bat文件中(用下面的power shell脚本的路径替换破折号)
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 --------
这包含在power shell脚本中(用目标计算机的mac和IP替换破折号)