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. }                                         

Sunday, 20 November 2011

Show-FileInformation.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays information returned from the  
  4.     file version object. 
  5. .DESCRIPTION 
  6.     This script gets, then displays, all the information returned 
  7.     from the System.Diagnostics.Fileinfo of Notepad.exe 
  8. .NOTES 
  9.     File Name  : Show-FileInformation.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell Version 2.0 
  12. .LINK 
  13.     This script posted to: 
  14.         http://http://pshscripts.blogspot.com 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo_properties.aspx 
  17. .EXAMPLE 
  18.     Psh> .\Show-FileInformation.ps1 
  19.     File Major Part for C:\Windows\system32\Notepad.exe is: 6             
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Display all the file information: 
  28. "FileInfo information for {0}" -f $file 
  29. "Comments           : {0}"  -f $myFileVersionInfo.Comments 
  30. "Company Name       : {0}"  -f $myFileVersionInfo.CompanyName 
  31. "FileBuldPart       : {0}"  -f $myFileVersionInfo.FileBuildPart 
  32. "FileDescription    : {0}"  -f $myFileVersionInfo.FileDescription 
  33. "FileMajorPart      : {0}"  -f $myFileVersionInfo.FileMajorPart 
  34. "FileMinorPart      : {0}"  -f $myFileVersionInfo.FileMinorPart 
  35. "FilePrivatePart    : {0}"  -f $myFileVersionInfo.FilePrivatePart 
  36. "FileName           : {0}"  -f $myFileVersionInfo.FileName  
  37. "FileVersion        : {0}"  -f $myFileVersionInfo.FileVersion 
  38. "InternalName       : {0}"  -f $myFileVersionInfo.InternalName 
  39. "IsDebug            : {0}"  -f $myFileVersionInfo.IsDebug 
  40. "IsPatched          : {0}"  -f $myFileVersionInfo.IsPatched 
  41. "IsPreRelease       : {0}"  -f $myFileVersionInfo.IsPreRelease 
  42. "IsPrivateBuild     : {0}"  -f $myFileVersionInfo.IsPrivateBuild 
  43. "IsSpecialBuild     : {0}"  -f $myFileVersionInfo.IsSpecialBuild 
  44. "Language           : {0}"  -f $myFileVersionInfo.Language 
  45. "LegalCopyright     : {0}"  -f $myFileVersionInfo.LegalCopyright 
  46. "LegalTrademarks    : {0}"  -f $myFileVersionInfo.LegalTrademarks 
  47. "OriginalFilename   : {0}"  -f $myFileVersionInfo.OriginalFilename 
  48. "PrivateBuild       : {0}"  -f $myFileVersionInfo.PrivateBuild 
  49. "ProductBuildPart   : {0}"  -f $myFileVersionInfo.ProductBuildPart 
  50. "ProductMajordPart  : {0}"  -f $myFileVersionInfo.ProductMajorPart 
  51. "ProductMinorPart   : {0}"  -f $myFileVersionInfo.ProductMinorPart 
  52. "ProductName        : {0}"  -f $myFileVersionInfo.ProductName 
  53. "ProductPrivatePart : {0}"  -f $myFileVersionInfo.ProductMinorPart 
  54. "ProductVersion     : {0}"  -f $myFileVersionInfo.ProductVersion 
  55. "SpecialBuild       : {0}"  -f $myFileVersionInfo.SpecialBuild 

Sunday, 13 November 2011

ShowFileDescription.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the build description of file version object. 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the description of the file. 
  8. .NOTES 
  9.     File Name  : Show-FileDescription.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/2011/11/showfiledescriptionps1.html 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filedescription.aspx  
  17. .EXAMPLE 
  18.     Psh> .\Show-FileDescription.ps1 
  19.     File description for C:\Windows\system32\Notepad.exe is: Notepad 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Build details name 
  28. "File description for {0} is: {1}"  -f $file,$myFileVersionInfo.FileDescription 

Show-FileBuildPart.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the build number of file version object. 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the build identification of the file. 
  8. .NOTES 
  9.     File Name  : Show-FileBuildPart.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/2011/11/show-filebuildpartps1.html 
  15.     MSDN sample posted to: 
  16.          http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.filebuildpart.aspx   
  17. .EXAMPLE 
  18.     Psh> .\Show-FileBuildPart.ps1 
  19.     File build number for C:\Windows\system32\Notepad.exe is: 6001 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Build details name 
  28. "File build number for {0} is: {1}"  -f $file,$myFileVersionInfo.FileBuildPart 

Saturday, 12 November 2011

