Friday, 31 December 2010

Count-GDDuplicate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Finds duplicates in the GD archive – A script
  4.     for New Year’s eve! 
  5. .DESCRIPTION 
  6.  
  7. .NOTES 
  8.     File Name  : Count-GDDuplicate.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 
  11. .LINK 
  12.     http://pshscripts.blogspot.com/2010/12/count-gdduplicateps1.html 
  13. .EXAMPLE 
  14.     Psh[Cookham8]>C:\foo\Count-GdDuplicate.ps1 
  15.     Count-GDDuplicate.ps1 - v 1.0.1 
  16.  
  17.     99 shows have duplicates 
  18.     109 shows are duplicates of others 
  19. #> 
  20.  
  21. ### 
  22. # Start of Script 
  23. ## 
  24.  
  25. # Constants: 
  26. # $GDDiskRoot    - where to find shows 
  27. # $DeadShowBase  - folder at top of gd shows 
  28.  
  29. $GDDiskRoot = "M:" 
  30. $DeadShowBase = $GDDiskRoot + "\gd" 
  31.  
  32. # Announce Ourselves 
  33. "DuplicateCount.ps1 - v 0.0.1" 
  34. "+---------------------------------+" 
  35. "! Find Duplicate Shows :  $DeadShowBase   !" 
  36. "+---------------------------------+" 
  37. "" 
  38. # Get start time 
  39. $starttime = Get-Date 
  40. set-strictmode -off 
  41.  
  42. # Get the Dead shows 
  43.  
  44. $Dir = ls $DeadShowBase | where {$_.psiscontainer} 
  45. $DeadShows = $Dir.count 
  46. if ($DeadSHows -le 0) {"no shows found - check constants"; return
  47.  
  48. # So here look for duplicates. 
  49.  
  50. $shows =@{} 
  51.  
  52. $j = 0 
  53. foreach ($show in $dir){ 
  54. $j++ 
  55. $showdate = $show.name.substring(0,10) 
  56. if ($shows.$showdate) { 
  57.   $shows.$showdate++ 
  58. }   
  59. else
  60.   $shows += @{$showdate=1} 
  61. }    
  62.  
  63.  
  64. $shows = $shows.getenumerator() | Sort name 
  65. $totaldups = 0 
  66. $totaldupshows = 0 
  67. foreach ($show in $shows){ 
  68.    if ($show.value -gt 1) { 
  69.      # "{1} copies of: {0}" -f  $show.name, $show.value 
  70.      $totaldups++    
  71.      $totaldupshows += $show.value - 1 # anything after the 1st is a dup 
  72.    } 
  73.  
  74. # Display Summary 
  75. "{0} shows have duplicates" -f $totaldups 
  76. "{0} shows are duplicates of others" -f $totaldupshows 
Technorati Tags:

Thursday, 23 December 2010

Set-IseThemeVim.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script sets an ISE Theme to similar to the old VIM editor. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to values consistent 
  6.     with the VIM editor, beloved by many, particularly on the Powershell 
  7.     product team. This script is based on Davis Mohundro's blog post
  8.     (http://bit.ly/iib5IM), updated for RTM of PowerShell V2.0. See also  
  9. .NOTES 
  10.     File Name  : Set-ISEThemeVIM.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscriptsbog.blogspot.com  
  16.         http://bit.ly/iaA2iX
  17. .EXAMPLE 
  18.     This script when run resets colours on key panes, including 
  19.     colourising tokens in the script pane. Try it and see it... 
  20. #> 
  21.  
  22.  
  23. # PowerShell ISE version of the VIM blackboard theme at  
  24. # http://www.vim.org/scripts/script.php?script_id=2280 
  25.  
  26. # Set font name and size 
  27. $psISE.Options.FontName = 'Courier New' 
  28. $psISE.Options.FontSize = 16 
  29.  
  30. # Set colours for output pane 
  31. $psISE.Options.OutputPaneBackgroundColor     = '#FF000000' 
  32. $psISE.Options.OutputPaneTextBackgroundColor = '#FF000000' 
  33. $psISE.Options.OutputPaneForegroundColor     = '#FFFFFFFF' 
  34.  
  35. # Set colours for command pane 
  36. $psISE.Options.CommandPaneBackgroundColor    = '#FF000000' 
  37.  
  38. # Set colours for script pane 
  39.  
  40. $psise.options.ScriptPaneBackgroundColor    ='#FF000000' 
  41.  
  42. # Set colours for tokens in Script Pane 
  43. $psISE.Options.TokenColors['Command'] = '#FFFFFF60' 
  44. $psISE.Options.TokenColors['Unknown'] = '#FFFFFFFF' 
  45. $psISE.Options.TokenColors['Member'] = '#FFFFFFFF' 
  46. $psISE.Options.TokenColors['Position'] = '#FFFFFFFF' 
  47. $psISE.Options.TokenColors['GroupEnd'] = '#FFFFFFFF' 
  48. $psISE.Options.TokenColors['GroupStart'] = '#FFFFFFFF' 
  49. $psISE.Options.TokenColors['LineContinuation'] = '#FFFFFFFF' 
  50. $psISE.Options.TokenColors['NewLine'] = '#FFFFFFFF' 
  51. $psISE.Options.TokenColors['StatementSeparator'] = '#FFFFFFFF' 
  52. $psISE.Options.TokenColors['Comment'] = '#FFAEAEAE' 
  53. $psISE.Options.TokenColors['String'] = '#FF00D42D' 
  54. $psISE.Options.TokenColors['Keyword'] = '#FFFFDE00' 
  55. $psISE.Options.TokenColors['Attribute'] = '#FF84A7C1' 
  56. $psISE.Options.TokenColors['Type'] = '#FF84A7C1' 
  57. $psISE.Options.TokenColors['Variable'] = '#FF00D42D' 
  58. $psISE.Options.TokenColors['CommandParameter'] = '#FFFFDE00' 
  59. $psISE.Options.TokenColors['CommandArgument'] = '#FFFFFFFF' 
  60. $psISE.Options.TokenColors['Number'] = '#FF98FE1E' 
Technorati Tags: ,,,

Set-ISEThemeDefault.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script resets the ISE to default ‘theme’. 
  4. .DESCRIPTION 
  5.     This script sets the key values in $PsIse.Options to their default 
  6.     options in $Psise.Options.DefaultOptions. This script is useful if you 
  7.     are playing with ISE options and don't quite get it right - just run this  
  8.     script to set things back to default.        
  9. .NOTES 
  10.     File Name  : Set-ISEThemeDefault.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 (ISE only) 
  13. .LINK 
  14.     This script posted to: 
  15.         http://bit.ly/gJpu2W  
  16. .EXAMPLE 
  17.     This script when run just resets the theme to the default. To view, run from 
  18.     the ISE after changing options! 
  19. #> 
  20.  
  21. # Set Basic options 
  22. $psise.options.SelectedScriptPaneState = $psise.options.DefaultOptions.SelectedScriptPaneState 
  23. $psise.options.ShowToolBar             = $psise.options.DefaultOptions.ShowToolBar 
  24. $psise.options.FontSize                = $psise.options.DefaultOptions.FontSize 
  25. $psise.options.Fontname                = $psise.options.DefaultOptions.Fontname 
  26.  
  27. # set colouring 
  28. $psise.options.ErrorForegroundColor          = $psise.options.DefaultOptions.ErrorForegroundColor 
  29. $psise.options.ErrorBackgroundColor          = $psise.options.DefaultOptions.ErrorBackgroundColor 
  30. $psise.options.WarningForegroundColor        = $psise.options.DefaultOptions.WarningForegroundColor 
  31. $psise.options.WarningBackgroundColor        = $psise.options.DefaultOptions.WarningBackgroundColor 
  32. $psise.options.VerboseForegroundColor        = $psise.options.DefaultOptions.VerboseForegroundColor 
  33. $psise.options.VerboseBackgroundColor        = $psise.options.DefaultOptions.VerboseBackgroundColor 
  34. $psise.options.DebugBackgroundColor          = $psise.options.DefaultOptions.DebugBackgroundColor 
  35. $psise.options.DebugForegroundColor          = $psise.options.DefaultOptions.DebugForegroundColor 
  36. $psise.options.OutputPaneBackgroundColor     = $psise.options.DefaultOptions.OutputPaneBackgroundColor 
  37. $psise.options.OutputPaneTextBackgroundColor = $psise.options.DefaultOptions.OutputPaneTextBackgroundColor 
  38. $psise.options.OutputPaneForegroundColor     = $psise.options.DefaultOptions.OutPutPaneForegroundColor 
  39. $psise.options.CommandPaneBackgroundColor    = $psise.options.DefaultOptions.CommandPaneBackgroundColor 
  40. $psise.options.ScriptPaneBackgroundColor     = $psise.options.DefaultOptions.ScriptPaneBackgroundColor  
  41. $psise.options.ScriptPaneForegroundColor     = $psise.options.DefaultOptions.ScriptPaneForegroundColor  
  42.  
  43. # More options 
  44. $psise.options.ShowWarningForDuplicateFiles  = $psise.options.DefaultOptions.ShowWarningForDuplicateFiles   
  45. $psise.options.ShowWarningBeforeSavingOnRun  = $psise.options.DefaultOptions.ShowWarningBeforeSavingOnRun 
  46. $psise.options.UseLocalHelp                  = $psise.options.DefaultOpitons.UseLocalHelp 
  47. $psise.options.CommandPaneUp                 = $psise.options.DefaultOptions.CommandPaneUp 
  48.  
  49. # Reset Tokens Colors 
  50. $psISE.Options.TokenColors['Attribute']          = $psISE.Options.DefaultOptions.TokenColors['Attribute'] 
  51. $psISE.Options.TokenColors['Command']            = $psISE.Options.DefaultOptions.TokenColors['Command'] 
  52. $psISE.Options.TokenColors['CommandArgument']    = $psISE.Options.DefaultOPtions.TokenColors['CommandArgument'] 
  53. $psISE.Options.TokenColors['CommandParameter']   = $psISE.Options.DefaultOptions.TokenColors['CommandParameter'] 
  54. $psISE.Options.TokenColors['Comment']            = $psISE.Options.DefaultOptions.TokenColors['Comment'] 
  55. $psISE.Options.TokenColors['GroupEnd']           = $psISE.Options.DefaultOptions.TokenColors['GroupEnd'] 
  56. $psISE.Options.TokenColors['GroupStart']         = $psISE.Options.DefaultOptions.TokenColors['GroupStart'] 
  57. $psISE.Options.TokenColors['Keyword']            = $psISE.Options.DefaultOptions.TokenColors['Keyword'] 
  58. $psISE.Options.TokenColors['LineContinuation']   = $psISE.Options.DefaultOptions.TokenColors['LineContinuation'] 
  59. $psISE.Options.TokenColors['LoopLabel']          = $psISE.Options.DefaultOptions.TokenColors['LoopLabel'] 
  60. $psISE.Options.TokenColors['Member']             = $psISE.Options.DefaultOptions.TokenColors['Member'] 
  61. $psISE.Options.TokenColors['NewLine']            = $psISE.Options.DefaultOptions.TokenColors['NewLine'] 
  62. $psISE.Options.TokenColors['Number']             = $psISE.Options.DefaultOPtions.TokenColors['Number'] 
  63. $psISE.Options.TokenColors['Position']           = $psISE.Options.DefaultOptions.TokenColors['Position'] 
  64. $psISE.Options.TokenColors['StatementSeparator'] = $psISE.Options.DefaultOptions.TokenColors['StatementSeparator'] 
  65. $psISE.Options.TokenColors['String']             = $psISE.Options.DefaultOptions.TokenColors['String'] 
  66. $psISE.Options.TokenColors['Type']               = $psISE.Options.DefaultOptions.TokenColors['Type']  
  67. $psISE.Options.TokenColors['Unknown']            = $psISE.Options.DefaultOptions.TokenColors['Unknown'] 
  68. $psISE.Options.TokenColors['Variable']           = $psISE.Options.DefaultOptions.TokenColors['Variable'] 
  69.  
  70. # Done 

Thursday, 7 October 2010

Get-WMINameSpace.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script displays all the WMI namespaces within a Windows system 
  4. .DESCRIPTION 
  5.     This script uses Get-WMIObject to retrieve the names of all the namespaces 
  6.     within a system. 
  7. .NOTES 
  8.     File Name  : Get-WMINameSpace.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to:
  13.         http://pshscripts.blogspot.com/2010/10/get-wminamespaceps1.html
  14. .EXAMPLE 
  15.     PSH [C:\foo]: .\Get-WMINameSpace.ps1 
  16.     37 Namespaces on: Cookham8 
  17.  
  18.     Namespace 
  19.     --------- 
  20.     ROOT 
  21.     ROOT\aspnet 
  22.     ROOT\CIMV2 
  23.     ROOT\CIMV2\Security 
  24.     ROOT\CIMV2\Security\MicrosoftTpm 
  25.     ... {Remainder of list snipped to save space on this page} 
  26. #> 
  27.  
  28. # Set computer name
  29. $comp = "." 
  30. # Get the name spaces on the local computer, and the local computer name 
  31. $Namespace = get-wmiobject __namespace -namespace 'root' -list -recurse -computer $comp  
  32. $hostname = hostname 
  33.  
  34. # Display number of and names of the namespaces 
  35. "{0} Namespaces on: {1}" -f $namespace.count, $hostname 
  36. $NameSpace| sort __namespace  | Format-Table @{Expression = "__Namespace"; Label = "Namespace"
Technorati Tags: ,,,

Wednesday, 6 October 2010

Get-LoopBack.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script checks whether a parameter is a Loopback Address  
  4. .DESCRIPTION 
  5.     This script checks to see if the passsed string is an IPV4
  6.     or an IPv6 loopback address and if so, displays details.
  7. .NOTES 
  8.     File Name  : Get-LoopBack.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell Version 2.0 
  11. .LINK 
  12.     This script posted to: 
  13.         http://www.pshscripts.blogspot.com  
  14.     MSDN sample posted to: 
  15.         http://msdn.microsoft.com/en-us/library/system.net.ipaddress.isloopback.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-LoopBack.ps1 
  18.     Your input address: \127.0.0.1\ is an IPv4 loopback address whose internal format is: 127.0.0.1. 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-LoopBack.ps1 ::1 
  21.     Your input address: \::1\ is an IPv6 loopback address whose internal format is: ::1. 
  22. .EXAMPLE 
  23.     PSH [C:\foo]: .\Get-LoopBack.ps1 131.107.2.200 
  24.     Your input address: \131.107.2.200\ is not a loopback address. 
  25. .PARAM 
  26.     $IPAddress - Address to look up to see if it's Loopback 
  27. #> 
  28. param
  29. [String] $IpAddress = "127.0.0.1" 
  30. )  
  31. # Setup Default answer! 
  32. $loopBack=" is not a loopback address."
  33.  
  34. # Perform syntax check by parsing the address string entered by the user. 
  35. $Address = [System.Net.IPAddress]::Parse($IpAddress); 
  36.  
  37. # Perform semantic check by verifying that the address is a valid IPv4  
  38. # or IPv6 loopback address.  
  39. if([System.Net.IPAddress]::IsLoopback($Address) -and ($address.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetworkV6) ) { 
  40.         $loopBack" is an IPv6 loopback address "
  41.                     "whose internal format is: " + $Address.ToString() + "."
  42. }            

Tuesday, 5 October 2010

Get-HostByName.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays basic DNS Information about a host. 
  4. .DESCRIPTION 
  5.     This script just gets and displays host details returnd by GetHostByName. 
  6. .NOTES 
  7.     File Name  : Get-ByName.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.net.dns.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-HostByName.ps1 
  17.  
  18.     HostName    : contoso.com 
  19.     Aliases     : {www.contoso.com} 
  20.     AddressList : {207.46.197.32, 207.46.232.182} 
  21. #> 
  22. $hostInfo = [system.net.Dns]::GetHostByName("www.contoso.com"); 
  23. $hostinfo | fl * -force 

Monday, 4 October 2010

Remove-FtpFile.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script deletes a file from an FTP Server 
  4. .DESCRIPTION 
  5.     This script is a rewrite of an MSDN Sample 
  6. .NOTES 
  7.     File Name  : Remove-FtpFile.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Remove-FtpFile.ps1 
  17.     Delete status: 250 DELE command successful. 
  18. #> 
  19. $ServerUri = New-Object System.Uri "ftp://www.reskit.net/powershell/foo.txt" 
  20. # The serverUri parameter should use the ftp:// scheme. 
  21. # It contains the name of the server file that is to be deleted. 
  22. # Example: ftp://contoso.com/someFile.txt. 
  23.  
  24. if ($ServerUri.Scheme -ne [system.Uri]::UriSchemeFtp) { 
  25.         " Bad URI"; return 
  26.  
  27. # Get the object used to communicate with the server. 
  28. $request = [system.Net.FtpWebRequest]::Create($serverUri
  29. $request.Method = [System.Net.WebRequestMethods+ftp]::Deletefile 
  30. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  31.  
  32. $response = $request.GetResponse() 
  33. "Delete status: {0}" -f $response.StatusDescription 
  34. $response.Close(); 

Sunday, 3 October 2010

Copy-FileToFtp.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script Uploads a text file to an FTP Server using PowerShell.  
  4. .DESCRIPTION 
  5.     This script first creates an FTP 'web' request to upload a file. Then the  
  6.     source file is read from disk and written up to the FTP Server. A response 
  7.     is then displayed. This is a rewrite of an MSDN Sample. 
  8. .NOTES 
  9.     File Name  : Copy-FileToFtp.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2010/10/copy-filetoftpps1.html
  15.     MSDN sample posted tot: 
  16.         http://msdn.microsoft.com/en-us/library/ms229715.aspx
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .Copy-FileToFtp.ps1 
  19.     Upload File Complete, status 226
  20.     226 Transfer complete. 
  21. #> 
  22.   
  23. # Get the object used to communicate with the server. 
  24. $Request = [System.Net.FtpWebRequest]::Create("ftp://www.reskit.net/powershell/Greetings.Txt"
  25. $Request.Method = $Request.Method = [System.Net.WebRequestMethods+ftp]::UploadFile 
  26.  
  27. # This example assumes the FTP site uses anonymous logon. 
  28. $Request.Credentials = New-Object System.Net.NetworkCredential "anonymous","tfl@psp.co.uk" 
  29.  
  30. # Copy the contents of the file to the request stream. 
  31. $FileContents = [System.IO.File]::ReadAllBytes("C:\foo\scriptlib.zip"
  32. $Request.ContentLength = $fileContents.Length
  33. $RequestStream = $request.GetRequestStream() 
  34. $RequestStream.Write($FileContents, 0, $FileContents.Length) 
  35. $RequestStream.Close()
  36. $Response = $Request.GetResponse() 
  37. "Upload File Complete, status {0}" -f $Response.StatusDescription 
  38. $Response.Close()

Monday, 27 September 2010

Show-HtmlCoding.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script encodes and decodes an HTML String 
  4. .DESCRIPTION 
  5.     This script used  
  6. .NOTES 
  7.     File Name  : Show-HtmlCoding.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/ee388364.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Show-HtmlCoding.ps1 
  17.     Original String: <this is a string123> & so is this one?? 
  18.     Encoded String : &lt;this is a string123&gt; &amp; so is this one?? 
  19.     Decoded String : <this is a string123> & so is this one?? 
  20.     Original string = Decoded string?: True    
  21. #> 
  22.  
  23. # Create string to encode/decode 
  24. $Str = "<this is a string123> & so is this one??" 
  25.  
  26. # Encode String 
  27. $Encstr = [System.Net.WebUtility]::HtmlEncode($str
  28.  
  29. # Decode String 
  30. $Decstr = [System.Net.WebUtility]::HtmlDecode($EncStr
  31.  
  32. # Display strings 
  33. "Original String: {0}" -f $Str 
  34. "Encoded String : {0}" -f $Encstr 
  35. "Decoded String : {0}" -f $Decstr 
  36. $eq = ($str -eq $Decstr
  37. "Original string = Decoded string?: {0}" -f $eq  

Sunday, 26 September 2010

Get-FTPDirectory.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display the root of an FTP site. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample. 
  6. .NOTESW 
  7.     File Name  : Get-FtpDirectory.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         htthttp://pshscripts.blogspot.com/2010/09/get-ftpdirectoryps1.html 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/ms229716.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpDirectory.ps1 
  17.     drwxrwxrwx   1 user     group           0 Dec  4  2005 pcpro 
  18.     drwxrwxrwx   1 user     group           0 Sep 23 15:18 PowerShell 
  19.     ... {Listing truncated} 
  20.     Download Complete, status: 
  21.     226-Maximum disk quota limited to 100000 Kbytes 
  22.         Used disk quota 78232 Kbytes, available 21767 Kbytes 
  23.     226 Transfer complete. 
  24. #> 
  25. # Get the object used to communicate with the server. 
  26. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net"
  27. $Request.Method =  [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails 
  28.  
  29. # This example assumes the FTP site uses anonymous logon. 
  30. # Username/password not real 
  31. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous",tfl@psp.co.uk 
  32.  
  33. $Response = $Request.GetResponse() 
  34. $ResponseStream = $Response.GetResponseStream() 
  35.  
  36. # Read and display the text in the file 
  37. $Reader = new-object System.Io.StreamReader $Responsestream 
  38. [System.Console]::Writeline($Reader.ReadToEnd()) 
  39.  
  40. # Display Status 
  41. "Download Complete, status:" 
  42. $response.StatusDescription  
  43.  
  44. # Close Reader and Response objects 
  45. $Reader.Close() 
  46. $Response.Close() 

Get-FtpFile.ps1

  1. # 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display a text file. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN Sample 
  6. .NOTESW 
  7.     File Name  : Get-FtpFile.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/09/get-ftpfileps1.html 
  13.     MSDN sample posted to: 
  14.         http://msdn.microsoft.com/en-us/library/ms229711.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpFile.ps1' 
  17.     This is Hello.Txt from www.reskit.net 
  18.     Have a great day! 
  19.   
  20.     Download Complete, status: 
  21.     226-Maximum disk quota limited to 100000 Kbytes 
  22.         Used disk quota 78232 Kbytes, available 21767 Kbytes 
  23.     226 Transfer complete.     
  24. #> 
  25.  
  26. # Get the object used to communicate with the server. 
  27. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net/hello.txt"); 
  28. $Method =  [System.Net.WebRequestMethods+Ftp]::DownloadFile 
  29.  
  30. # This example assumes the FTP site uses anonymous logon. 
  31. # Username/password not real 
  32. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous","tfl@psp.co.uk"
  33. $ResponseStream = $Response.GetResponseStream() 
  34.  
  35. # Read and display the text in the file 
  36. $Reader = new-object System.Io.StreamReader $ResponseStream 
  37. [System.Console]::Writeline($Reader.ReadToEnd()) 
  38.  
  39. # Display Status 
  40. "Download Complete, status:" 
  41. $response.StatusDescription  
  42.  
  43. # Close Reader and Response objects 
  44. $Reader.Close() 
  45. $Response.Close() 

Friday, 24 September 2010

New-Task.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a scheduled task object. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell 
  6. .NOTES 
  7.     File Name  : New-Task.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/aa383665%28VS.85%29.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\New-Task.ps1
  17.     Time Now       : 9/24/2010 12:43:47 PM 
  18.     Task startTime : 2010-09-24T12:44:17 
  19.     Task endTime   : 2010-09-24T12:48:47 
  20.     Task definition created. About to submit the task... 
  21.   
  22.   
  23.     Name               : Test TimeTrigger 
  24.     Path               : Test TimeTrigger 
  25.     State              : 3 
  26.     Enabled            : True 
  27.     LastRunTime        : 12/30/1899 12:00:00 AM 
  28.     LastTaskResult     : 1 
  29.     NumberOfMissedRuns : 0 
  30.     NextRunTime        : 9/24/2010 12:44:17 PM 
  31.     Definition         : System.__ComObject 
  32.     Xml                : <?xml version="1.0" encoding="UTF-16"?> 
  33.                          <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"
  34.                            <RegistrationInfo> 
  35.                              <Author>Thomas Lee</Author> 
  36.                              <Description>Start notepad at a certain time</Description> 
  37.                            </RegistrationInfo> 
  38.                            <Triggers> 
  39.                              <TimeTrigger id="TimeTriggerId"
  40.                                <StartBoundary>2010-09-24T12:44:17</StartBoundary> 
  41.                                <EndBoundary>2010-09-24T12:48:47</EndBoundary> 
  42.                                <ExecutionTimeLimit>PT5M</ExecutionTimeLimit> 
  43.                                <Enabled>true</Enabled> 
  44.                              </TimeTrigger> 
  45.                            </Triggers> 
  46.                            <Settings> 
  47.                              <IdleSettings> 
  48.                                <Duration>PT10M</Duration> 
  49.                                <WaitTimeout>PT1H</WaitTimeout> 
  50.                                <StopOnIdleEnd>true</StopOnIdleEnd> 
  51.                                <RestartOnIdle>false</RestartOnIdle> 
  52.                              </IdleSettings> 
  53.                              <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
  54.                              <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
  55.                              <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
  56.                              <AllowHardTerminate>true</AllowHardTerminate> 
  57.                              <StartWhenAvailable>true</StartWhenAvailable> 
  58.                              <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
  59.                              <AllowStartOnDemand>true</AllowStartOnDemand> 
  60.                              <Enabled>true</Enabled> 
  61.                              <Hidden>false</Hidden> 
  62.                              <RunOnlyIfIdle>false</RunOnlyIfIdle> 
  63.                              <WakeToRun>false</WakeToRun> 
  64.                              <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> 
  65.                              <Priority>7</Priority> 
  66.                            </Settings> 
  67.                            <Actions Context="Author"
  68.                              <Exec> 
  69.                                <Command>C:\Windows\System32\notepad.exe</Command> 
  70.                              </Exec> 
  71.                            </Actions> 
  72.                            <Principals> 
  73.                              <Principal id="Author"
  74.                                <UserId>COOKHAM\tfl</UserId> 
  75.                                <LogonType>InteractiveToken</LogonType> 
  76.                              </Principal> 
  77.                            </Principals> 
  78.                          </Task> 
  79.  
  80.     Task submitted. 
  81. #> 
  82. # Helper Function 
  83. function XMLTIME{ 
  84. Param ( $T) 
  85. $csecond = $t.Second.ToString() 
  86. $cminute = $t.minute.ToString() 
  87. $chour = $t.hour.ToString() 
  88. $cday  = $t.day.ToString() 
  89. $cmonth  = $t.month.ToString() 
  90. $cyear = $t.year.ToString() 
  91.  
  92. $date =  $cyear + "-" 
  93. if ($cmonth.Length -eq 1) { $date += "0" + $cmonth + "-"}  
  94. else                      { $date += $cmonth + "-"
  95. if ($cday.length -eq 1)   { $date += "0" + $cday + "T"
  96. else                      { $date += $cday + "T"
  97. if ($chour.length -eq 1)  { $date += "0" + $chour + ":"
  98. else                      { $date += $chour + ":"
  99. if ($cminute.length -eq 1){ $date += "0" + $cminute + ":"
  100. else                      { $date += $cminute + ":"
  101. if ($csecond.length -eq 1){ $date += "0" + $csecond} 
  102. else                      { $date += $csecond} 
  103. # return 
  104. $date 
  105.  
  106. ## Script starts here 
  107.  
  108. # A constant that specifies a time-based trigger. 
  109. $TriggerTypeTime = 1 
  110. # A constant that specifies an executable action. 
  111. $ActionTypeExec = 0    
  112.  
  113. # Create and connect to the service 
  114. $service = New-Object -com schedule.service  
  115. $service.Connect() 
  116.  
  117. # Get a folder to create a task definition in.  
  118. $rootFolder = $service.GetFolder("\") 
  119.  
  120. # The taskDefinition variable is the TaskDefinition object. 
  121. # The flags parameter is 0 because it is not supported. 
  122. $taskDefinition = $service.NewTask(0)  
  123.  
  124. # Define information about the task. 
  125. # Set the registration info for the task by  
  126. # creating the RegistrationInfo object. 
  127. $regInfo = $taskDefinition.RegistrationInfo 
  128. $regInfo.Description = "Start notepad at a certain time" 
  129. $regInfo.Author = "Thomas Lee" 
  130.  
  131. # Set the principal for the task 
  132. $principal = $taskDefinition.Principal 
  133.  
  134. # Set the logon type to interactive logon 
  135. $principal.LogonType = 3 
  136.  
  137. # Set the task setting info for the Task Scheduler by 
  138. # creating a TaskSettings object. 
  139. $settings = $taskDefinition.Settings 
  140. $settings.Enabled = $True 
  141. $settings.StartWhenAvailable = $True 
  142. $settings.Hidden = $False 
  143.  
  144. # Create a time-based trigger. 
  145. $triggers = $taskDefinition.Triggers 
  146. $trigger = $triggers.Create($TriggerTypeTime) 
  147.  
  148. # Trigger variables that define when the trigger is active. 
  149. $time = ([system.datetime]::now).addseconds(30) 
  150. $startTime = XmlTime($time) 
  151.  
  152. $time = ([system.datetime]::now).addminutes(5) 
  153. $endTime = XmlTime($time) 
  154.  
  155. "Time Now       : {0}" -f (Get-Date -display time) 
  156. "Task startTime : {0}" -f $startTime 
  157. "Task endTime   : {0}" -f $endTime 
  158.  
  159. $trigger.StartBoundary = $startTime 
  160. $trigger.EndBoundary = $endTime 
  161. $trigger.ExecutionTimeLimit = "PT5M"    #Five minutes 
  162. $trigger.Id = "TimeTriggerId" 
  163. $trigger.Enabled = $True 
  164.  
  165. # Create the action for the task to execute. 
  166.  
  167. # Add an action to the task to run notepad.exe. 
  168. $Action = $taskDefinition.Actions.Create( $ActionTypeExec ) 
  169. $Action.Path = "C:\Windows\System32\notepad.exe" 
  170.  
  171. "Task definition created. About to submit the task..." 
  172.  
  173. # Register (create) the task. 
  174. $rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3) 
  175.  
  176. # all done! 
  177. "Task submitted."