Skip to content

Instantly share code, notes, and snippets.

@heinz-otto
Last active January 24, 2018 08:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heinz-otto/7a3922590c11ecbd806b9b3c0607a63c to your computer and use it in GitHub Desktop.
Save heinz-otto/7a3922590c11ecbd806b9b3c0607a63c to your computer and use it in GitHub Desktop.
NAS automatisch mit PC starten
# Hilfsscripte mit dot sourcing eingebunden
# Sende WOL
# und warte bis IP erreichbar, Abruch nach x Durchläufen
Set-Location $PSScriptRoot
. .\SendWOLPacket.ps1 #-macstring "H340" -verbose
. .\BalloonTipp.ps1
$x=15 # Abruch nach x Durchläufen
Send-WOL -mac '00:26:2D:00:10:91' -ip 192.168.178.255
$i=0
while (!(Test-Connection 192.168.178.44 -Count 1 -quiet)){
sleep(1)
$i++
write-verbose $i
If ($i -gt $x) {
Show–BalloonTip –Text 'NAS bitte per Hand starten, eventuell war Stromausfall' –Title 'Achtung' –Icon Warning –Timeout 15000
exit
}
}
Show–BalloonTip –Text 'NAS ist jetzt erreichbar' –Title 'Alles in Ordnung'
<#
.SYNOPSIS
Send a WOL packet to a broadcast address
.DESCRIPTION
The Script could be called with the Parameter -MacString then could also be used a Table with Device Names
If the Script is called without any Parameter, only the Function is available
.PARAMETER -MacString
The MAC address of the device that need to wake up
.EXAMPLE
SendWOLPacket -MacString 'H340'
.EXAMPLE
. .\SendWOLPacket.ps1
#>
Param ($MacString)
$Table=@{
H340 ='00:26:2D:00:10:91';
lsk2012 ='78:24:AF:43:AC:E5';
Desktop ='00-00-00-00-00-1B';
Laptop ='00-00-00-00-00-18';
Playroom ='00-00-00-00-00-5C';
Betty ='00-00-00-00-00-32';
gr8 ='00-00-00-00-00-D7'
}
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
.LINK
https://gallery.technet.microsoft.com/scriptcenter/Send-WOL-packet-using-0638be7b
#>
[CmdletBinding()]
param(
[Parameter(Mandatory=$True,Position=1)]
[string]$mac,
[string]$ip="255.255.255.255",
[int]$port=9
)
$broadcast = [Net.IPAddress]::Parse($ip)
Write-Verbose "Using MAC string $mac"
Write-Verbose "Using Broadcast $broadcast"
$mac=(($mac.replace(":","")).replace("-","")).replace(".","")
$target=0,2,4,6,8,10 | % {[convert]::ToByte($mac.substring($_,2),16)}
$packet = (,[byte]255 * 6) + ($target * 16)
Write-Verbose "Packet $packet"
$UDPclient = new-Object System.Net.Sockets.UdpClient
$UDPclient.Connect($broadcast,$port)
[void]$UDPclient.Send($packet, 102)
}
if ($MacString){
If ($Table.ContainsKey($MacString)) {$MacString=$Table[$MacString]}
If ($MacString -NotMatch '^([0-9A-F]{2}[:-]){5}([0-9A-F]{2})$') {
Throw 'Mac address must be 6 hex bytes separated by : or -'
}
Send-WOL -mac $MacString
}
<#
.SYNOPSIS
The script shows a Message in BalloonTipp Area
.DESCRIPTION
Hiermit können ohne Unterbrechung des Scripts Nachrichten im rechten Nachrichten Areal von Windows ausgegeben werden.
.EXAPMLE
Show–BalloonTip –Text 'Script has retrieved data' –Title 'All is fine'
.EXAMPLE
Show–BalloonTip –Text 'NAS bitte per Hand starten, eventuell war Stromausfall' –Title 'Achtung' –Icon Warning –Timeout 15000
.LINK
http://www.powertheshell.com/balloontip/
#>
function Show–BalloonTip
{
[CmdletBinding(SupportsShouldProcess = $true)]
param
(
[Parameter(Mandatory=$true)]
$Text,
[Parameter(Mandatory=$true)]
$Title,
[ValidateSet('None', 'Info', 'Warning', 'Error')]
$Icon = 'Info',
$Timeout = 10000
)
Add-Type -AssemblyName System.Windows.Forms
if ($script:balloon -eq $null)
{
$script:balloon = New-Object System.Windows.Forms.NotifyIcon
}
$path = Get-Process -id $pid | Select-Object -ExpandProperty Path
$balloon.Icon = [System.Drawing.Icon]::ExtractAssociatedIcon($path)
$balloon.BalloonTipIcon = $Icon
$balloon.BalloonTipText = $Text
$balloon.BalloonTipTitle = $Title
$balloon.Visible = $true
$balloon.ShowBalloonTip($Timeout)
}
# Shortcut File Name erzeugen über Shell Object Startup Pfad ermitteln
$ShortcutFile = (New-Object -ComObject Shell.Application).NameSpace(0x07).Self.Path + "\StarteNas.lnk"
# Shortcut über Wscript Object erzeugen und Inhalte setzen
$WScriptShell = New-Object -ComObject WScript.Shell
$Shortcut = $WScriptShell.CreateShortcut($ShortcutFile)
# Powershell findet sich im Pfad $PsHome
$Shortcut.TargetPath = "$PsHome\powershell.exe"
$Shortcut.Description = "Beschreibung"
$Shortcut.WorkingDirectory = $PSScriptRoot
$Shortcut.Arguments ="-WindowStyle Hidden &'$PSScriptRoot\StarteNAS.ps1'"
$Shortcut.Save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment