Einstieg in die PowerShell

URL dieses Beitrags: http://blog.stefan-macke.com/2007/04/06/einstieg-in-die-powershell/

Heute habe ich mal zum Einstieg in die Windows PowerShell ein kleines Script erstellt, dass meine RSSOwl-Konfigurationsdatei auf meinen FTP-Server kopiert bzw. von dort herunterlädt. Die Datei ständig manuell zwischen 3 PCs (Arbeit, Privat, Laptop) zu synchronisieren ging mir mit der Zeit doch auf den Senkel…

Das Script basiert auf einem Beispiel von Joannes Vermorel. Ich habe es nur ein wenig erweitert. Das fertige Script sieht nun wie folgt aus. Es benötigt lediglich einen Parameter: up oder down.

  1. Param ($UpDown = "up");
  2.  
  3. $Server = "ftp.server.com";
  4. $User = "username";
  5. $Password = "password";
  6.  
  7. function ExecuteFTPCommands($FileCommand)
  8. {
  9.     $FtpCommandFilePath = [System.IO.Path]::GetFullPath("FTPCommand.tmp");
  10.     $FtpCommands = @( $User, $Password, "cd RSSOwl", "bin", "quote pasv", $FileCommand, "quit" );
  11.     $FtpCommand = [String]::Join( "`r`n", $FtpCommands );
  12.     set-content $FtpCommandFilePath $FtpCommand;
  13.     ftp "-s:$FtpCommandFilePath" $Server;
  14.     remove-item $FtpCommandFilePath;
  15. }
  16.  
  17.  
  18. # Get path of local user profile
  19. $UserProfile = $env:UserProfile;
  20. $RSSOwlPath = $UserProfile + "\.rssowl";
  21. $RSSOwlFile = $RSSOwlPath + "\user.xml";
  22.  
  23. # check whether RSSOwl’s config file exists
  24. if (!(Test-Path $RSSOwlFile))
  25. {
  26.     write-host ("RSSOwl file not found: " + $RSSOwlFile) -foregroundcolor "red";
  27.     exit;
  28. }
  29.  
  30.  
  31. if ($UpDown.equals("up"))
  32. {
  33.     write-host "Uploading RSSOwl’s config file…" -foregroundcolor "green";
  34.     $FtpUploadCommand = ‘PUT "’ + $RSSOwlFile + ‘"’;
  35.     ExecuteFTPCommands([string]$ftpUploadCommand);
  36. }
  37. elseif ($UpDown.equals("down"))
  38. {
  39.     write-host "Downloading RSSOwl’s config file…" -foregroundcolor "green";
  40.     $FtpUploadCommand = ‘GET "user.xml"’;
  41.     ExecuteFTPCommands([string]$ftpUploadCommand);
  42.     Move-Item user.xml $RSSOwlPath -force;
  43.     C:\Programme\RSSOwl\rssowl.exe
  44. }
  45. else
  46. {
  47.     write-host ("Wrong parameter: " + $UpDown + "`nAllowed: ‘up’ or ‘down’") -foregroundcolor "red";
  48. }

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>