NetSendAll Teil 3: PowerShell-Script NetSendAll

URL dieses Beitrags: http://blog.stefan-macke.com/2008/05/03/netsendall-teil-3-powershell-script-netsendall/

Es folgt der letzte Artikel meiner kleinen Reihe zum PowerShell-Skript NetSendAll: das eigentliche Script, das die Nachricht versendet. Es nutzt die beiden vorgestellten Funktionen PingPC und ReadTextFile um die aus der Textdatei eingelesenen PCs anzupingen. Bei Erfolg wird die Nachricht gesendet bzw. der Nachrichtendienst gestartet, falls dieser nicht läuft. In letzterem Fall wird dann später erneut versucht, die Nachricht zu senden.

  1. # Sends a message to every PC in a text file via "net send"
  2. # ---------------------------------------------------------------------------- #
  3. # the text file containing the PCs
  4. $filePCsOnline = "PCs.txt";
  5. # ---------------------------------------------------------------------------- #
  6. # the script needs only the message as a parameter
  7. if ($args.count -ne 1)
  8. {
  9.         write-host ("Usage: .\" + $MyInvocation.MyCommand + " ""this is the message""") -foregroundcolor "red";
  10.         exit;
  11. }
  12. $message = $args[0];
  13. # these arrays will be filled with PCs to ping/notify
  14. $pcsOnline = ReadTextFile $filePCsOnline;
  15. $pcsTryAgain = @();
  16. write-host ("Sending the following message to " + $pcsOnline.length + " PCs:`n  " + $message);
  17. # find out if PCs are online and notify them resp. enable messenger if disabled
  18. foreach ($pc in $pcsOnline)
  19. {
  20.         if (PingPC($pc.Name))
  21.         {
  22.                 $svc = gwmi win32_service -computername $pc.Name | ? { $_.name -eq "Messenger" }
  23.                 if ($svc.state -eq "Running")
  24.                 {
  25.                         write-host ("Messenger is running. Sending message to " + $pc.Name) -foregroundcolor "green";
  26.                         net send $pc.Name $message
  27.                 }
  28.                 else
  29.                 {
  30.                         write-host ("Messenger is not running on " + $pc.Name + ". Starting it...") -foregroundcolor "magenta";
  31.                         [void]$svc.startservice();
  32.                         $pcsTryAgain += $pc;
  33.                 }
  34.         }
  35.         else
  36.         {
  37.                 write-host ($pc.Name + " is offline") -foregroundcolor "red";
  38.         }
  39. }
  40. if ($pcsTryAgain.length -gt 0)
  41. {
  42.         write-host ("" + $pcsTryAgain.length + " PCs with disabled Messenger will be tried again in 5 seconds");
  43.         for ($i = 1; $i -le 5; $i++)
  44.         {
  45.                 write-host $i;
  46.                 sleep(1);
  47.         }
  48.         foreach ($pc in $pcsTryAgain)
  49.         {
  50.                 if (PingPC($pc.Name))
  51.                 {
  52.                         $svc = gwmi win32_service -computername $pc.Name | ? { $_.name -eq "Messenger" }
  53.                         if ($svc.state -eq "Running")
  54.                         {
  55.                                 write-host ("Messenger is running. Sending message to " + $pc.Name) -foregroundcolor "green";
  56.                                 net send $pc.Name $message
  57.                         }
  58.                         else
  59.                         {
  60.                                 write-host ("Messenger is not running on " + $pc.Name + ".") -foregroundcolor "red";
  61.                         }
  62.                 }
  63.                 else
  64.                 {
  65.                         write-host ($pc.Name + " is offline") -foregroundcolor "red";
  66.                 }
  67.         }
  68. }

Beispielausgabe des PowerShell-Scripts NetSendAll

Download

Das Script inkl. der beiden benötigten Funktionen gibt es hier zum Download: NetSendAll.ps1

Füge diesen Artikel zu deinen Bookmarks hinzu Diese Icons verzweigen auf soziale Netzwerke bei denen Nutzer neue Inhalte finden und mit anderen teilen können.
  • del.icio.us
  • bodytext
  • MisterWong
  • Reddit
  • Technorati
  • Spurl
  • description

Einen Kommentar schreiben

XHTML: Diese Tags sind erlaubt: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>