Friday, 30 December 2011

Show-MessageBox.ps1

  1. <#
  2. .SYNOPSIS
  3.     This script displays a message box and then processes it
  4. .DESCRIPTION
  5.     This script firsts creates a wscript.shell object and
  6.     invokes the popup method to display a message. The script
  7.     then processes the response to the geroup (timeout, yes, no).
  8. .NOTES
  9.     File Name : Show-MessageBox.ps1
  10.     Author : Thomas Lee - tfl@psp.co.uk
  11.     Requires : PowerShell Version 2.0
  12. .LINK
  13.     This script posted to:
  14.         http://www.pshscripts.blogspot.com
  15.     MSDN sample posted tot:
  16.         http://msdn.microsoft.com/en-us/library/x83z1d9f%28VS.84%29.aspx
  17. .EXAMPLE
  18.     Left as an exercise to the reader!
  19. #>
  20. # Create the shell object
  21. $WshShell = New-Object -Com Wscript.Shell
  22. # Call the Popup method with a 7 second timeout.
  23. $Btn = $WshShell.Popup("Do you feel alright?", 7, "Question:", 0x4 + 0x20)
  24. # Process the response
  25. switch ($Btn) {
  26. # Yes button pressed.
  27. 6 {"Glad to hear you feel alright."}
  28. # No button pressed.
  29. 7 {"Hope you're feeling better soon."}
  30. # Timed out.
  31. -1 {"Is there anybody out there?"}
  32. }

Monday, 12 December 2011

Get-PortAndProtocolFromUrl.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script strips out a port and protocol number from a URL  
  4. .DESCRIPTION 
  5.     This script creates a regular expression reged then uses it to  
  6.     match against the URL to get the protocol and port. This is a 
  7.     re-write of the MSDN sample. 
  8. .NOTES 
  9.     File Name  : Get-PortAndProtocolFromUrl.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/63ew9az0.aspx 
  17. .EXAMPLE 
  18.     PowerShell> .\Get-PortAndProtocolFromUrl.ps1 
  19.     Port    : http 
  20.     Protocol: 8080    
  21. #> 
  22.  
  23. # Set URL 
  24. $url = "http://www.contoso.com:8080/letters/readme.html" 
  25.  
  26. # Create Regex, then match against the URL 
  27. $r = new-object System.Text.RegularExpressions.Regex "^(?<proto>\w+)://[^/]+?:(?<port>\d+)?/" 
  28. $m = $r.Match($url
  29.  
  30. # Print results 
  31. if ($m.Success) { 
  32.    "Port    : {0}" -f $M.groups["proto"].value 
  33.    "Protocol: {0}" -f $M.groups["port"].value 
  34.            

Wednesday, 7 December 2011

Confirm-ValidEmailAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script validates email addresses based on 
  4.     MSFT published Regular Expression. This is a  
  5.     re-write with PowerShell of an existing bit of  
  6.     MSDN sample code  
  7. .DESCRIPTION 
  8.     This script first creates a function to validate 
  9.     an email address. It uses a large regex that is 
  10.     documented at the MSDN page noted below. The script 
  11.     then creates an array of email addreses and then 
  12.     validates them against the function and displays 
  13.     the results. 
  14. .NOTES 
  15.     File Name  : Confirm-ValidEmailAddress.ps1 
  16.     Author     : Thomas Lee - tfl@psp.co.uk 
  17.     Requires   : PowerShell Version 2.0 
  18. .LINK 
  19.     This script posted to: 
  20.         http://pshscripts.blogspot.com/2011/12/confirm-validemailaddressps1.html 
  21.     MSDN sample posted to: 
  22.         http://msdn.microsoft.com/en-us/library/01escwtf.aspx  
  23. .EXAMPLE 
  24.     Valid: david.jones@proseware.com 
  25.     Valid: d.j@server1.proseware.com 
  26.     Valid: jones@ms1.proseware.com 
  27.     Invalid: j.@server1.proseware.com 
  28.     Invalid: j@proseware.com9 
  29.     Valid: js#internal@proseware.com 
  30.     Valid: j_9@[129.126.118.1] 
  31.     Invalid: j..s@proseware.com 
  32.     Invalid: js*@proseware.com 
  33.     Invalid: js@proseware..com 
  34.     Invalid: js@proseware.com9 
  35.     Valid: j.s@server1.proseware.com 
  36.     Valid: tfl@psp.co.uk 
  37.     Valid: cuddly.penguin@cookham.net 
  38. #> 
  39.  
  40. Function IsValidEmail   { 
  41. Param ([string] $In
  42. # Returns true if In is in valid e-mail format. 
  43. [system.Text.RegularExpressions.Regex]::IsMatch($In,  
  44.               "^(?("")(""[^""]+?""@)|(([0-9a-zA-Z]((\.(?!\.))|[-!#\$%&'\*\+/=\?\^`\{\}\|~\w])*)(?<=[0-9a-zA-Z])@))" +  
  45.               "(?(\[)(\[(\d{1,3}\.){3}\d{1,3}\])|(([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,6}))$");  
  46. } # End of IsValidEmail 
  47.     
  48. [string[]] $emailAddresses = "david.jones@proseware.com", "d.j@server1.proseware.com",  
  49.                             "jones@ms1.proseware.com", "j.@server1.proseware.com",  
  50.                             "j@proseware.com9", "js#internal@proseware.com",  
  51.                             "j_9@[129.126.118.1]", "j..s@proseware.com",  
  52.                             "js*@proseware.com", "js@proseware..com",  
  53.                             "js@proseware.com9", "j.s@server1.proseware.com"
  54.                             "tfl@psp.co.uk", "cuddly.penguin@cookham.net"  
  55.                              
  56. ForEach ($emailAddress in $emailAddresses)    { 
  57.   if (IsValidEmail($emailAddress)) { 
  58.        "Valid: {0}" -f $emailAddress 
  59.        } 
  60.   else
  61.         "Invalid: {0}" -f $emailAddress 
  62.        }     
  63. }