可使用PowerShell達到遠端開機的功能

$strMAC="00-E0-18-C0-AC-6B"
$mac = new-object byte[] 6
$i=0;
foreach($m in $strMAC.Split("-"))
{
    $mac[$i] = [Byte]("0x"+$m)
    $i++;
}
$UdpClient = new-object System.net.sockets.UdpClient;
$UdpClient.Connect([System.Net.IPAddress]::Broadcast, 40000);
$packet = new-object byte[] 102
for($i = 0; $i -lt 6; $i++){$packet[$i] = [Byte]"0xFF" }
for ($i = 1; $i -le 16; $i++)
{
     for ($j = 0; $j -lt 6; $j++)
     {
         $packet[$i*6+$j] += $mac[$j]
     }
}
$UdpClient.Send($packet, $packet.Length);

 

假設我們今天要開啟許多台電腦
而這些電腦的MAC address可來自某檔案(我們當然可以利用PowerShell先行做其他的處理)
而非一定要將這些MAC address寫死在程式中

我們將上述的Script包成一個filter
filter WOL
{
     $strMAC="00-E0-18-C0-AC-6B"
     #我們僅需將第一行寫死的MAC address改成$_
     
$strMAC=$_
     #其餘一模一樣
}


再假設我們有一個MAC address List檔案 MacAddressList.txt
裡面每列為欲喚醒的MAC address

因此我們可使用以下指令喚醒電腦
PS C:\> get-content D:\MacAddressList.txt | WOL

看! 多麼簡潔明白又有彈性^^

  • 本篇重點
    • 宣告固定長度的陣列
      • 如 new-object byte[] 6
    • 十六進位轉Byte
      • 如 [Byte]("0x"+"E0")
    • 使用.NET Framework類別
      • 如 new-object System.net.sockets.UdpClient
    • 使用.NET Framework靜態變數
      • [System.Net.IPAddress]::Broadcast
    • 使用filter
創作者介紹

PowerShell@HungYuWu

ayowu 發表在 痞客邦 PIXNET 留言(2) 人氣()


留言列表 (2)

發表留言
  • No name
  • wow power leveling
    wow power leveling
    wow power leveling
    cheap wow gold
    wow gold
    lotro gold
    wow gold
    World of Warcraft gold
    power leveling
    powerleveling
    Rolex
    rolex replica
    replica rolex
    Watches Rolex
    Rolex Watches
    Watch Rolex
    Rolex Watch
    rs gold
    Runescape Gold
    RuneScape Money
    power level
    powerleveling wow
    powerleveling wow
    powerleveling wow
    powerleveling wow
    powerleveling wow
    powerleveling wow
    powerleveling wow
    powerleveling wow
    power leveling wow
    power leveling wow
    power leveling wow
    power leveling wow
    power leveling wow
    power leveling wow
    power leveling wow
    power leveling wow -19145325974259
  • No name
  • wow power leveling
    wow gold
    World of Warcraft gold
    wow gold
    wow powerleveling
    wow powerleveling
    Rolex
    rolex replica
    replica rolex
    Runescape Gold
    RuneScape Money
    powerleveling
    powerleveling
    powerleveling
    powerleveling
    powerleveling
    power leveling
    power leveling
    power leveling
    powerleveling
    power leveling
    Watches Rolex
    Rolex Watches
    Watch Rolex
    Rolex Watch
    rs gold
    wow
    wowgold
    gold wow
    cheap wow gold
    buy wow gold
    lotro gold
    wow gold -399293314326069