Sunday, 28 June 2009

Get-TypeTest.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the value, and type, of an expression. 
  4. .DESCRIPTION 
  5.     This script is a rewrite of the second example on this page, The  
  6.     script illustrates how to use the GetType method to return 
  7.     the type that results from a calculation. in this case, two  
  8.     multiplying two int32 with the Pi field results in a System.Double. 
  9. .NOTES 
  10.     File Name  : Get-TypeTest.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.     This script posted to: 
  15.         http://pshscripts.blogspot.com/2009/06/get-typetestps1.html
  16.     MSDN Sample posted at: 
  17.         http://msdn.microsoft.com/en-us/library/58918ffs.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-TypeTest.PS1 
  20.     The type of $Radius is       : System.Int32 
  21.     Area =                         28.2743338823081 
  22.     The type is the expression is: System.Double 
  23. #> 
  24.  
  25. ## 
  26. # Start of Script 
  27. ## 
  28.   
  29. # Set and display type of radius 
  30. [int] $radius = 3 
  31. "The type of `$Radius is       : {0}" -f $radius.GetType() 
  32.  
  33. # Display Area, and the type of an expression. 
  34. "Area                           {0}" -f ($radius * $radius * [System.Math]::PI) 
  35. "The type is the expression is: {0}" -f ($radius * $radius * [System.Math]::PI).GetType() 
  36. # End of Script 

Wednesday, 17 June 2009

