Hauptscript
Die eigentliche Aufgabe erledigt das Script StarteNAS.ps1Dies sendet ein magic Packet zur NAS, geht dann in eine Prüfschleife und wartet darauf, dass die IP Adresse der NAS erreichbar ist. Bei Erfolg oder Misserfolg wird der passende Balloon-Tip ausgegeben.
Die Zahl $x muss man seiner realen Umgebung anpassen, bei mir ist die NAS nach ca. 10 Schleifen Durchläufen gestartet, nach 15 Durchläufen ist irgendetwas schief gelaufen.
Hilfsmittel aus der Sammlung
Die Function Send-WOL habe ich im Scriptcenter der Technet gefunden. Ich habe diese im Script SendWOLPacket.ps1 noch mit der Variante kombiniert, einen oder mehrere PC aus einem vorhandenen Array zu starten.Für etwas Benutzerinformation verwende ich noch das Script BalloonTipp.ps1 welches nur die Function Show-BalloonTip liefert. Wie der Name sagt, kann man damit Nachrichten im BalloonTipp Area von Windows erzeugen.
Beides sind nur allgemein verwendbare Scripts aus der Sammlung.
Installation
Alle 3 Scripts kopiert man in einen passenden Pfad, z.B. c:\Tools\ScriptsEs gibt viele Möglichkeiten ein Script bei der Anmeldung einen Benutzers zu starten, die allereinfachste ist immer noch der Autostartordner, auch wenn MS den immer weiter versteckt hat. So findet man ihn schnell:
Windows+r (Ausführen) und dort ins Fenster shell:startup eintragen und enter drücken. Schon wird der persönliche Autostartordner geöffnet.
Hier erzeugt man jetzt eine Verknüpfung mit folgendem Ziel:
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass &'C:\Tools\Scripts\StarteNAS.ps1'
Tipp: Der Parameter ExecutionPolicy ignoriert die gleichnamige Einstellung im System.
Wer gerne den Link mit einem Script erzeugen möchte -> ShortCutFileErzeugen.ps1
NAS nach der Arbeit herunterfahren
Meine NAS kann eine oder mehrere IP Adressen überwachen und sich automatisch herunterfahren wenn diese IP Adressen im Netz nicht mehr aktiv sind. (Openmediavault Plugin autoshutdown)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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' | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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 | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
.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) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
Hallo, wenn ich die Befehlszeile C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass &'C:\Tools\Scripts\StarteNAS.ps1' so eingebe, erhalte ich folgende Meldung:
AntwortenLöschenIn Zeile:1 Zeichen:103
+ ... \powershell.exe -WindowStyle Hidden -ExecutionPolicy Bypass &'C:\Tool ...
+ ~
Das kaufmännische Und-Zeichen (&) ist nicht zulässig. Der &-Operator ist für eine zukünftige Verwendung reserviert.
Verwenden Sie das kaufmännische Und-Zeichen in doppelten Anführungszeichen ("&"), um es als Teil einer Zeichenfolge zu
übergeben.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : AmpersandNotAllowed
Ich wäre für eine Lösung dankbar.
Als "Befehlszeile" verwendest Du offenbar die Powershell. Diese Zeile ist aber für die CMD bzw. Ausführen Zeile gedacht. Innerhalb der Powershell, führt dieser Aufruf zu genau dieser Fehlermeldung.
Löschen