function send-wakeonlan{The reason I am making this a function is so that I can dot source this in a later script. It keeps the end result clean and simple. to dot source something just browse powershell to the right folder path and then ./script-name.ps1 Now to call the function I will only need to find the correct MAC Address to use and then enter it into the following command. send-wakeonlan -mac “00:11:22:33:AA:BB" There that was not so hard was it? Now for the next step. How to know when the server has fully started and is ready. For this I have adapted a script that was not exactly set to do what we are doing with it but it works great. Please understand I am a big believer in not rewriting what someone else has already done. I big thank you for those have gone before and worked out the bigger problems. This script I did not modify. Make sure to take a look at the original site I found it it on. Located here http://gallery.technet.microsoft.com/scriptcenter/PowerShell-queryService-94ecfac6
param(
[string]$mac)
if (!($mac -like "*:*:*:*:*") -or ($mac -like "*-*-*-*-*")){
write-error "mac address not in correct format"
break
}
$string=@($mac.split(":""-") | foreach {$_.insert(0,"0x")})
$target = [byte[]]($string[0], $string[1], $string[2], $string[3], $string[4], $string[5])
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect(([System.Net.IPAddress]::Broadcast),4000)
$packet = [byte[]](,0xFF * 102)
6..101 |% { $packet[$_] = $target[($_%6)]}
$UDPclient.Send($packet, $packet.Length) | out-null
}
# Author ITBABLE.BLOGGER.COM This is an edited and altered scrip from the link below.
# source http://andrewmorgan.ie/2011/08/22/simple-wake-on-lan-powershell-module/
# This Function Will Query A Dependancy Service and wait until the timer expires OR for the service to start.Now we have all the pieces we need. All we need to do now is create a new script dot source the two functions and we have something that looks like this.
function Query-Service { param($Service,$timer1,$Computer)
$success = ""
write-host "Waiting on $Service Service..."
# Create a for loop to INC a timer Every Second
for ($b=1; $b -lt $timer1; $b++) {
$servicestat = get-service $Service -ComputerName $Computer
$status = $servicestat.status
$b2 = $timer1 - $b
# Determine the Percent Complete for the seconds.
$percent = $b * (100 / $timer1)
# Display the progress on the Screen
Write-Progress -Activity "Waiting on $Service Service..." -PercentComplete $percent -CurrentOperation "$b2 Seconds Remaining" -Status "Current WMI Status: $status"
# Determine if the Process is Running. If not, reloop. If so exit loop.
if ($status -eq "Running") {
write-host "$Service Service Started Successfully: $status in $b Seconds"
[int]$b = $timer1
$success = "yes"
# Tells the Loop to Stop Incrementing as the Service is running
Write-Progress -Activity "Completed" -Status "Current $Service Status: $status in $b Seconds" -Completed
}
# Start-Sleep is available for the write-progress. Its value is in seconds.
start-sleep 1
}
# The script will now stop as the above loop has meet its time criteria and the success is not set to yes.time has expired.
if ($success -ne "yes") {
write-host "ERROR in Script: $Service Service Did Not Start In $timer1 Seconds. Status: $status"
# Stop the Script
BREAK
}
}
# The service must be the actual executible name, not the friendly name
# The Below Examples are Querying SQL Services for startup waiting 120 seconds for a timeout.
# Source http://gallery.technet.microsoft.com/scriptcenter/PowerShell-queryService-94ecfac6
</blockquote>
. "c:\Scripts\Samples\QueryService.ps1"
. "c:\Scripts\functions\Send-WakeOnLan.PS1" #Wake SERVER1 send-wakeonlan -mac 00:11:22:AA:BB:CC" Queryservice "Some Service" "300" "192.168.1.10" #Wake SERVER2 send-wakeonlan -mac 11:22:33:BB:CC:DD"
Queryservice "Another Service" "300" "192.168.1.11"
The cool thing is the script will hold and wait for as long as you tell it to. Here the wait is 300 seconds and you can change that based on how long the server can take to start. You can also add the names of multiple services to wait for on one server making sure all the needed ones have started. That is the quick and dirty part. Later it would be wise to enter error coding to send you an email alert if a service has failed. Or change it to move on any way gather the alerts and send them off in one email. That however, can get a little tricky and would be best to save for another post. When I have the time to take that on.
No comments:
Post a Comment