[英]Running a WOL PowerShell script through a batch file
我正在嘗試通過批處理文件運行PowerShell腳本。 我想這樣做,是因為我想得到我的回聲以能夠打開計算機。 (可能有更簡單的方法從回顯中發送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中也是如此。
假設外部腳本位於C:\\ Downloads,請在批處理文件中嘗試以下操作:
Powershell -File "C:\Downloads\Send-WOL.ps1" -mac ------- -ip --------
在所有的幫助下,我終於弄清楚了。
@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)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.