Show-NumberPadding3.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of double values. 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates a double value then formats it with 5 leading 
  7.     zeros. 
  8. .NOTES 
  9.     File Name  : Show-NumberPadding3.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/ 
  15.     MSDN sample posted to: 
  16.         http://msdn.microsoft.com/en-us/library/dd260048.aspx  
  17. .EXAMPLE 
  18.     Psh> Show-NumberPadding3.ps1 
  19.     01053240 
  20.     00103932.52 
  21.     01549230 
  22.  
  23.             01053240 
  24.          00103932.52 
  25.             01549230 
  26.        9034521202.93 
  27.  
  28. #>   
  29.  
  30. $fmt                 = "00000000.##" 
  31. [int]     $intValue  = 1053240 
  32. [decimal] $decValue  = 103932.52 
  33. [float]   $sngValue  = 1549230.10873992 
  34. [double]  $dblValue  = 9034521202.93217412 
  35.  
  36. # Display the numbers using the ToString method 
  37. $intValue.ToString($fmt
  38. $decValue.ToString($fmt
  39. $sngValue.ToString($fmt)            
  40. "" 
  41.  
  42. # Display the numbers using composite formatting 
  43. $formatString = " {0,15:" + $fmt + "}"  # right justified 
  44. $formatString -f $intValue  
  45. $formatString -f $decValue  
  46. $formatString -f $sngValue       
  47. $formatString -f $dblValue 

Show-NumberPadding1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Shows formatting of leading Zeros 
  4. .DESCRIPTION 
  5.     This script, a re-implementation of an MSDN Sample,  
  6.     creates several numbers of varying type, then 
  7.     displays them using .NET Formatting. The second set 
  8.     of formatting shows the difference between .ToString() 
  9.     and composite format strings to format - to approaches 
  10.     that accomplish the same goal! 
  11. .NOTES 
  12.     File Name  : Show-NumberPadding1.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://pshscripts.blogspot.com/2011/11/show-numberpadding1ps1.html 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library/dd260048.aspx 
  20. .EXAMPLE 
  21.     Psh> Show-NumberPadding1.ps1 
  22.                   00000254               000000FE 
  23.                   00010342               00002866 
  24.                   01023983               000F9FEF 
  25.                   06985321               006A9669 
  26.        9223372036854775807       7FFFFFFFFFFFFFFF 
  27.       18446744073709551615       FFFFFFFFFFFFFFFF 
  28.      
  29.                   00000254               000000FE 
  30.                   00010342               00002866 
  31.                   01023983               000F9FEF 
  32.        9223372036854775807       7FFFFFFFFFFFFFFF 
  33.       18446744073709551615       FFFFFFFFFFFFFFFF        
  34.  
  35. #> 
  36.  
  37. [byte]   $byteValue   = 254 
  38. [int16]  $shortValue  = 10342 
  39. [int]    $intValue    = 1023983 
  40. [long]   $lngValue    = 6985321 
  41. [long]   $lngValue2   = [System.Int64]::MaxValue 
  42. [UInt64] $ulngValue   = [System.UInt64]::MaxValue 
  43.  
  44. # Display integer values by caling the ToString method. 
  45. "{0,22} {1,22}" -f $byteValue.ToString("D8") , $byteValue.ToString("X8"
  46. "{0,22} {1,22}" -f $shortValue.ToString("D8"), $shortValue.ToString("X8"
  47. "{0,22} {1,22}" -f $intValue.ToString("D8")  , $intValue.ToString("X8"
  48. "{0,22} {1,22}" -f $lngValue.ToString("D8")  , $lngValue.ToString("X8"
  49. "{0,22} {1,22}" -f $lngValue2.ToString("D8") , $lngValue2.ToString("X8"
  50. "{0,22} {1,22}" -f $ulngValue.ToString("D8") , $ulngValue.ToString("X8"
  51. "" 
  52.  
  53. # Display the same integer values by using composite formatting 
  54. "{0,22:D8} {0,22:X8}" -f $byteValue 
  55. "{0,22:D8} {0,22:X8}" -f $shortValue 
  56. "{0,22:D8} {0,22:X8}" -f $intValue 
  57. "{0,22:D8} {0,22:X8}" -f $lngValue2 
  58. "{0,22:D8} {0,22:X8}" -f $ulngValue 

Thursday, 10 November 2011

Show-FileVersionCompanyName

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the company name of a file version object 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the company name (if it exists). 
  8. .NOTES 
  9.     File Name  : Show-FileVersionCompanyName.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/system.diagnostics.fileversioninfo.companyname.aspx    
  17. .EXAMPLE 
  18.     Psh> .\Show-FileVersionCompanyName.ps1 
  19.     Company name: Microsoft Corporation 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Company name 
  28. "Company name: " + $myFileVersionInfo.CompanyName 

Show-FileVersionComments.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the comments of a file version object 
  4. .DESCRIPTION 
  5.     This script is a re-implementation of an MSDN Sample script 
  6.     that uses System.Diagnostics.FileVersionInfo to get 
  7.     the company name (if it exists). 
  8. .NOTES 
  9.     File Name  : Show-FileVersionComments.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/system.diagnostics.fileversioninfo.comments.aspx    
  17. .EXAMPLE 
  18.     Psh> .\Show-FileVersionComments.ps1 
  19.     Comments : Microsoft Corporation 
  20. #> 
  21. # Set filename 
  22. $File = [System.Environment]::SystemDirectory + "\Notepad.exe" 
  23.  
  24. #Get Version information for this file 
  25. $myFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo($File
  26.  
  27. # Print the Comments field 
  28. "Commments : " + $myFileVersionInfo.CompanyName