Monday, 30 April 2012

Show-DnsConfiguration.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script shows the DNS Configuration  of NICs  
  4.     in your system 
  5. .DESCRIPTION 
  6.     This script is a re-write of an MSDN Sample  
  7.     using PowerShell./ The script gets all network 
  8.     active network interfaces then prints out that 
  9.     interfaces' DNS Properties. 
  10. .NOTES 
  11.     File Name  : Show-DnsConfiguration.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17.     MSDN sample posted to: 
  18.          http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.getallnetworkinterfaces.aspx    
  19. .EXAMPLE 
  20.     Psh[C:\foo]> .\Show-DnsConfiguration.ps1 
  21.     Broadcom NetXtreme 57xx Gigabit Controller 
  22.       DNS suffix .............................. : cookham.net 
  23.       DNS enabled ............................. : False 
  24.       Dynamically configured DNS .............. : True 
  25.  
  26.     ... more interfaces snipped for brevity!     
  27. #> 
  28.  
  29. # Get the adapters than iterate over the collection and display DNS configuration 
  30. $adapters = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  31. ForEach ($adapter in $adapters)   { 
  32.   $properties = $adapter.GetIPProperties() 
  33.   $adapter.Description 
  34.   "  DNS suffix .............................. : {0}" -f $properties.DnsSuffix 
  35.   "  DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled 
  36.   "  Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled 

Show-NetworkInterfaces2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script, shows details about the Network Interfaces 
  4.     on a system. This is a re-write of an MSDN script into 
  5.     PowerShell 
  6. .DESCRIPTION 
  7.     This script Uses several .NET classes to get the details 
  8.     of the interfaces on the system, then displays these details. 
  9.     Note in the MSDN Script, there were calls to other MSDN Samples.  
  10.     To make things simple, I have removed these calls.  
  11. .NOTES 
  12.     File Name  : Show-NetworkInterfaces2.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell Version 2.0 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN sample posted to: 
  19.          http://msdn.microsoft.com/en-us/library/system.net.networkinformation.networkinterface.aspx    
  20. .EXAMPLE 
  21.  
  22.     Psh[C:\foo]> .\Show-NetworkInterfaces2.ps1 
  23.     Interface information for Cookham8.cookham.net 
  24.       Number of interfaces .................... : 2 
  25.  
  26.     Broadcom NetXtreme 57xx Gigabit Controller 
  27.     ========================================== 
  28.       Interface type .......................... : Ethernet 
  29.       Physical Address ........................ : 001E4F955CC4 
  30.       Operational status ...................... : Up 
  31.       IP version .............................. : IPv4, IPv6 
  32.       DNS suffix .............................. : cookham.net 
  33.       MTU...................................... : 1500 
  34.       WINS Servers ............................ : 
  35.       10.10.1.101 
  36.       DNS enabled ............................. : False 
  37.       Dynamically configured DNS .............. : True 
  38.       Receive Only ............................ : False 
  39.       Multicast ............................... : True 
  40.  
  41.     Microsoft ISATAP Adapter 
  42.     ======================== 
  43.       Interface type .......................... : Tunnel 
  44.       Physical Address ........................ : 00000000000000E0 
  45.       Operational status ...................... : Down 
  46.       IP version .............................. : IPv4, IPv6 
  47.       DNS suffix .............................. : cookham.net 
  48.       MTU...................................... :  
  49.       DNS enabled ............................. : False 
  50.       Dynamically configured DNS .............. : True 
  51.       Receive Only ............................ : False 
  52.       Multicast ............................... : False 
  53.        
  54. #> 
  55.  
  56. # First, get network properties of, and the nics in, this system 
  57. $computerProperties = [System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties() 
  58. $nics =               [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  59.  
  60. "Interface information for {0}.{1}" -f $computerProperties.HostName, $computerProperties.DomainName 
  61.  
  62. # Do we have nics?? 
  63. If (!$nics -or $nics.Length -lt 1) 
  64.     { 
  65.         "  No network interfaces found." 
  66.         return 
  67.     } 
  68.  
  69. # Show interface details 
  70. "  Number of interfaces .................... : {0}" -f $nics.Length 
  71. ForEach ($adapter in $nics
  72.     { 
  73.         $properties = $adapter.GetIPProperties() 
  74.         "" 
  75.         $adapter.Description 
  76.         "=" * $adapter.Description.Length 
  77.         "  Interface type .......................... : {0}" -f $adapter.NetworkInterfaceType 
  78.         "  Physical Address ........................ : {0}" -f $adapter.GetPhysicalAddress().ToString() 
  79.         "  Operational status ...................... : {0}" -f $adapter.OperationalStatus 
  80.     # Create a display string for the supported IP versions. 
  81.        $versions ="" 
  82.         if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPV4 )) 
  83.         { 
  84.              $versions = "IPv4" 
  85.          } 
  86.         if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPv6)) 
  87.         { 
  88.             if ($versions.Length -gt 0) 
  89.             { 
  90.                $versions += ", "
  91.              } 
  92.             $versions += "IPv6"
  93.         } 
  94.         "  IP version .............................. : {0}" -f $versions 
  95.  
  96. # The remaining information is not useful for loopback adapters. 
  97.  if ($adapter.NetworkInterfaceType -eq [System.Net.NetworkInformation.NetworkInterfaceType]::Loopback) 
  98.         { 
  99.             continue 
  100.         } 
  101.  "  DNS suffix .............................. : {0}" -f $properties.DnsSuffix 
  102.         if ($adapter.Supports([System.Net.NetworkInformation.NetworkInterfaceComponent]::IPv4)) 
  103.         { 
  104.             $ipv4 = $properties.GetIPv4Properties() 
  105.             "  MTU...................................... : {0}" -f $ipv4.Mtu 
  106.             if ($ipv4.UsesWins) 
  107.             { 
  108.  
  109.                 $winsServers = $properties.WinsServersAddresses 
  110.                 if ($winsServers.Count -gt 0) 
  111.                 { 
  112.                     "  WINS Servers ............................ :"
  113.                     foreach ($Winserver in $winsServers) { 
  114.                     "  {0}" -f $Winserver.IpAddressToString 
  115.                     } 
  116.                 } 
  117.             } 
  118.         } 
  119.  
  120.         "  DNS enabled ............................. : {0}" -f $properties.IsDnsEnabled 
  121.         "  Dynamically configured DNS .............. : {0}" -f $properties.IsDynamicDnsEnabled 
  122.         "  Receive Only ............................ : {0}" -f $adapter.IsReceiveOnly 
  123.         "  Multicast ............................... : {0}" -f $adapter.SupportsMulticast 
  124. # End Foreach 

Sunday, 29 April 2012

Show-NetworkInterfaces1.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the NICs in a system and their physical
  4.     address. 
  5. .DESCRIPTION 
  6.     This script is a MSDN Sample recoded in PowerShell. The script
  7.     first gets all the interfaces on the system, then loops through
  8.     them displaying more information about them to the console.
  9.     Note the use of Console.Write in the loop at the end. Not quite
  10.     sure a better PowerShell equivalent other than creating a
  11.     string with all the bytes, then displaying that string. 
  12. .NOTES 
  13.     File Name  : Show-NetworkInterfaces1.ps1 
  14.     Author     : Thomas Lee - tfl@psp.co.uk 
  15.     Requires   : PowerShell Version 2.0 
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.     MSDN sample posted to: 
  20.         http://msdn.microsoft.com/en-us/library/system.net.networkinformation.physicaladdress.aspx    
  21. .EXAMPLE 
  22.     PSH[c:\foo]> .\Show-Networkinterfaces1.ps1 
  23. Interface information for Cookham8.cookham.net      
  24.   Number of interfaces .................... : 2 
  25. Broadcom NetXtreme 57xx Gigabit Controller 
  26. ========================================== 
  27.   Interface type .......................... : Ethernet 
  28.   Physical address ........................ : 00-1E-4F-95-5C-C4  
  29.  
  30. Microsoft ISATAP Adapter 
  31. ======================== 
  32.   Interface type .......................... : Tunnel 
  33.   Physical address ........................ : 00-00-00-00-00-00-00-E0  
  34.      
  35. #> 
  36.      
  37. # Get computer IP global properties 
  38. $ComputerProperties = [System.Net.NetworkInformation.IpGlobalProperties]::GetIPGlobalProperties() 
  39.  
  40. # Get the nics in this system 
  41. $nics = [System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces() 
  42.  
  43. # Show information header 
  44. "Interface information for {0}.{1}     " -f $ComputerProperties.HostName, $ComputerProperties.DomainName 
  45.   
  46. # Iterate through the NIcs ans outout per nic info 
  47. # First, check if interfaces are found 
  48.  if (!$nics -OR $nics.Length -LT 1) 
  49.     { 
  50.         "  No network interfaces found." 
  51.         return 
  52.     } 
  53.  
  54. # Here print out number of interfaces and interface details 
  55. [System.Console]::WriteLine("  Number of interfaces .................... : {0}" -f $nics.Length) 
  56. Foreach ($adapter in $nics
  57.     { 
  58.         $properties = $adapter.GetIPProperties();    
  59.         " ";"" 
  60.         "{0}" -F $adapter.Description 
  61.         "=" * $adapter.Description.Length 
  62.         "  Interface type .......................... : {0}" -F $adapter.NetworkInterfaceType 
  63.         [System.Console]::Write("  Physical address ........................ : "
  64.         $address = $adapter.GetPhysicalAddress(); 
  65.         $bytes = $address.GetAddressBytes() 
  66.         for($i = 0; $i -lt $bytes.Length; $i++) 
  67.         { 
  68.             # Display the physical address in hexadecimal. 
  69.             [system.Console]::Write("{0}" -f $bytes[$i].ToString("X2")) 
  70.             # Insert a hyphen after each byte, unless we are at the end of the  
  71.             # address. 
  72.             if ($i -NE $bytes.Length -1) 
  73.             { 
  74.                  [System.Console]::Write("-"
  75.             } 
  76.         } 
  77.         [System.Console]::WriteLine() 
  78.     } 

Sunday, 19 February 2012

Get-ProcessesAsExcel.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates an Excel workbook using PowerShell and  
  4.     populates it with the results of calling Get-Process and  
  5.     copying across the key properties 
  6. .DESCRIPTION 
  7.     This script demonstrates manipulating Excel with PowerShell 
  8.     and the Excel.Application COM object. 
  9. .NOTES 
  10.     File Name  : .\Get-ProcessesAsExcel.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell Version 2.0 
  13. .LINK 
  14.     This script posted to:
  15. http://pshscripts.blogspot.com/2012/02/get-processesasexcelps1.html
  16. .EXAMPLE 
  17.     Left as an exercise to the reader. 
  18. #> 
  19. ##  
  20. # Start of Script 
  21. ## 
  22.  
  23. # First, create and single worksheet workbook 
  24.  
  25. # Create Excel object 
  26. $excel = new-object -comobject Excel.Application 
  27.     
  28. # Make Excel visible 
  29. $excel.visible = $true 
  30.    
  31. # Create a new workbook 
  32. $workbook = $excel.workbooks.add() 
  33.  
  34. # The default workbook has three sheets, remove 2,4 
  35. $S2 = $workbook.sheets | where {$_.name -eq "Sheet2"
  36. $s3 = $workbook.sheets | where {$_.name -eq "Sheet3"
  37. $s2.delete() 
  38. $s3.delete() 
  39.   
  40. # Get sheet and update sheet name 
  41. $s1 = $workbook.sheets | where {$_.name -eq 'Sheet1'
  42. $s1.name = "Processes" 
  43.    
  44. # Update workook properties 
  45. $workbook.author  = "Thomas Lee - Doctordns@gmail.com" 
  46. $workbook.title   = "Processes running on $(hostname)" 
  47. $workbook.subject = "Demonstrating the Power of PowerShell with Excel" 
  48.    
  49. # Next update Headers 
  50. $s1.range("A1:A1").cells="Handles" 
  51. $s1.range("B1:B1").cells="NPM(k)" 
  52. $s1.range("C1:C1").cells="PM(k)" 
  53. $s1.range("D1:D1").cells="WS(k)" 
  54. $s1.range("E1:E1").cells="VM(M)" 
  55. $s1.range("F1:F1").cells="CPU" 
  56. $s1.range("G1:G1").cells="ID" 
  57. $s1.range("H1:H1").cells="Process Name" 
  58.  
  59. $row = 2 
  60. Foreach ($Process in $(Get-Process)) { 
  61.  $s1.range("A$Row:A$Row").cells=$Process.handles 
  62.  $s1.range("b$Row:B$Row").cells=$Process.NPM 
  63.  $s1.range("c$Row:C$Row").cells=$Process.PM 
  64.  $s1.range("d$Row:D$Row").cells=$Process.WS 
  65.  $s1.range("e$Row:E$Row").cells=$Process.VM 
  66.  $s1.range("f$Row:F$Row").cells=$Process.CPU 
  67.  $s1.range("g$Row:G$Row").cells=$Process.ID 
  68.  $s1.range("h$Row:H$Row").cells=$Process.Name 
  69.  $row++ 
  70.  
  71. # And save it away: 
  72. $s1.saveas("c:\foo\process.xlsx"
  73. # end of script 

Tuesday, 7 February 2012

Test-FileOpen

  1. <# 
  2. .SYNOPSIS 
  3.     This script defines a function that tests to 
  4.     see if a file is open. 
  5. .DESCRIPTION 
  6.     This script used the System.Io.FileStream class  
  7.     and the FileInfo class to try to open a file 
  8.     stream for write. If it fails, we return $false
  9.     else we close the file and return $True 
  10. .NOTES 
  11.     File Name  : Test-FileOpen.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell Version 2.0 
  14. .LINK 
  15.     This script posted to: 
  16.         http://www.pshscripts.blogspot.com 
  17. .EXAMPLE 
  18.     Psh[Cookham8:C:\foo]> $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx 
  19.     Psh[Cookham8:C:\foo]>Test-FileOpen $file 
  20.     True 
  21. #> 
  22.  
  23. Function Test-FileOpen { 
  24.  
  25. Param ( 
  26. $fileName = $(Throw '***** No File specified')  
  27.  
  28. $ErrorActionPreference = "SilentlyContinue" 
  29. [System.IO.FileStream] $fs = $file.OpenWrite();  
  30. if (!$?) {$true
  31. else {$fs.Dispose();$false
  32.  
  33. # Test the function 
  34. $file = New-Object -TypeName System.IO.FileInfo C:\foo\doc1.docx 
  35. Test-FileOpen $file 

Sunday, 22 January 2012

New-SpanishCulture.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a Spanish cultureinfo object with a traditional 
  4.      sort and another with an international sort. The script then compares them. 
  5. .DESCRIPTION 
  6.     This script re-implements an MSDN sample.  
  7. .NOTES 
  8.     File Name  : New-SpanishCulture.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.globalization.cultureinfo.aspx    
  16. .EXAMPLE 
  17.     C:\foo> .\New-SpanishCulture.ps1 
  18.         PROPERTY                       INTERNATIONAL                                  TRADITIONAL               
  19.     CompareInfo                    CompareInfo - es-ES                            CompareInfo - es-ES_tradnl 
  20.     DisplayName                    Spanish (Spain)                                Spanish (Spain)           
  21.     EnglishName                    Spanish (Spain, International Sort)            Spanish (Spain, Traditional Sort) 
  22.     IsNeutralCulture               False                                          False                     
  23.     IsReadOnly                                                                    False                     
  24.     LCID                           3082                                           1034                      
  25.     Name                           es-ES                                          es-ES                     
  26.     NativeName                     Español (España, alfabetización internacional) Español (España, alfabetización tradicional) 
  27.     Parent                         es                                             es                        
  28.     TextInfo                       TextInfo - es-ES                               TextInfo - es-ES_tradnl   
  29.     ThreeLetterISOLanguageName     spa                                            spa                       
  30.     ThreeLetterWindowsLanguageName ESN                                            ESP                       
  31.     TwoLetterISOLanguageName       es                                             es                        
  32.  
  33.     Comparing [llegar] and [lugar] 
  34.        With myCIintl.CompareInfo.Compare: -1 
  35.        With myCItrad.CompareInfo.Compare: 1 
  36. #> 
  37.  
  38. # Create and initialize the CultureInfo which uses the international sort 
  39. $myCIintl = New-Object System.Globalization.CultureInfo "es-ES", $false 
  40.  
  41. # Create and initialize the CultureInfo which uses the traditional sort 
  42. $myCItrad = New-Object System.Globalization.CultureINfo 0x040A, $false 
  43.  
  44. # Display the properties of each culture. 
  45. "{0,-31}{1,-47}{2,-25}" -f "PROPERTY", "INTERNATIONAL", "TRADITIONAL" 
  46. "{0,-31}{1,-47}{2,-25}" -f "CompareInfo", $myCIintl.CompareInfo, $myCItrad.CompareInfo 
  47. "{0,-31}{1,-47}{2,-25}" -f "DisplayName", $myCIintl.DisplayName, $myCItrad.DisplayName 
  48. "{0,-31}{1,-47}{2,-25}" -f "EnglishName", $myCIintl.EnglishName, $myCItrad.EnglishName 
  49. "{0,-31}{1,-47}{2,-25}" -f "IsNeutralCulture", $myCIintl.IsNeutralCulture, $myCItrad.IsNeutralCulture 
  50. "{0,-31}{1,-47}{2,-25}" -f "IsReadOnly", $myCIintl.$IsReadOnly, $myCItrad.IsReadOnly 
  51. "{0,-31}{1,-47}{2,-25}" -f "LCID", $myCIintl.LCID, $myCItrad.LCID 
  52. "{0,-31}{1,-47}{2,-25}" -f "Name", $myCIintl.Name, $myCItrad.Name 
  53. "{0,-31}{1,-47}{2,-25}" -f "NativeName", $myCIintl.NativeName, $myCItrad.NativeName 
  54. "{0,-31}{1,-47}{2,-25}" -f "Parent", $myCIintl.Parent, $myCItrad.Parent 
  55. "{0,-31}{1,-47}{2,-25}" -f "TextInfo", $myCIintl.TextInfo, $myCItrad.TextInfo 
  56. "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterISOLanguageName", $myCIintl.ThreeLetterISOLanguageName, $myCItrad.ThreeLetterISOLanguageName 
  57. "{0,-31}{1,-47}{2,-25}" -f "ThreeLetterWindowsLanguageName",$myCIintl.ThreeLetterWindowsLanguageName, $myCItrad.ThreeLetterWindowsLanguageName 
  58. "{0,-31}{1,-47}{2,-25}" -f "TwoLetterISOLanguageName", $myCIintl.TwoLetterISOLanguageName, $myCItrad.TwoLetterISOLanguageName 
  59. "" 
  60.  
  61. # Compare two strings using myCIintl 
  62. "Comparing [llegar] and [lugar]" 
  63. "   With myCIintl.CompareInfo.Compare: {0}" -f $myCIintl.CompareInfo.Compare("llegar", "lugar"
  64. "   With myCItrad.CompareInfo.Compare: {0}" -f $myCItrad.CompareInfo.Compare("llegar", "lugar"

Show-ChineeseParentCulture.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the parent culture of each  
  4.     specific culture using the Chinese language. 
  5. .DESCRIPTION 
  6.     This script looks at each Chineese culture and displays 
  7.     the culture name and the parent.  
  8. .NOTES 
  9.     File Name  : Show-ChineeseParentCulture.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.globalization.cultureinfo.aspx    
  17. .EXAMPLE 
  18.     C:\foo> .\Show-ChineeseParentCulture.ps1 
  19.     SPECIFIC CULTURE                                     PARENT CULTURE 
  20.     0x0804 zh-CN Chinese (Simplified, PRC)               0x0004 zh-CHS Chinese (Simplified) Legacy 
  21.     0x0C04 zh-HK Chinese (Traditional, Hong Kong S.A.R.) 0x7C04 zh-CHT Chinese (Traditional) Legacy 
  22.     0x1404 zh-MO Chinese (Traditional, Macao S.A.R.)     0x7C04 zh-CHT Chinese (Traditional) Legacy 
  23.     0x1004 zh-SG Chinese (Simplified, Singapore)         0x0004 zh-CHS Chinese (Simplified) Legacy 
  24.     0x0404 zh-TW Chinese (Traditional, Taiwan)           0x7C04 zh-CHT Chinese (Traditional) Legacy     
  25. #> 
  26.  
  27. # Display a header 
  28. "SPECIFIC CULTURE                                     PARENT CULTURE" 
  29.  
  30. # Determine the specific cultures that use the Chinese language, and displays the parent culture 
  31.  
  32. ForEach ($ci in [System.Globalization.CultureInfo]::GetCultures([System.Globalization.CultureTypes]::SpecificCultures))  { 
  33.   if ($ci.TwoLetterISOLanguageName -eq "zh"
  34.    { 
  35.     $s1 = "0x{0} {1} {2,-40}" -f $ci.LCID.ToString("X4"), $ci.Name, $ci.EnglishName 
  36.     $s2 = "0x{0} {1} {2}" -f $ci.Parent.LCID.ToString("X4"), $ci.Parent.Name, $ci.Parent.EnglishName 
  37.     "{0}{1}" -f $s1, $s2 
  38.   } 

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