Get-ConformanceLevelEnum.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the confirmance level enum. 
  4. .DESCRIPTION 
  5.     This script displays the values form the conformancelevel enum 
  6.     and checks it against a value. 
  7. .NOTES 
  8.     File Name  : Get-ConformanceLevelEnum.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     This script posted to: 
  13.         http://pshscripts.blogspot.com 
  14.     MSDN Sample posted at: 
  15.         http://msdn.microsoft.com/en-us/library/h2344bs2(VS.85).aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-ConformanceLevelEnum.ps1' 
  18.     System.XML.ConformanceLevel enum has 3 possible values 
  19.     Value 0: Auto 
  20.     Value 1: Fragment 
  21.     Value 2: Document 
  22.      
  23.     $ToCheck1 is NOT document 
  24.     $ToCheck2 is Document 
  25. #> 
  26.  
  27. ## 
  28. # Start of script 
  29. ## 
  30. # Demonstrates the ConformanceLevel enum 
  31. # Thomas Lee - tfl@psp.co.uk 
  32. # Enumerate the enum 
  33. $enums=[enum]::GetValues([System.Xml.ConformanceLevel]) 
  34.  
  35. # Display 
  36. "System.XML.ConformanceLevel enum has {0} possible values" -f $enums.count 
  37. $i=0 
  38. $enums| %{"Value {0}: {1}" -f $i,$_.tostring();$i++} 
  39. "" 
  40. # Checking against a an enum value  
  41. $ToCheck1 = "somethingelse" 
  42. $Tocheck2 = "document" 
  43. if ($ToCheck1 -eq  [System.XML.ConformanceLevel]::Document) { 
  44.   "`$ToCheck1 is document"
  45. else
  46.   "`$ToCheck1 is NOT document" 
  47.    
  48. if ($ToCheck2 -eq  [System.XML.ConformanceLevel]::Document) { 
  49.  "`$ToCheck2 is Document"
  50. else
  51.  "`$ToCheck2 is NOT document" 
  52. }  
  53. #End of Script 

Monday, 15 June 2009

Get-TimeTick.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the static Tick values from TimeSpan Object 
  4. .DESCRIPTION 
  5.     This script 
  6. .NOTES 
  7.     File Name  : Get-TimeTick.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.timespan.tickspersecond(VS.85).aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-TimeTick.ps1 
  17.     Ticks Per: 
  18.     Day         : 864,000,000,000 
  19.     Hour        :  36,000,000,000 
  20.     Minute      :     600,000,000 
  21.     Second      :      10,000,000 
  22.     Millisecond :          10,000 
  23. #> 
  24.  
  25. ## 
  26. # Start of script 
  27. ## 
  28.   
  29. # Create the values 
  30. $tsticksperday         = [system.timespan]::ticksperday 
  31. $tsticksperhour        = [system.timespan]::ticksperhour 
  32. $tstickspermillisecond = [system.timespan]::TicksPerMillisecond 
  33. $tsticksperminute      = [system.timespan]::ticksperminute 
  34. $tstickspersecond =      [system.timespan]::tickspersecond 
  35.  
  36. # Display results nicely formatted 
  37. "Ticks Per:"  
  38. "Day         : {0, 15}" -f $tsticksperday.tostring("###,###"
  39. "Hour        : {0, 15}" -f $tsticksperhour.tostring("###,###"
  40. "Minute      : {0, 15}" -f $tsticksperminute.tostring("###,###"
  41. "Second      : {0, 15}" -f $tstickspersecond.tostring("###,###"
  42. "Millisecond : {0, 15}" -f $tstickspermillisecond.tostring("###,###"
  43. #End of Script 

Sunday, 14 June 2009

Get-JapaneseDate.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays dates using the Japanese calander. 
  4. .DESCRIPTION 
  5.     This script creates a new date and time object, then displays  
  6.     things using the Japanese calendar. I have changed the original 
  7.     script slightly to show the different eras in use.  
  8. .NOTES 
  9.     File Name  : Get-JapaneseDate.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://pshscripts.blogspot.com/2009/06/get-japanesedateps1.html
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.globalization.japanesecalendar.getyear.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Get-Japanesedate.ps1' 
  19.     April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar: 
  20.        Era:        1 
  21.        Year:       8 
  22.        Month:      4 
  23.        DayOfYear:  93 
  24.        DayOfMonth: 3 
  25.        DayOfWeek:  Saturday 
  26.   
  27.     After adding 138 years and 10 months: 
  28.        Era:        4 
  29.        Year:       26 
  30.        Month:      2 
  31.        DayOfYear:  34 
  32.        DayOfMonth: 3 
  33.        DayOfWeek:  Monday 
  34. #> 
  35. ## 
  36. # start of script 
  37. ## 
  38.  
  39. # Helper function 
  40. function DisplayJapaneseDateValue{ 
  41. Param ( $DT ) # 
  42. # create a Japenese calendar 
  43. $cal=new-object System.Globalization.JapaneseCalendar 
  44.   
  45.  # display dates using that calendar 
  46. "   Era:        {0}" -f $Cal.GetEra($DT)  
  47. "   Year:       {0}" -f $Cal.GetYear($DT)  
  48. "   Month:      {0}" -f $Cal.GetMonth($DT
  49. "   DayOfYear:  {0}" -f $Cal.GetDayOfYear($DT
  50. "   DayOfMonth: {0}" -f $Cal.GetDayOfMonth($DT
  51. "   DayOfWeek:  {0}" -f $Cal.GetDayOfWeek($DT)  
  52. "" 
  53.   
  54. # Set a DateTime to April 3, 1875 of the Gregorian calendar.  
  55. # This date is in the Meiji era (era 1) 
  56. $myDT = new-object System.DateTime 1875, 4, 3,(New-Object System.Globalization.GregorianCalendar) 
  57.   
  58. # Display the values of the DateTime 
  59. "April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar:"  
  60. DisplayJapaneseDateValue $myDT  
  61.   
  62. # Add 138 years and 10 months 
  63. # This takes the date into the Heisei era (era 4) 
  64. $myDT = $myCal.AddYears( $myDT, 138 ) 
  65. $myDT = $myCal.AddMonths($myDT, 10 ) 
  66.   
  67. # Displays the values of the DateTime. 
  68.  "After adding 138 years and 10 months:"  
  69. DisplayJapaneseDateValue $myDT  
  70. # end of script 

Sunday, 7 June 2009

Get-Cookie.ps1


  1. <# 
  2. .SYNOPSIS 
  3.     This script gets and displays the cookies returned from a site. 
  4. .DESCRIPTION 
  5.     This script calls System.Net.WebRequest to get a page. in the  
  6.     response there may be cookies returned which this script then 
  7.     displays. By default, the cookies returned from the MSDN home page 
  8.     are displayed. To improve production orientation, the creation of 
  9.     the request and getting the response are protected by try/catch 
  10.     blocks. 
  11. .NOTES 
  12.     File Name  : get-cookie.ps1 
  13.     Author     : Thomas Lee - tfl@psp.co.uk 
  14.     Requires   : PowerShell V2 CTP3 
  15.      
  16. .LINK 
  17.     This script posted to: 
  18.         http://www.pshscripts.blogspot.com 
  19.     MSDN Sample posted at: 
  20.         http://msdn.microsoft.com/en-us/library/system.net.cookie.aspx 
  21. .EXAMPLE 
  22.     PSH [C:\foo]: .\Get-Cookie.ps1 
  23.     3 Cookies returned from: http://www.microsoft.com/msdn 
  24.      
  25.     Cookie: 
  26.     A = I&I=AxUFAAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1 
  27.     Domain      : .microsoft.com 
  28.     Path        : / 
  29.     Port        : 
  30.     Secure      : False 
  31.     When issued : 6/7/2009 10:06:30 AM 
  32.     Expires     : 
  33.     Expired?    : False 
  34.     Don’t save  : False
  35.     Comment     :
  36.     URI for Comments:
  37.     Version     : 0
  38.     String      : A=I&I=AxUFAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1 :
  39.     Cookie:
  40.     ADS = SN=175A21EF
  41.     Domain      : .microsoft.com
  42.     Path        : /
  43.     Port        :
  44.     Secure      :
  45.     When issued : 6/7/2009 10:06:30 AM
  46.     Expires     :
  47.     Expired?    : false
  48.     Don’t save  : false  
  49.     Comment     : 
  50.     Uri for comments: 
  51.     Version     : 0 
  52.     String: ADS=SN=175A21EF : 
  53.      
  54.     Cookie: 
  55.     mediaRssUrl = http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml 
  56.     Domain      : msdn.microsoft.com 
  57.     Path        : / 
  58.     Port        : 
  59.     Secure      : False 
  60.     When issued : 6/7/2009 10:06:30 AM 
  61.     Expires     : 
  62.     Expired?    : False 
  63.     Don’t save  : False
  64.     Comment     :
  65.     Uri for comments :
  66.     Version     : 0
  67.     String      : mediaRssUrl=http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml :
  68. #>
  69. param (
  70. $site = “http://www.microsoft.com/msdn”
  71.    
  72. ## 
  73. # Start of script 
  74. ## 
  75.   
  76. # Create the web request object, catching any errors 
  77. try { 
  78. $request = [System.Net.WebRequest]::Create($site) 
  79. $request.CookieContainer = New-Object System.Net.CookieContainer 
  80. } 
  81. catch { 
  82. "Error creating request";$requst 
  83. return 
  84. } 
  85.   
  86. # Now get response 
  87. try { 
  88. $response = $request.GetResponse() 
  89. } 
  90. catch { 
  91. "Error getting response from $site - try later" 
  92. return 
  93. } 
  94.   
  95. # Print number of cookies 
  96. if ($response.Cookies.Count -gt 0) { 
  97. "{0} Cookies returned from: {1}" -f $Response.Cookies.Count,$site 
  98. "" 
  99. } 
  100.   
  101. # Print the properties of each cookie. 
  102. foreach ($cook in $Response.Cookies)    { 
  103. "Cookie:" 
  104. "{0} = {1}"    -f $cook.Name, $cook.Value 
  105. "Domain      : {0}"     -f $cook.Domain 
  106. "Path        : {0}"     -f $cook.Path 
  107. "Port        : {0}"     -f $cook.Port 
  108. "Secure      : {0}"     -f $cook.Secure 
  109. "When issued : {0}"     -f $cook.TimeStamp 
  110. "Expires     : {0}"     -f $cook.expireds 
  111. "Expired?    : {0}"     -f $cook.expired 
  112. "Don't save  : {0}"     -f $cook.Discard 
  113. "Comment     : {0}"     -f $cook.Comment 
  114. "Uri for comments: {0}" -f $cook.CommentUri 
  115. "Version     : {0}"     -f $cook.Version 
  116. "String: {0} :"         -f $cook.ToString() 
  117. "" 
  118. # End of Script 

Saturday, 6 June 2009

Get-ProcessPerfCounter.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a process, then displays some performance counters. 
  4. .DESCRIPTION 
  5.     This script calls System.Diagnostics.Process's Start static 
  6.     method to create a process.  Then it displays perf stats till the
  7.     process is stopped. Then it prints final stats out.
  8. .NOTES 
  9.     File Name  : Get-ProcessPerfCounter 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. <# 
  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.process.peakworkingset64.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Get-ProcessPerfCounter.ps1 
  20.  
  21.     System.Diagnostics.Process (notepad) - 
  22.     ------------------------------------- 
  23.       physical memory usage     : 2,512 
  24.       base priority             : 8 
  25.       priority class            : Normal 
  26.       user processor time       : 00:00:00.0156001 
  27.       privileged processor time : 00:00:00.0156001 
  28.       total processor time      : 00:00:00.0312002 
  29.   
  30.     ** Loads more - snipped for brevity ** 
  31.      
  32.     System.Diagnostics.Process (notepad) - 
  33.     ------------------------------------- 
  34.       physical memory usage     : 12,424 
  35.       base priority             : 8 
  36.       priority class            : Normal 
  37.       user processor time       : 00:00:00.0156001 
  38.       privileged processor time : 00:00:00.0624004 
  39.       total processor time      : 00:00:00.0780005 
  40.   
  41.       Process has ended 
  42.       Process exit code: 0 
  43.       Peak physical memory usage of the process :  12,424 kb 
  44.       Peak paged memory usage of the process    :   2,740 kb 
  45.       Peak virtual memory usage of the process  :  88,832 kb 
  46. #> 
  47.   
  48. ## 
  49. # Start of script 
  50. ## 
  51.    
  52. # Start up Notepad, catching issues 
  53. try  { 
  54. $myproc =  [System.Diagnostics.Process]::Start("c:\windows\notepad.exe"
  55. catch { 
  56.   "Error starting process" 
  57.   return 
  58.   
  59. # Now print perf stats until Notepad.exe is closed 
  60. do { 
  61.   if ( ! $myproc.HasExited ) { 
  62.   $myproc.Refresh() 
  63.   "" 
  64.   "{0} -"        -f $myProc.ToString() 
  65.   "-------------------------------------" 
  66.   "  physical memory usage     : {0}" -f $($MyProc.WorkingSet64/1kb).tostring("###,###"
  67.   "  base priority             : {0}" -f $MyProc.BasePriority 
  68.   "  priority class            : {0}" -f $MyProc.PriorityClass 
  69.   "  user processor time       : {0}" -f $MyProc.UserProcessorTime 
  70.   "  privileged processor time : {0}" -f $MyProc.PrivilegedProcessorTime 
  71.   "  total processor time      : {0}" -f $MyProc.TotalProcessorTime 
  72.     
  73. # calculate overall peak 
  74. $peakPagedMem   = $MyProc.PeakPagedMemorySize64 
  75. $peakVirtualMem = $MyProc.PeakVirtualMemorySize64 
  76. $peakWorkingSet = $MyProc.PeakWorkingSet64 
  77.   
  78. } # end of if 
  79.      
  80. } # end of do 
  81. while (!$myproc.WaitForExit(1000)) # Wait a second and do it again 
  82.  
  83. # Here process has exited 
  84. # Print out final results 
  85. "" 
  86. "Process has ended" 
  87. "Process exit code: {0}" -f $MyProc.ExitCode 
  88.  
  89. # Display peak memory statistics for the process. 
  90. "Peak physical memory usage of the process : {0,7} kb" -f $($peakWorkingSet/1kb).ToString("###,###"
  91. "Peak paged memory usage of the process    : {0,7} kb" -f $($peakPagedMem/1kb).ToString("###,###"
  92. "Peak virtual memory usage of the process  : {0,7} kb" -f $($peakVirtualMem/1kb).ToString("###,###"
  93. # End of script