2017-10-09 33 views
0

バッチファイルを使用してPowerShellスクリプトを実行しようとしています。私は私のエコーを私のコンピュータをつけることができるようにしたいので私はこれをしたいと思う。 (エコーからWOLリクエストを送信する方が簡単ですが、学習目的でPowerShellを使用したい)ちょうど)、それらを表示したくない:私は.BATファイルを実行するとバッチファイルを使用してWOL PowerShellスクリプトを実行する

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

今、私はこのエラーを得るでしょう:

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

をそれから私の.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

PowerShellは 'Send-WOL.ps1'を見つけられません。おそらく、その場所がPATHにないか、そのスクリプトのコードをプロファイルに入れているからです。バッチファイルはロードされません)。 –

+0

'-File'の代わりに' -Command'を使う理由は? '@Powershell -NoProfile -ExecutionPolicy Bypass -File%〜dp0script.ps1'、そしてパスにスペースが含まれている場合はパスを引用符で囲んでください。 '-Command'を使いたいなら' .ps1'ファイルを使う必要は全くありません! '@Powershell -NoProfile -ExecutionPolicy Bypass -Command" Send-WOL -mac ------- -ip -------- "'。 – Compo

+0

'Set-Location $ PSScriptRoot'をあなたのscript.ps1ファイルに追加してみてください – TToni

答えて

0

外部スクリプトはCに位置していますと仮定:\ダウンロード、あなたのバッチファイルでこれを試してみてください:

Powershell -File "C:\Downloads\Send-WOL.ps1" -mac ------- -ip -------- 
0

私は最終的にそれを把握するすべての助けを借りて。

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

この.BATファイルである(以下、パワーシェルスクリプトのパスにダッシュを置き換える)

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 -------- 

これはパワーシェルスクリプト内に含まれる(MACとダッシュを置き換えとターゲットコンピュータのIPアドレス)

関連する問題