Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Downloaded Powershell script not working
New Posts  All Forums:Forum Nav:

Downloaded Powershell script not working

post #1 of 2
Thread Starter 
Warning: Spoiler! (Click to show)
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:

#######################################################
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
Heisenberg
(10 items)
 
Redemption
(18 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core 2 Extreme @ 2.8GHz (upgrade) Intel GMA 950 2 x 2 DDR2 667MHz (upgrade) 120GB Mushkin ECO2 
Optical DriveOSMonitorPower
Replaced with 42 W/hr aux battery Mint 17.3 1280x800 Dell 65w Dell Adapter 
CaseOther
Latitude D630 Chassis Intel 7260-802.11ac 2x2 
CPUMotherboardGraphicsRAM
Not telling AsRock something Pixel Pusher 2 x 8 GB AMD Radeon RAM @ 1600mhz (All the VM's) 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 128GB (8 second boot) Western Digital RE4 1TB Western Digital RE4 2TB Pioneer Blu-Ray Reader/Writer 
CoolingOSMonitorKeyboard
Cooler Master Hyper 212 Evo Windows 8.1 Pro x64 Dell U2713HM IBM Model M (CLICKY!!) 
PowerCaseMouseAudio
Antec HCG-900 NZXT Source 210 (it was cheap) Logitech G502 Schiit Modi 
AudioAudio
Sennheiser HD 598 M-Audio AV40's 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Microsoft BGA1168 Intel HD 4400 8 GB LPDDR3 Dual Channel @ 1600mhz 
Hard DriveOSMonitorKeyboard
SK Hynix 256GB SSD Windows 8.1 Professional 1920 x 1080p IPS Display Microsoft Touch Cover 2 
Power
48w PSU 
  hide details  
Reply
Heisenberg
(10 items)
 
Redemption
(18 items)
 
 
CPUGraphicsRAMHard Drive
Intel Core 2 Extreme @ 2.8GHz (upgrade) Intel GMA 950 2 x 2 DDR2 667MHz (upgrade) 120GB Mushkin ECO2 
Optical DriveOSMonitorPower
Replaced with 42 W/hr aux battery Mint 17.3 1280x800 Dell 65w Dell Adapter 
CaseOther
Latitude D630 Chassis Intel 7260-802.11ac 2x2 
CPUMotherboardGraphicsRAM
Not telling AsRock something Pixel Pusher 2 x 8 GB AMD Radeon RAM @ 1600mhz (All the VM's) 
Hard DriveHard DriveHard DriveOptical Drive
Crucial M4 128GB (8 second boot) Western Digital RE4 1TB Western Digital RE4 2TB Pioneer Blu-Ray Reader/Writer 
CoolingOSMonitorKeyboard
Cooler Master Hyper 212 Evo Windows 8.1 Pro x64 Dell U2713HM IBM Model M (CLICKY!!) 
PowerCaseMouseAudio
Antec HCG-900 NZXT Source 210 (it was cheap) Logitech G502 Schiit Modi 
AudioAudio
Sennheiser HD 598 M-Audio AV40's 
CPUMotherboardGraphicsRAM
Intel Core i5-4200U Microsoft BGA1168 Intel HD 4400 8 GB LPDDR3 Dual Channel @ 1600mhz 
Hard DriveOSMonitorKeyboard
SK Hynix 256GB SSD Windows 8.1 Professional 1920 x 1080p IPS Display Microsoft Touch Cover 2 
Power
48w PSU 
  hide details  
Reply
post #2 of 2
Did you copy the line numbers into the script? Because those will definitely cause problems...
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
Perpetual Upgrade
(17 items)
 
Server
(17 items)
 
Galago UltraPro
(9 items)
 
CPUMotherboardGraphicsRAM
i7-4770K MSI Z97M Gaming Zotac GTX 1080 AMP! Edition (2x4GB) Corsair DDR3-2000 
Hard DriveHard DriveCoolingCooling
128GB Crucial M4 (2x) 500GB RAID 0 Swiftech Apogee Black Ice GT Stealth 240 
OSKeyboardPowerCase
Windows 10 Pro 64bit Corsair K70 Vengence Seasonic X650 Aerocool DS Cube 
MouseAudio
Logitech G500 ASUS Xonar DX 
CPUMotherboardGraphicsRAM
Phenom II X4 965 MSI 870A-G54 nVidia 8400GS (2x2GB) Patriot DDR3-1600 
RAMHard DriveHard DriveCooling
(2x4GB) Patriot DDR3-1600 (3x) 320GB RAID 5 (1x) 1TB Backup Storage Coolermaster TX3 
OSPowerOther
Proxmox Hypervisor Antec TruePower 430W HP Smart Array P400 
CPUGraphicsRAMHard Drive
Intel i7-4750HQ Intel Iris Pro Graphics 5200  (2 x 4GB) DDR3-1600 90GB Intel mSATA SSD 
Hard DriveOSOSMonitor
500GB 5400RPM HDD Ubuntu Gnome 15.10 Windows 10 14" 1080p ColorPro IPS 
Case
Galago UltraPro 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Application Programming › Downloaded Powershell script not working