Overclock.net banner

Downloaded Powershell script not working

434 Views 1 Reply 2 Participants Last post by  SectorNine50
2
I didn't actually write it, but I cannot for the life of me get it to run. I copied and pasted it directly into PS and started reading through it to understand what exactly it's doing, but it's broken right out of the gate.
frown.gif


It's a Wake-On-LAN script that would prove immensely useful if I could get it working. Here's where I got it from (it's the second script on the page)

And here's the full text. I get all kinds of red text when I try to run it about "Unexpected Tokens" everywhere, leading me to think that there's some parenthesis or brace that's missing.... Thoughts?

Code:

Code:
#######################################################
2.##
3.## WakeUp-Machines.ps1, v1.1, 2012
4.##
5.## Created by Matthijs ten Seldam, Microsoft
6.##
7.#######################################################
8. 
9.<#
10..SYNOPSIS
11.Starts a list of physical machines by using Wake On LAN.
12. 
13..DESCRIPTION
14.WakeUp-Machines starts a list of servers using Wake On LAN magic packets. It then sends echo requests to verify that the machine has TCP/IP connectivity. It waits for a specified amount of echo replies before starting the next machine in the list.
15. 
16..PARAMETER Machines
17.The name of the file containing the machines to wake.
18. 
19..PARAMETER TimeOut
20.The number of seconds to wait for an echo reply before continuing with the next machine.
21. 
22..PARAMETER Repeat
23.The number of echo requests to send before continuing with the next machine.
24. 
25..EXAMPLE
26.WakeUp-Machines machines.csv
27. 
28..EXAMPLE
29.WakeUp-Machines c:\tools\machines.csv
30. 
31..INPUTS
32.None
33. 
34..OUTPUTS
35.None
36. 
37..NOTES
38.Make sure the MAC addresses supplied don't contain "-" or ".".
39. 
40.The CSV file with machines must be outlined using Name, MAC Address and IP Address with the first line being Name,MacAddress,IpAddress.
41.See below for an example of a properly formatted CSV file.
42. 
43.Name,MacAddress,IpAddress
44.Host1,A0DEF169BE02,192.168.0.11
45.Host3,AC1708486CA2,192.168.0.12
46.Host2,FDDEF15D5401,192.168.0.13
47. 
48..LINK
49.http://blogs.technet.com/matthts
50.#>
51. 
52. 
53.param(
54.    [Parameter(Mandatory=$true, HelpMessage="Path to the CSV file containing the machines to wake.")]
55.    [string] $Machines,
56.    [Parameter(Mandatory=$false, HelpMessage="Number of unsuccesful echo requests before continuing.")]
57.    [int] $TimeOut=30,
58.    [Parameter(Mandatory=$false, HelpMessage="Number of successful echo requests before continuing.")]
59.    [int] $Repeat=10,
60.    [Parameter(Mandatory=$false, HelpMessage="Number of magic packets to send to the broadcast address.")]
61.    [int] $Packets=2
62.    )
63. 
64. 
65.Set-StrictMode -Version Latest
66. 
67.clear;Write-Host
68. 
69.## Read CSV file with machine names
70.try
71.{
72.    $File=Import-Csv $Machines
73.}
74.Catch
75.{
76.    Write-Host "$Machines file not found!";Write-Host
77.    exit
78.}
79. 
80.function Send-Packet([string]$MacAddress, [int]$Packets)
81.{
82.    <#
83.    .SYNOPSIS
84.    Sends a number of magic packets using UDP broadcast.
85. 
86.    .DESCRIPTION
87.    Send-Packet sends a specified number of magic packets to a MAC address in order to wake up the machine.  
88. 
89.    .PARAMETER MacAddress
90.    The MAC address of the machine to wake up.
91. 
92.    .PARAMETER
93.    The number of packets to send.
94.    #>
95. 
96.    try 
97.    {
98.        $Broadcast = ([System.Net.IPAddress]::Broadcast)
99. 
100.        ## Create UDP client instance
101.        $UdpClient = New-Object Net.Sockets.UdpClient
102. 
103.        ## Create IP endpoints for each port
104.        $IPEndPoint1 = New-Object Net.IPEndPoint $Broadcast, 0
105.        $IPEndPoint2 = New-Object Net.IPEndPoint $Broadcast, 7
106.        $IPEndPoint3 = New-Object Net.IPEndPoint $Broadcast, 9
107. 
108.        ## Construct physical address instance for the MAC address of the machine (string to byte array)
109.        $MAC = [Net.NetworkInformation.PhysicalAddress]::Parse($MacAddress)
110. 
111.        ## Construct the Magic Packet frame
112.        $Frame = [byte[]]@(255,255,255, 255,255,255);
113.        $Frame += ($MAC.GetAddressBytes()*16)
114. 
115.        ## Broadcast UDP packets to the IP endpoints of the machine
116.        for($i = 0; $i -lt $Packets; $i++) {
117.            $UdpClient.Send($Frame, $Frame.Length, $IPEndPoint1) | Out-Null
118.            $UdpClient.Send($Frame, $Frame.Length, $IPEndPoint2) | Out-Null
119.            $UdpClient.Send($Frame, $Frame.Length, $IPEndPoint3) | Out-Null
120.            sleep 1;
121.        }
122.    }
123.    catch
124.    {
125.        $Error | Write-Error;
126.    }
127.}
128. 
129.$i=1
130.foreach($Machine in $File)
131.{
132.    $Name=$Machine.Name
133.    $MacAddress=$Machine.MacAddress
134.    $IPAddress=$Machine.IpAddress
135. 
136.    ## Send magic packet to wake machine
137.    Write-Progress -ID 1 -Activity "Waking up machine $Name" -PercentComplete ($i*100/$file.Count)
138.    Send-Packet $MacAddress $Packets
139. 
140.    $j=1
141.    ## Go into loop until machine replies to echo
142.    $Ping = New-Object System.Net.NetworkInformation.Ping
143.    do
144.    {
145.        $Echo = $Ping.Send($IPAddress)
146.        Write-Progress -ID 2 -ParentID 1 -Activity "Waiting for $Name to respond to echo" -PercentComplete ($j*100/$TimeOut)
147.        sleep 1
148.        
149.        if ($j -eq $TimeOut)
150.        {
151.            Write-Host "Time out expired, aborting.";Write-Host
152.            exit
153.        }
154.        $j++
155.    }
156.    while ($Echo.Status.ToString() -ne "Success" )
157. 
158.    ## Machine is alive, keep sending for $Replies amount
159.    for ($k = 1; $k -le $Repeat; $k++) 
160.    { 
161.       Write-Progress -ID 2 -ParentID 1 -Activity "Waiting for $Name to respond to echo" -PercentComplete (100) 
162.       Write-Progress -Id 3 -ParentId 2 -Activity "Receiving echo reply"  -PercentComplete ($k*100/$Repeat)
163.       sleep 1
164.    }
165.    $i++
166.    Write-Progress -Id 3 -Completed $true
167.    $Ping=$null
168.}

Nevermind, I just wrote my own that is much shorter and easier to understand.
smile.gif
See less See more
1 - 2 of 2 Posts
Did you copy the line numbers into the script? Because those will definitely cause problems...
1 - 2 of 2 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top