Monday 25 May 2009

Get-RegionInfo

  1. <# 
  2. .SYNOPSIS 
  3.     This script displaysthe property values for a globlisation region 
  4. .DESCRIPTION 
  5.     This script 
  6. .NOTES 
  7.     File Name  : Get-RegionInfo.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.globalization.regioninfo.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-RegionInfo.ps1' 
  17.     Name:                         US 
  18.     DisplayName:                  United States 
  19.     EnglishName:                  United States 
  20.     IsMetric:                     False 
  21.     ThreeLetterISORegionName:     USA 
  22.     ThreeLetterWindowsRegionName: USA 
  23.     TwoLetterISORegionName:       US 
  24.     CurrencySymbol:               $ 
  25.     ISOCurrencySymbol:            USD 
  26.  
  27.    The two RegionInfo instances are equal. 
  28.      
  29. #> 
  30.   
  31. ## 
  32. # start of script 
  33. ## 
  34.   
  35. # greate a region 
  36.  
  37. $myRI1 = New-Object System.Globalization.RegionInfo "US"  
  38. "   Name:                         {0}" -f $myRI1.Name 
  39. "   DisplayName:                  {0}" -f $myRI1.DisplayName 
  40. "   EnglishName:                  {0}" -f $myRI1.EnglishName 
  41. "   IsMetric:                     {0}" -f $myRI1.IsMetric 
  42. "   ThreeLetterISORegionName:     {0}" -f $myRI1.ThreeLetterISORegionName 
  43. "   ThreeLetterWindowsRegionName: {0}" -f $myRI1.ThreeLetterWindowsRegionName 
  44. "   TwoLetterISORegionName:       {0}" -f $myRI1.TwoLetterISORegionName  
  45. "   CurrencySymbol:               {0}" -f $myRI1.CurrencySymbol  
  46. "   ISOCurrencySymbol:            {0}" -f $myRI1.ISOCurrencySymbol  
  47. "" 
  48.  
  49. # Compare the RegionInfo above with another RegionInfo created using CultureInfo 
  50. $culinfo = New-Object system.Globalization.CultureInfo "en-us", $false 
  51. $myRI2   = New-Object System.Globalization.RegionInfo $culinfo.lcid 
  52.  if ( $myRI1.Equals( $myRI2 )) { 
  53.    "The two RegionInfo instances are equal." 
  54. else
  55.    "The two RegionInfo instances are NOT equal." 
  56.     

Saturday 23 May 2009

Get-FileVersionInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the file version information of a file 
  4. .DESCRIPTION 
  5.     This script calls System.Diagnostics.FileVersionInfo's  
  6.     GetVersion info method on the file. By default, the file version 
  7.     displayed/returned is that of %systemroot%\notepad.exe.  
  8. .NOTES 
  9.     File Name  : Get-FileVersionInfo.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12.     Note       : The file named passed to GetVersionInfo needs to  
  13.                  be fully qualified, not just local name. 
  14. .LINK 
  15.     This script posted to: 
  16.       http://www.pshscripts.blogspot.com 
  17.     MSDN Sample posted at: 
  18.       http://msdn.microsoft.com/en-us/library/system.diagnostics.fileversioninfo.aspx 
  19. .EXAMPLE 
  20.     PSH [C:\foo]: .\Get-FileVersionInfo.ps1 
  21.     File Description : Notepad 
  22.     File Version     : 6.0.6000.16386 (vista_rtm.061101-2205) 
  23. .EXAMPLE 
  24.     PSH [C:\Foo]: .\Get-FileVersionInfo.ps1' c:\windows\fveupdate.exe 
  25.     File Description : BitLocker Drive Encryption Servicing Utility 
  26.     File Version     : 6.0.6000.16386 (vista_rtm.061101-2205) 
  27. .PARAMETER filename 
  28.     The name of the file for which file version information is displayed. 
  29. #> 
  30.  
  31. param
  32. [string] $filename = $(join-path ${env:systemroot} "Notepad.Exe"
  33.  
  34. ## 
  35. # start of script 
  36. ## 
  37.  
  38. $info= [system.Diagnostics.FileVersionInfo]::GetVersionInfo($filename
  39. "File Description : {0}" -f  $info.filedescription 
  40. "File Version     : {0}" -f  $info.fileversion 
  41.   

Tuesday 19 May 2009

Get-XMLFileContents.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script reads a simple XML file and prints the results 
  4. .DESCRIPTION 
  5.     This script is a re-write of an XML C# MSDN Sample. 
  6. .NOTES 
  7.     File Name  : get-xmlfilecontents.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10.     Books3.xml =  
  11.       <book> 
  12.       <title>C# Introduction</title> 
  13.       <price>20</price> 
  14.       </book> 
  15. .LINK 
  16.     This script posted to: 
  17.         http://www.pshscripts.blogspot.com 
  18.     MSDN Sample posted at: 
  19.         http://msdn.microsoft.com/en-us/library/t9bfea29.aspx 
  20. .EXAMPLE 
  21.     PSH [C:\foo]: .\Get-XMLFileContents.PS1 
  22.     The content of the title element: C# Introduction 
  23.     The content of the price element: 20 
  24.   
  25. #> 
  26.   
  27. ## 
  28. # Start of Script 
  29. ## 
  30.   
  31. # Create XML Reader 
  32. $reader = [system.Xml.XmlReader]::Create("C:\foo\book3.xml"
  33.   
  34. # Parse the XML document.  ReadString is used to  
  35. # read the text content of the elements. 
  36. $result=$reader.Read() 
  37.   
  38. # Read/Display title 
  39. $reader.ReadStartElement("book"
  40. $reader.ReadStartElement("title")    
  41. "The content of the title element: {0}" -f $reader.ReadString() 
  42. $reader.ReadEndElement() 
  43.   
  44. # Read and display price 
  45. $reader.ReadStartElement("price"
  46. "The content of the price element: {0}" -f $reader.ReadString() 
  47. $reader.ReadEndElement() 
  48. $reader.ReadEndElement() 
  49. # End of Script 

Wednesday 6 May 2009

Get-ElapsedTime.ps1

  1. <#  
  2. .SYNOPSIS  
  3.     Demonstrates use of the System.Diagnostics.Stopwatch .NET Class    
  4. .DESCRIPTION  
  5.     This script is a community content MSDN sample,using PowerShell  
  6. .NOTES  
  7.     File Name  : Get-ElapsedTime 
  8.     Author     : Thomas Lee - tfl@psp.co.uk  
  9.     Requires   : PowerShell V2 CTP3  
  10. .LINK  
  11.     Sample posted to:  
  12.     http://pshscripts.blogspot.com  
  13.     Original MSDN sample at:  
  14.     http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.aspx 
  15. .EXAMPLE  
  16.     PSH [C:\foo]: .\get-stopwatch.ps1' 
  17.     Runtime = 00:00:10.03 
  18.  
  19. #>  
  20.  
  21. ## 
  22. # start of script 
  23. ## 
  24.  
  25. # Create a new stopwatch objecty 
  26. $StopWatch = New-Object system.Diagnostics.Stopwatch 
  27.   
  28. # Start the stop watch 
  29. $stopWatch.Start() 
  30.  
  31. # Go to sleep for approx 10 seconds 
  32. [system.threading.Thread]::Sleep(10000) 
  33.  
  34. # Now after sleep, stop the watch 
  35. $StopWatch.Stop(); 
  36.  
  37. # Get the elapsed time as a TimeSpan value. 
  38. $ts = $StopWatch.Elapsed 
  39.  
  40. # Format and display the TimeSpan value. 
  41. $ElapsedTime = [system.String]::Format("{0:00}:{1:00}:{2:00}.{3:00}"
  42.            $ts.Hours, $ts.Minutes, $ts.Seconds, 
  43.             $ts.Milliseconds / 10); 
  44. "Runtime = $elapsedTime" 

Tuesday 5 May 2009

Clear-Stopwatch.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the use of the Reset method in a StopWatch object. 
  4. .DESCRIPTION 
  5.     This script calls the StartNew static method to create and start a new  
  6.     stopwatch object, and displays the current values for the stop watch. The  
  7.     stopwatch is then reset using the Reset method, then the current values are displayed.  At completion, 
  8.     the stopwatch is stopped and all counters are zero. 
  9. .NOTES 
  10.     File Name  : Clear-Stopwatch.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     This script posted to: 
  15.         http://www.pshscripts.blogspot.com 
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch.reset.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Clear-Stopwatch.ps1 
  20.     StopWatch object before reset: 
  21.     IsRunning           : True 
  22.     Elapsed             : 00:00:00.0039085 
  23.     ElapsedMilliseconds : 3 
  24.     ElapsedTicks        : 57153 
  25.     StopWatch object after reset: 
  26.     IsRunning           : False 
  27.     Elapsed             : 00:00:00 
  28.     ElapsedMilliseconds : 0 
  29.     ElapsedTicks        : 0 
  30. #> 
  31.    
  32. ## 
  33. #  Begin script 
  34. ## 
  35. # Create a stop watch object and auto start it 
  36. $sw = [system.Diagnostics.Stopwatch]::StartNew() 
  37.    
  38. # Now get current elapsed time: 
  39. "StopWatch object before reset:" 
  40. $sw | fl * 
  41.    
  42. # Now reset 
  43. $sw.reset() 
  44.    
  45. # Observe Results 
  46. "StopWatch object after reset:" 
  47. $sw | fl * 
  48. # End script 

Monday 4 May 2009

Get-LegalInfo.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets LegalCopyright and Legal Trademarks from a file, if they exist. 
  4. .DESCRIPTION 
  5.     This script uses System.Diagnostic.FileVersionInfo to get the version information 
  6.     of a file. in this sample, I use notepad.exe, as shipped with Windows. 
  7.    
  8.     If you look at the output, you see LegalTrademarks are empty - this is the result I get on my 
  9.     main workstation.  
  10. .NOTES 
  11.     File Name  : Get-LegalInfo.ps1 
  12.     Author     : Thomas Lee - tfl@psp.co.uk 
  13.     Requires   : PowerShell V2 CTP3 
  14. .LINK 
  15.     http://www.pshscripts.blogspot.com 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-LegalInfo.ps1 
  18.     LegalCopyright  :  c Microsoft Corporation. All rights reserved. 
  19.     Legal Trademarks: 
  20. #> 
  21. ## 
  22. # start of script 
  23. ## 
  24.   
  25. # Get the file version for the notepad. 
  26. $MyFileVersionInfo = [System.Diagnostics.FileVersionInfo]::GetVersionInfo("C:\windows\Notepad.exe"
  27.   
  28. # Now output legal Information 
  29. "LegalCopyright  :  {0}" -f $MyFileVersionInfo.LegalCopyright 
  30. "Legal Trademarks:  {0}" -f $myFileVersionInfo.LegalTrademarks 
  31. # End of Script 

Saturday 2 May 2009

Get-StopWatchProperties.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets and displays properties of a stopwatch 
  4. .DESCRIPTION 
  5.     This script, written as an MSDN Sample, creates and starts a stop watch. The 
  6.     script stops the stopwatch, and displays the properties. To some degree, the  
  7.     results indicate how long it takes to start and stop a stopwatch. Also, the 
  8.     runtimes vary if you run it multiple times.  
  9. .NOTES 
  10.     File Name  : Get-StopWatchProperties.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK
  14.     Original sample posted at: http://pshscripts.blogspot.com/2009/05/get-stopwatchpropertiesps1.html
  15.     MSDN Sample posted at: http://msdn.microsoft.com/en-us/library/system.diagnostics.stopwatch_properties.aspx  
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-StopWatchProperties.PS1' 
  18.     IsRunning           : False 
  19.     Elapsed             : 00:00:00.0000162 
  20.     ElapsedMilliseconds : 0 
  21.     ElapsedTicks        : 233 
  22.   
  23. #> 
  24.   
  25. ## 
  26. # Start of script 
  27. # 
  28.    
  29. # Create and start a stopwatch 
  30.    
  31. $StopWatch = New-Object System.Diagnostics.Stopwatch 
  32.   
  33. # Now start, then stop, the stopwatch 
  34. $StopWatch.start() 
  35. $StopWatch.stop() 
  36.    
  37. # Display results 
  38.    
  39. $stopwatch | Format-List
  40. # End of Script