Monday, 27 September 2010

Show-HtmlCoding.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script encodes and decodes an HTML String 
  4. .DESCRIPTION 
  5.     This script used  
  6. .NOTES 
  7.     File Name  : Show-HtmlCoding.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/ee388364.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Show-HtmlCoding.ps1 
  17.     Original String: <this is a string123> & so is this one?? 
  18.     Encoded String : &lt;this is a string123&gt; &amp; so is this one?? 
  19.     Decoded String : <this is a string123> & so is this one?? 
  20.     Original string = Decoded string?: True    
  21. #> 
  22.  
  23. # Create string to encode/decode 
  24. $Str = "<this is a string123> & so is this one??" 
  25.  
  26. # Encode String 
  27. $Encstr = [System.Net.WebUtility]::HtmlEncode($str
  28.  
  29. # Decode String 
  30. $Decstr = [System.Net.WebUtility]::HtmlDecode($EncStr
  31.  
  32. # Display strings 
  33. "Original String: {0}" -f $Str 
  34. "Encoded String : {0}" -f $Encstr 
  35. "Decoded String : {0}" -f $Decstr 
  36. $eq = ($str -eq $Decstr
  37. "Original string = Decoded string?: {0}" -f $eq  

Sunday, 26 September 2010

Get-FTPDirectory.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display the root of an FTP site. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample. 
  6. .NOTESW 
  7.     File Name  : Get-FtpDirectory.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         htthttp://pshscripts.blogspot.com/2010/09/get-ftpdirectoryps1.html 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/ms229716.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpDirectory.ps1 
  17.     drwxrwxrwx   1 user     group           0 Dec  4  2005 pcpro 
  18.     drwxrwxrwx   1 user     group           0 Sep 23 15:18 PowerShell 
  19.     ... {Listing truncated} 
  20.     Download Complete, status: 
  21.     226-Maximum disk quota limited to 100000 Kbytes 
  22.         Used disk quota 78232 Kbytes, available 21767 Kbytes 
  23.     226 Transfer complete. 
  24. #> 
  25. # Get the object used to communicate with the server. 
  26. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net"
  27. $Request.Method =  [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails 
  28.  
  29. # This example assumes the FTP site uses anonymous logon. 
  30. # Username/password not real 
  31. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous",tfl@psp.co.uk 
  32.  
  33. $Response = $Request.GetResponse() 
  34. $ResponseStream = $Response.GetResponseStream() 
  35.  
  36. # Read and display the text in the file 
  37. $Reader = new-object System.Io.StreamReader $Responsestream 
  38. [System.Console]::Writeline($Reader.ReadToEnd()) 
  39.  
  40. # Display Status 
  41. "Download Complete, status:" 
  42. $response.StatusDescription  
  43.  
  44. # Close Reader and Response objects 
  45. $Reader.Close() 
  46. $Response.Close() 

Get-FtpFile.ps1

  1. # 
  2. .SYNOPSIS 
  3.     This script used FTP to get and display a text file. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN Sample 
  6. .NOTESW 
  7.     File Name  : Get-FtpFile.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/09/get-ftpfileps1.html 
  13.     MSDN sample posted to: 
  14.         http://msdn.microsoft.com/en-us/library/ms229711.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-FtpFile.ps1' 
  17.     This is Hello.Txt from www.reskit.net 
  18.     Have a great day! 
  19.   
  20.     Download Complete, status: 
  21.     226-Maximum disk quota limited to 100000 Kbytes 
  22.         Used disk quota 78232 Kbytes, available 21767 Kbytes 
  23.     226 Transfer complete.     
  24. #> 
  25.  
  26. # Get the object used to communicate with the server. 
  27. $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net/hello.txt"); 
  28. $Method =  [System.Net.WebRequestMethods+Ftp]::DownloadFile 
  29.  
  30. # This example assumes the FTP site uses anonymous logon. 
  31. # Username/password not real 
  32. $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous","tfl@psp.co.uk"
  33. $ResponseStream = $Response.GetResponseStream() 
  34.  
  35. # Read and display the text in the file 
  36. $Reader = new-object System.Io.StreamReader $ResponseStream 
  37. [System.Console]::Writeline($Reader.ReadToEnd()) 
  38.  
  39. # Display Status 
  40. "Download Complete, status:" 
  41. $response.StatusDescription  
  42.  
  43. # Close Reader and Response objects 
  44. $Reader.Close() 
  45. $Response.Close() 

Friday, 24 September 2010

New-Task.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates a scheduled task object. 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN sample using PowerShell 
  6. .NOTES 
  7.     File Name  : New-Task.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://www.pshscripts.blogspot.com 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/aa383665%28VS.85%29.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\New-Task.ps1
  17.     Time Now       : 9/24/2010 12:43:47 PM 
  18.     Task startTime : 2010-09-24T12:44:17 
  19.     Task endTime   : 2010-09-24T12:48:47 
  20.     Task definition created. About to submit the task... 
  21.   
  22.   
  23.     Name               : Test TimeTrigger 
  24.     Path               : Test TimeTrigger 
  25.     State              : 3 
  26.     Enabled            : True 
  27.     LastRunTime        : 12/30/1899 12:00:00 AM 
  28.     LastTaskResult     : 1 
  29.     NumberOfMissedRuns : 0 
  30.     NextRunTime        : 9/24/2010 12:44:17 PM 
  31.     Definition         : System.__ComObject 
  32.     Xml                : <?xml version="1.0" encoding="UTF-16"?> 
  33.                          <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task"
  34.                            <RegistrationInfo> 
  35.                              <Author>Thomas Lee</Author> 
  36.                              <Description>Start notepad at a certain time</Description> 
  37.                            </RegistrationInfo> 
  38.                            <Triggers> 
  39.                              <TimeTrigger id="TimeTriggerId"
  40.                                <StartBoundary>2010-09-24T12:44:17</StartBoundary> 
  41.                                <EndBoundary>2010-09-24T12:48:47</EndBoundary> 
  42.                                <ExecutionTimeLimit>PT5M</ExecutionTimeLimit> 
  43.                                <Enabled>true</Enabled> 
  44.                              </TimeTrigger> 
  45.                            </Triggers> 
  46.                            <Settings> 
  47.                              <IdleSettings> 
  48.                                <Duration>PT10M</Duration> 
  49.                                <WaitTimeout>PT1H</WaitTimeout> 
  50.                                <StopOnIdleEnd>true</StopOnIdleEnd> 
  51.                                <RestartOnIdle>false</RestartOnIdle> 
  52.                              </IdleSettings> 
  53.                              <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy> 
  54.                              <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries> 
  55.                              <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries> 
  56.                              <AllowHardTerminate>true</AllowHardTerminate> 
  57.                              <StartWhenAvailable>true</StartWhenAvailable> 
  58.                              <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable> 
  59.                              <AllowStartOnDemand>true</AllowStartOnDemand> 
  60.                              <Enabled>true</Enabled> 
  61.                              <Hidden>false</Hidden> 
  62.                              <RunOnlyIfIdle>false</RunOnlyIfIdle> 
  63.                              <WakeToRun>false</WakeToRun> 
  64.                              <ExecutionTimeLimit>PT72H</ExecutionTimeLimit> 
  65.                              <Priority>7</Priority> 
  66.                            </Settings> 
  67.                            <Actions Context="Author"
  68.                              <Exec> 
  69.                                <Command>C:\Windows\System32\notepad.exe</Command> 
  70.                              </Exec> 
  71.                            </Actions> 
  72.                            <Principals> 
  73.                              <Principal id="Author"
  74.                                <UserId>COOKHAM\tfl</UserId> 
  75.                                <LogonType>InteractiveToken</LogonType> 
  76.                              </Principal> 
  77.                            </Principals> 
  78.                          </Task> 
  79.  
  80.     Task submitted. 
  81. #> 
  82. # Helper Function 
  83. function XMLTIME{ 
  84. Param ( $T) 
  85. $csecond = $t.Second.ToString() 
  86. $cminute = $t.minute.ToString() 
  87. $chour = $t.hour.ToString() 
  88. $cday  = $t.day.ToString() 
  89. $cmonth  = $t.month.ToString() 
  90. $cyear = $t.year.ToString() 
  91.  
  92. $date =  $cyear + "-" 
  93. if ($cmonth.Length -eq 1) { $date += "0" + $cmonth + "-"}  
  94. else                      { $date += $cmonth + "-"
  95. if ($cday.length -eq 1)   { $date += "0" + $cday + "T"
  96. else                      { $date += $cday + "T"
  97. if ($chour.length -eq 1)  { $date += "0" + $chour + ":"
  98. else                      { $date += $chour + ":"
  99. if ($cminute.length -eq 1){ $date += "0" + $cminute + ":"
  100. else                      { $date += $cminute + ":"
  101. if ($csecond.length -eq 1){ $date += "0" + $csecond} 
  102. else                      { $date += $csecond} 
  103. # return 
  104. $date 
  105.  
  106. ## Script starts here 
  107.  
  108. # A constant that specifies a time-based trigger. 
  109. $TriggerTypeTime = 1 
  110. # A constant that specifies an executable action. 
  111. $ActionTypeExec = 0    
  112.  
  113. # Create and connect to the service 
  114. $service = New-Object -com schedule.service  
  115. $service.Connect() 
  116.  
  117. # Get a folder to create a task definition in.  
  118. $rootFolder = $service.GetFolder("\") 
  119.  
  120. # The taskDefinition variable is the TaskDefinition object. 
  121. # The flags parameter is 0 because it is not supported. 
  122. $taskDefinition = $service.NewTask(0)  
  123.  
  124. # Define information about the task. 
  125. # Set the registration info for the task by  
  126. # creating the RegistrationInfo object. 
  127. $regInfo = $taskDefinition.RegistrationInfo 
  128. $regInfo.Description = "Start notepad at a certain time" 
  129. $regInfo.Author = "Thomas Lee" 
  130.  
  131. # Set the principal for the task 
  132. $principal = $taskDefinition.Principal 
  133.  
  134. # Set the logon type to interactive logon 
  135. $principal.LogonType = 3 
  136.  
  137. # Set the task setting info for the Task Scheduler by 
  138. # creating a TaskSettings object. 
  139. $settings = $taskDefinition.Settings 
  140. $settings.Enabled = $True 
  141. $settings.StartWhenAvailable = $True 
  142. $settings.Hidden = $False 
  143.  
  144. # Create a time-based trigger. 
  145. $triggers = $taskDefinition.Triggers 
  146. $trigger = $triggers.Create($TriggerTypeTime) 
  147.  
  148. # Trigger variables that define when the trigger is active. 
  149. $time = ([system.datetime]::now).addseconds(30) 
  150. $startTime = XmlTime($time) 
  151.  
  152. $time = ([system.datetime]::now).addminutes(5) 
  153. $endTime = XmlTime($time) 
  154.  
  155. "Time Now       : {0}" -f (Get-Date -display time) 
  156. "Task startTime : {0}" -f $startTime 
  157. "Task endTime   : {0}" -f $endTime 
  158.  
  159. $trigger.StartBoundary = $startTime 
  160. $trigger.EndBoundary = $endTime 
  161. $trigger.ExecutionTimeLimit = "PT5M"    #Five minutes 
  162. $trigger.Id = "TimeTriggerId" 
  163. $trigger.Enabled = $True 
  164.  
  165. # Create the action for the task to execute. 
  166.  
  167. # Add an action to the task to run notepad.exe. 
  168. $Action = $taskDefinition.Actions.Create( $ActionTypeExec ) 
  169. $Action.Path = "C:\Windows\System32\notepad.exe" 
  170.  
  171. "Task definition created. About to submit the task..." 
  172.  
  173. # Register (create) the task. 
  174. $rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3) 
  175.  
  176. # all done! 
  177. "Task submitted." 

Monday, 20 September 2010

Get-VirtualMachine.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script gets the list of Virtual Machines on a Hyper-V Host 
  4. .DESCRIPTION 
  5.     This script uses WMI to get the VMs defined on a Hyper-V host 
  6.     then displays them. 
  7. .NOTES 
  8.     File Name  : Get-VirtualMachine.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 tot 
  15.         http://msdn.microsoft.com/en-us/library/cc136822%28VS.85%29.aspx 
  16. .EXAMPLE 
  17.     PSH [c:\foo\: Get-VirtualMachine  
  18.     5 Virtual Machines on: COOKHAM2 
  19.  
  20.     PSMC-DC1 
  21.     PSMC-EXCH1 
  22.     PSMC-SQL 
  23.     PSMC-SRV1 
  24.     PSMC-SRV2 
  25. #> 
  26.  
  27. # Get list of VMs from WMI 
  28. $vmbase = get-wmiobject Msvm_ComputerSystem -namespace root\virtualization -ComputerName Cookham2 
  29.  
  30. # Get hosting computer System Name 
  31.  
  32. $HostName = $vmbase | ? {$_.Caption -eq "Hosting Computer System"} | select name 
  33.  
  34. # Print results 
  35. "{0} Virtual Machines on: {1}" -f $($vmbase.count-1),$Hostname.name 
  36. $vmbase | where {$_.Caption -ne "Hosting Computer System"} | sort elementname | ft elementname -HideTableHeaders 
Technorati Tags: ,,,,

Sunday, 19 September 2010

Provision-LyncDnsRecords.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script creates the DNS entries needed for a Lync 2010 SE deployment. 
  4. .DESCRIPTION 
  5.     This script creates a couple of wrapper functions to create DNS A and SRV records, then 
  6.     calls those functions to create the needed A and SRV records for a simple 
  7.     Lync 2010 SE deployment. 
  8. .NOTES 
  9.     File Name  : Provision-LyncDnsRecords.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/2010/09/provision-lync-dnsrecordsps1.html 
  15.      
  16. #> 
  17. function Get-DnsARecord { 
  18. Get-WmiObject -ComputerName DC1 -Namespace 'root\MicrosoftDNS' -class MicrosoftDNS_AType  -filter "ContainerName ='reskit.org'" | ft ownername, ipaddress -AutoSize  
  19. Set-Alias gda get-DNSARecord 
  20.   
  21. function New-DnsARecord { 
  22. param
  23. $zone = "reskit.org"
  24. $name = "Testxxx.reskit.org"
  25. $address = "131.107.2.200",  
  26. $server = "dc1.reskit.org"
  27. $class = 1, 
  28. $ttl = 3600 
  29. # get class, and create the record 
  30. $rec = [WmiClass]"\\DC1\root\MicrosoftDNS:MicrosoftDNS_AType" 
  31. $result = $rec.CreateInstanceFromPropertydata($server, $zone, $name, $class, $ttl, $address
  32. }  
  33.    
  34. function Get-DnsSrvRecord { 
  35. Get-WmiObject -ComputerName DC1 -Namespace 'root\MicrosoftDNS' -class MicrosoftDNS_SrvType  -filter "ContainerName ='reskit.org'" |  
  36.    ft ownername,port,srvdomainname -AutoSize 
  37. Set-Alias gds get-DNSsrvRecord 
  38.  
  39. function New-DnsSrvRecord { 
  40. param
  41. $zone = "reskit.org"
  42. $name = "SRVFake.reskit.org"
  43. $target = "se.reskit.org"
  44. $port   = 9999, 
  45. $priority = 0, 
  46. $weight = 0, 
  47. $server = "dc1.reskit.org"
  48. $class = 1, 
  49. $ttl = 3600 
  50. $rec = [WmiClass]"\\DC1\root\MicrosoftDNS:MicrosoftDNS_SRVType" 
  51.  
  52. $result = $rec.CreateInstanceFromPropertydata($server, $zone, $name, $class, $ttl, $priority, $weight, $port, $target
  53.  
  54. # Create the needed records 
  55.  
  56. # The machines 
  57. # DC - Domain Controller, CA, IIS 
  58. New-DNSARecord reskit.org dc1.reskit.org        10.100.100.25 
  59. # SE - SE Server 
  60. New-DNSARecord reskit.org dc1.reskit.org        10.100.100.20 
  61. # Monitoring Server 
  62. New-DNSARecord reskit.org monitor.reskit.org    10.100.100.210 
  63. # Archive Server 
  64. New-DNSARecord reskit.org archive.reskit.org    10.100.100.220 
  65. # Reverse Proxy - Inside edge 
  66. New-DNSARecord reskit.org proxy.reskit.org      10.100.100.200 
  67. # Edge Consolidated - Inside edge 
  68. New-DNSARecord reskit.org edge.reskit.org       10.100.100.201 
  69.  
  70. # Services 
  71. # Dialin URL 
  72. New-DNSARecord reskit.org dialin.reskit.org     10.100.100.20 
  73. # Meeting URL 
  74. New-DNSARecord reskit.org meet.reskit.org       10.100.100.20 
  75. # Administration URL 
  76. New-DNSARecord reskit.org admin.reskit.org      10.100.100.20 
  77. # Web Services external URL 
  78. New-DNSARecord reskit.org external.reskit.org   10.100.100.200 
  79. New-DNSARecord reskit.org external1.reskit.org  10.100.100.200 
  80.  
  81. # Client Autodiscover SRV 
  82. New-DnsSrvRecord reskit.org _SipInternalTls._tcp.reskit.org se.reskit.org 5061  
  83.  
  84. # Display A records 
  85. gda  
  86.  
  87. # Display Srv Records 
  88. gds  

Saturday, 18 September 2010

Get-ColourRGB.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the Windows Color dialog box, then 
  4.     displays the r, g, b values of the colours selected  
  5. .DESCRIPTION 
  6.     This script calls windows.forms to show the color dialog. You then  
  7.     select a colour, and hit OK. The script then displays the values you selected. 
  8. .NOTES 
  9.     File Name  : Get-ColourRGB.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/2010/09/get-colourrgbps1.html 
  15. #> 
  16.  
  17. # Define worker function 
  18. function Get-ColourRGB { 
  19. param
  20. $R = 0,   # Red value 
  21. $G = 0,   # Green value 
  22. $B = 0    # Blue value 
  23. )   
  24.  
  25. # Show colour dialog, get colour  
  26. $colorDialog = New-Object Windows.Forms.ColorDialog -Property @{ 
  27.                FullOpen = $true 
  28.                Color = [Drawing.Color]::FromArgb($R,$G,$B
  29.                } 
  30.      
  31. if ($colorDialog.ShowDialog() -eq "OK") { 
  32.            $R = $colordialog.color.R 
  33.            $G = $colordialog.color.G 
  34.            $B = $colordialog.color.B 
  35.            $R;$G;$B  
  36.     }  
  37.  
  38. # Call the worker function 
  39. $R1,$G1,$B1 = Get-ColourRGB 128 128 128 
  40.  
  41. # Describe  
  42. " You set:" 
  43. " R to: {0}" -f $R1 
  44. " G to: {0}" -f $G1 
  45. " B to: {0}" -f $B1 

Friday, 17 September 2010

Get-ArrayList2.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates using an ArrayList 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN Sample using  
  6.     a System.Collection.ArrayList 
  7. .NOTES 
  8.     File Name  : Get-Arraylist2.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 tot: 
  15.         http://msdn.microsoft.com/en-us/library/system.collections.arraylist.insert.aspx 
  16. .EXAMPLE 
  17.     Left as an exercise for the reader! 
  18.      
  19. #> 
  20. $myAL = New-Object system.Collections.ArrayList 
  21. $myAL.Insert( 0, "The"
  22. $myAL.Insert( 1, "fox"
  23. $myAL.Insert( 2, "jumps"
  24. $myAL.Insert( 3, "over"
  25. $myAL.Insert( 4, "the"
  26. $myAL.Insert( 5, "dog"
  27.  
  28. # Create and initializes a new Queue. 
  29. $myQueue = New-Object system.Collections.Queue 
  30. $myQueue.Enqueue( "quick"
  31. $myQueue.Enqueue( "brown"
  32.  
  33. # Displays the ArrayList and the Queue. 
  34. "The ArrayList initially contains the following:" 
  35. $myAL 
  36. "The Queue initially contains the following:" 
  37. $myQueue 
  38.  
  39. # Copy the Queue elements to the ArrayList at index 1. 
  40. $myAL.InsertRange( 1, $myQueue
  41.  
  42. # Displays the ArrayList. 
  43.  "After adding the Queue, the ArrayList now contains:"  
  44.  $myAL 
  45.   
  46.  # Search for "dog" and add "lazy" before it. 
  47.  $myAL.Insert( $myAL.IndexOf( "dog" ), "lazy"
  48.  
  49. # Display the ArrayList. 
  50. "After adding `"lazy`", the ArrayList now contains:"  
  51. $myAL 
  52.  
  53. # Add "!!!" at the end. 
  54.  $myAL.Insert( $myAL.Count, "!!!"
  55.   
  56. # Display the ArrayList. 
  57. "After adding `"!!!`", the ArrayList now contains:" 
  58. $myAL 
  59.  
  60. # Inserting an element beyond Count throws an exception. 
  61. try  { 
  62.      $myAL.Insert( $myAL.Count+1, "anystring"
  63.       }  
  64. catch { 
  65. "Exception: " + $Error[0] 
  66.       } 

Thursday, 16 September 2010

Get-ArrayList.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script demonstrates the use of the Systems.Collections.Arraylist class 
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample script 
  6. .NOTES 
  7.     File Name  : get-arraylist.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/09/get-arraylistps1.html 
  13.     MSDN sample posted tot: 
  14.         http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-ArrayList.ps1 
  17.     0 
  18.     1 
  19.     2 
  20.     myAL 
  21.     Count       : 3 
  22.     Capacity    : 4 
  23.     Fixed Len?  : False 
  24.     Read only?  : False 
  25.     Values   : 
  26.     Hello 
  27.     World 
  28.     ! 
  29. #> 
  30.  
  31. # Creates and initializes a new ArrayList. 
  32. $myAL = New-Object System.Collections.ArrayList 
  33.  
  34. # Add three values 
  35. $myAL.Add("Hello"
  36. $myAL.Add("World"
  37. $myAL.Add("!"
  38.  
  39. # Display the properties and values of the ArrayList. 
  40. "myAL" 
  41. "Count       : {0}" -f $myAL.Count 
  42. "Capacity    : {0}" -f $myAL.Capacity 
  43. "Fixed Len?  : {0}" -f $myAL.IsFixedSize 
  44. "Read only?  : {0}" -f $myAL.IsReadOnly 
  45. "Values   :" 
  46. $myAL 

Thursday, 2 September 2010

Get-UmAlQuraCalendar.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays details of a UmAlQura Calendar in PowerShell 
  4. .DESCRIPTION 
  5.     This script shows the various aspects of this calendar including key properties, 
  6.     fields and selected methods. 
  7. .NOTES 
  8.     File Name  : Get-UmAlQuraCalendar.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 at: 
  15.         http://msdn.microsoft.com/en-us/library/system.globalization.umalquracalendar.aspx 
  16. .EXAMPLE 
  17.     PSH [C:\foo]: .\Get-UmAlQuraCalendar.ps1' 
  18.     Um Al Qura Calendar 
  19.     Algorithm Type          : LunarCalendar 
  20.     Eras in Calendar        : 1 
  21.     Is read only?           : False 
  22.     Max Supported Date/Time : 5/13/2029 11:59:59 PM 
  23.     Min Supported Date/Time : 4/30/1900 12:00:00 AM 
  24.     Two Digit Year Max      : 1451 
  25.   
  26.     April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar: 
  27.        Era:            1 
  28.        Year:           1423 
  29.        Is Leap Year?   False 
  30.        Days In Year:   354 
  31.        Month:          1 
  32.        Months in Year: 12 
  33.        Days in Month:  29 
  34.        Leap Month:     0 
  35.        DayOfYear:      20 
  36.        DayOfMonth:     20 
  37.        DayOfWeek:      Wednesday 
  38.   
  39.     After adding two years and ten months and one day: 
  40.        Era:            1 
  41.        Year:           1425 
  42.        Is Leap Year?   True 
  43.        Days In Year:   355 
  44.        Month:          11 
  45.        Months in Year: 12 
  46.        Days in Month:  30 
  47.        Leap Month:     0 
  48.        DayOfYear:      317 
  49.        DayOfMonth:     21 
  50.        DayOfWeek:      Sunday 
  51. #>     
  52.  
  53. # Helper Function 
  54. Function DisplayValues { 
  55. param ($MyCal, $MyDT
  56.  
  57. "   Era:            {0}" -f $MyCal.GetEra($MyDT)  
  58. "   Year:           {0}" -f $MyCal.GetYear($MyDT
  59. "   Is Leap Year?   {0}" -f $MyCal.IsLeapYear($MyCal.GetYear($MyDT)) 
  60. "   Days In Year:   {0}" -f $MyCal.GetDaysInYear($MyCal.GetYear($MyDT)) 
  61. "   Month:          {0}" -f $MyCal.GetMonth($MyDT
  62. "   Months in Year: {0}" -f $MyCal.GetMonthsInYear($MyCal.GetYear($MyDT)) 
  63. "   Days in Month:  {0}" -f $MyCal.GetDaysInMonth($MyCal.GetYear($MyDT), $MyDT.Month) 
  64. "   Leap Month:     {0}" -f $MyCal.GetLeapMonth($MyCal.GetYear($MyDT)) 
  65. "   DayOfYear:      {0}" -f $MyCal.GetDayOfYear($MyDT
  66. "   DayOfMonth:     {0}" -f $MyCal.GetDayOfMonth($MyDT
  67. "   DayOfWeek:      {0}" -f $MyCal.GetDayOfWeek($MyDT
  68. "" 
  69.        
  70. # Sets a DateTime to April 3, 2002 of the Gregorian calendar. 
  71.  $MyDT = New-Object System.DateTime 2002, 4, 3, (New-Object System.Globalization.GregorianCalendar) 
  72.    
  73. # Creates an instance of the UmAlQuraCalendar. 
  74. $MyCal = New-Object System.Globalization.UmAlQuraCalendar 
  75. # Display properties of the calendar 
  76. "Um Al Qura Calendar" 
  77. "Algorithm Type          : {0}" -f $MyCal.AlgorithmType 
  78. "Eras in Calendar        : {0}" -f $MyCal.Eras.count 
  79. "Is read only?           : {0}" -f $MyCal.IsReadOnly 
  80. "Max Supported Date/Time : {0}" -f $MyCal.MaxSupportedDateTime 
  81. "Min Supported Date/Time : {0}" -f $MyCal.MinSupportedDateTime 
  82. "Two Digit Year Max      : {0}" -f $MyCal.TwoDigitYearMax 
  83. "" 
  84.   
  85. # Display the values of the DateTime. 
  86.  "April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar:"  
  87. DisplayValues $MyCal $MyDT  
  88.  
  89. # Adds two years and ten months and one Day. 
  90. $MyDT = $MyCal.AddYears( $MyDT, 2 ) 
  91. $MyDT = $MyCal.AddMonths($MyDT, 10 ) 
  92. $MyDT = $MyCal.AddDays($MyDT, 1 ) 
  93.   
  94. # Display the values of the DateTime. 
  95. "After adding two years and ten months and one day:" 
  96. DisplayValues $MyCal $MyDT 

Wednesday, 1 September 2010

Get-PersianCalendar.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays details of a Persian calendar in PowerShell 
  4. .DESCRIPTION 
  5.     This script re-implements an MSDN C# sample. 
  6. .NOTES 
  7.     File Name  : Get-PersianCalendar.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell Version 2.0 
  10. .LINK 
  11.     This script posted to: 
  12.         http://pshscripts.blogspot.com/2010/09/get-persiancalendarps1.html
  13.     MSDN Sample posted at: 
  14.         http://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar.aspx  
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-PersianCalendar.ps1
  17.     ................. Today .......................... 
  18.     Today is: 
  19.        Wednesday, 9/1/2010 03:25:17 PM in the Gregorian calendar. 
  20.        Wednesday, 6/10/1389 15:25:17 in the Persian calendar. 
  21.     ............... Fields ............................ 
  22.     PersianEra = 1 
  23.     ............... Properties ........................ 
  24.     Eras: 
  25.      era = 1 
  26.     Gregorian Date Range Supported by the Persian Calendar: 
  27.        From 3/21/0622 12:00:00 AM 
  28.        To   12/31/9999 11:59:59 PM 
  29.     TwoDigitYearMax = 99 
  30.     ............ Selected Methods ...................... 
  31.     GetDayOfYear: day       = 165 
  32.     GetDaysInMonth: days    = 30 
  33.     GetDaysInYear: days     = 366 
  34.     GetLeapMonth:  month    = 0 
  35.     GetMonthsInYear: months = 12 
  36.     IsLeapDay:              = False 
  37.     IsLeapMonth:            = False 
  38.     IsLeapYear: 1370 is a leap year = True 
  39.     ToFourDigitYear: 
  40.       If TwoDigitYearMax = 99, ToFourDigitYear(99) = 99 
  41.       If TwoDigitYearMax = 2010, ToFourDigitYear(99) = 1999 
  42. #> 
  43. # Get today's date. 
  44. $Jc       = new-object system.Globalization.PersianCalendar 
  45. $ThisDate = [System.DateTime]::Now 
  46.  
  47. # Display the current date using the Gregorian and Persian calendars. 
  48. "Today is:" 
  49. "   {0:dddd}, {0} in the Gregorian calendar." -f $ThisDate 
  50. "   {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar." -f $jc.GetDayOfWeek($thisDate),  
  51.                         $jc.GetMonth($thisDate),  
  52.                         $jc.GetDayOfMonth($thisDate),  
  53.                         $jc.GetYear($thisDate),  
  54.                         $jc.GetHour($thisDate),  
  55.                         $jc.GetMinute($thisDate),  
  56.                         $jc.GetSecond($thisDate
  57.  
  58. # Fields 
  59. "............... Fields ............................" 
  60. "PersianEra = {0}" -f [System.Globalization.PersianCalendar]::PersianEra 
  61.  
  62. # Properties 
  63. "............... Properties ........................" 
  64. "Eras:" 
  65. foreach ($era in $jc.Eras){ 
  66.          " era = {0}" -f $era 
  67. "Gregorian Date Range Supported by the Persian Calendar:" 
  68. "   From {0:G}" -f $jc.MinSupportedDateTime 
  69. "   To   {0:G}" -f $jc.MaxSupportedDateTime 
  70. "TwoDigitYearMax = {0}" -f $jc.TwoDigitYearMax 
  71.  
  72. # Methods 
  73. "............ Selected Methods ......................" 
  74. "GetDayOfYear: day       = {0}"    -f $jc.GetDayOfYear($thisDate
  75. "GetDaysInMonth: days    = {0}" -f $jc.GetDaysInMonth($thisDate.Year, $thisDate.Month,  
  76.                                    [System.Globalization.PersianCalendar]::PersianEra) 
  77. "GetDaysInYear: days     = {0}" -f $jc.GetDaysInYear($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra) 
  78. "GetLeapMonth:  month    = {0}" -f $jc.GetLeapMonth($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra) 
  79. "GetMonthsInYear: months = {0}" -f $jc.GetMonthsInYear($thisDate.Year,[System.Globalization.PersianCalendar]::PersianEra) 
  80. "IsLeapDay:              = {0}" -f $jc.IsLeapDay($thisDate.Year, $thisDate.Month, $thisDate.Day,  
  81.                         [System.Globalization.PersianCalendar]::PersianEra) 
  82. "IsLeapMonth:            = {0}" -f $jc.IsLeapMonth($thisDate.Year, $thisDate.Month,  
  83.                         [System.Globalization.PersianCalendar]::PersianEra) 
  84. "IsLeapYear: 1370 is a leap year = {0}" -f $jc.IsLeapYear(1370, [System.Globalization.PersianCalendar]::PersianEra) 
  85.  
  86. # Get the 4-digit year for a year whose last two digits are 99. The 4-digit year  
  87. # depends on the current value of the TwoDigitYearMax property. 
  88. "ToFourDigitYear:" 
  89. "  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99) 
  90. $jc.TwoDigitYearMax = $thisDate.Year 
  91. "  If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99)