- <#
- .SYNOPSIS
- This script encodes and decodes an HTML String
- .DESCRIPTION
- This script used
- .NOTES
- File Name : Show-HtmlCoding.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/ee388364.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Show-HtmlCoding.ps1
- Original String: <this is a string123> & so is this one??
- Encoded String : <this is a string123> & so is this one??
- Decoded String : <this is a string123> & so is this one??
- Original string = Decoded string?: True
- #>
- # Create string to encode/decode
- $Str = "<this is a string123> & so is this one??"
- # Encode String
- $Encstr = [System.Net.WebUtility]::HtmlEncode($str)
- # Decode String
- $Decstr = [System.Net.WebUtility]::HtmlDecode($EncStr)
- # Display strings
- "Original String: {0}" -f $Str
- "Encoded String : {0}" -f $Encstr
- "Decoded String : {0}" -f $Decstr
- $eq = ($str -eq $Decstr)
- "Original string = Decoded string?: {0}" -f $eq
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Monday, 27 September 2010
Show-HtmlCoding.ps1
Sunday, 26 September 2010
Get-FTPDirectory.ps1
- <#
- .SYNOPSIS
- This script used FTP to get and display the root of an FTP site.
- .DESCRIPTION
- This script re-implements an MSDN sample.
- .NOTESW
- File Name : Get-FtpDirectory.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- htthttp://pshscripts.blogspot.com/2010/09/get-ftpdirectoryps1.html
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/ms229716.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-FtpDirectory.ps1
- drwxrwxrwx 1 user group 0 Dec 4 2005 pcpro
- drwxrwxrwx 1 user group 0 Sep 23 15:18 PowerShell
- ... {Listing truncated}
- Download Complete, status:
- 226-Maximum disk quota limited to 100000 Kbytes
- Used disk quota 78232 Kbytes, available 21767 Kbytes
- 226 Transfer complete.
- #>
- # Get the object used to communicate with the server.
- $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net")
- $Request.Method = [System.Net.WebRequestMethods+Ftp]::ListDirectoryDetails
- # This example assumes the FTP site uses anonymous logon.
- # Username/password not real
- $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous",tfl@psp.co.uk
- $Response = $Request.GetResponse()
- $ResponseStream = $Response.GetResponseStream()
- # Read and display the text in the file
- $Reader = new-object System.Io.StreamReader $Responsestream
- [System.Console]::Writeline($Reader.ReadToEnd())
- # Display Status
- "Download Complete, status:"
- $response.StatusDescription
- # Close Reader and Response objects
- $Reader.Close()
- $Response.Close()
Labels:
powershell,
PowerShell scripts,
scripts,
System.Net.WebRequest
Get-FtpFile.ps1
- #
- .SYNOPSIS
- This script used FTP to get and display a text file.
- .DESCRIPTION
- This script re-implements an MSDN Sample
- .NOTESW
- File Name : Get-FtpFile.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/09/get-ftpfileps1.html
- MSDN sample posted to:
- http://msdn.microsoft.com/en-us/library/ms229711.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-FtpFile.ps1'
- This is Hello.Txt from www.reskit.net
- Have a great day!
- Download Complete, status:
- 226-Maximum disk quota limited to 100000 Kbytes
- Used disk quota 78232 Kbytes, available 21767 Kbytes
- 226 Transfer complete.
- #>
- # Get the object used to communicate with the server.
- $Request = [System.Net.WebRequest]::Create("ftp://www.reskit.net/hello.txt");
- $Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
- # This example assumes the FTP site uses anonymous logon.
- # Username/password not real
- $Request.Credentials = New-Object System.Net.NetworkCredential "Anonymous","tfl@psp.co.uk"
- $ResponseStream = $Response.GetResponseStream()
- # Read and display the text in the file
- $Reader = new-object System.Io.StreamReader $ResponseStream
- [System.Console]::Writeline($Reader.ReadToEnd())
- # Display Status
- "Download Complete, status:"
- $response.StatusDescription
- # Close Reader and Response objects
- $Reader.Close()
- $Response.Close()
Friday, 24 September 2010
New-Task.ps1
- <#
- .SYNOPSIS
- This script creates a scheduled task object.
- .DESCRIPTION
- This script re-implements an MSDN sample using PowerShell
- .NOTES
- File Name : New-Task.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/aa383665%28VS.85%29.aspx
- .EXAMPLE
- PSH [C:\foo]: .\New-Task.ps1
- Time Now : 9/24/2010 12:43:47 PM
- Task startTime : 2010-09-24T12:44:17
- Task endTime : 2010-09-24T12:48:47
- Task definition created. About to submit the task...
- Name : Test TimeTrigger
- Path : Test TimeTrigger
- State : 3
- Enabled : True
- LastRunTime : 12/30/1899 12:00:00 AM
- LastTaskResult : 1
- NumberOfMissedRuns : 0
- NextRunTime : 9/24/2010 12:44:17 PM
- Definition : System.__ComObject
- Xml : <?xml version="1.0" encoding="UTF-16"?>
- <Task version="1.2" xmlns="http://schemas.microsoft.com/windows/2004/02/mit/task">
- <RegistrationInfo>
- <Author>Thomas Lee</Author>
- <Description>Start notepad at a certain time</Description>
- </RegistrationInfo>
- <Triggers>
- <TimeTrigger id="TimeTriggerId">
- <StartBoundary>2010-09-24T12:44:17</StartBoundary>
- <EndBoundary>2010-09-24T12:48:47</EndBoundary>
- <ExecutionTimeLimit>PT5M</ExecutionTimeLimit>
- <Enabled>true</Enabled>
- </TimeTrigger>
- </Triggers>
- <Settings>
- <IdleSettings>
- <Duration>PT10M</Duration>
- <WaitTimeout>PT1H</WaitTimeout>
- <StopOnIdleEnd>true</StopOnIdleEnd>
- <RestartOnIdle>false</RestartOnIdle>
- </IdleSettings>
- <MultipleInstancesPolicy>IgnoreNew</MultipleInstancesPolicy>
- <DisallowStartIfOnBatteries>true</DisallowStartIfOnBatteries>
- <StopIfGoingOnBatteries>true</StopIfGoingOnBatteries>
- <AllowHardTerminate>true</AllowHardTerminate>
- <StartWhenAvailable>true</StartWhenAvailable>
- <RunOnlyIfNetworkAvailable>false</RunOnlyIfNetworkAvailable>
- <AllowStartOnDemand>true</AllowStartOnDemand>
- <Enabled>true</Enabled>
- <Hidden>false</Hidden>
- <RunOnlyIfIdle>false</RunOnlyIfIdle>
- <WakeToRun>false</WakeToRun>
- <ExecutionTimeLimit>PT72H</ExecutionTimeLimit>
- <Priority>7</Priority>
- </Settings>
- <Actions Context="Author">
- <Exec>
- <Command>C:\Windows\System32\notepad.exe</Command>
- </Exec>
- </Actions>
- <Principals>
- <Principal id="Author">
- <UserId>COOKHAM\tfl</UserId>
- <LogonType>InteractiveToken</LogonType>
- </Principal>
- </Principals>
- </Task>
- Task submitted.
- #>
- # Helper Function
- function XMLTIME{
- Param ( $T)
- $csecond = $t.Second.ToString()
- $cminute = $t.minute.ToString()
- $chour = $t.hour.ToString()
- $cday = $t.day.ToString()
- $cmonth = $t.month.ToString()
- $cyear = $t.year.ToString()
- $date = $cyear + "-"
- if ($cmonth.Length -eq 1) { $date += "0" + $cmonth + "-"}
- else { $date += $cmonth + "-"}
- if ($cday.length -eq 1) { $date += "0" + $cday + "T"}
- else { $date += $cday + "T"}
- if ($chour.length -eq 1) { $date += "0" + $chour + ":"}
- else { $date += $chour + ":"}
- if ($cminute.length -eq 1){ $date += "0" + $cminute + ":"}
- else { $date += $cminute + ":"}
- if ($csecond.length -eq 1){ $date += "0" + $csecond}
- else { $date += $csecond}
- # return
- $date
- }
- ## Script starts here
- # A constant that specifies a time-based trigger.
- $TriggerTypeTime = 1
- # A constant that specifies an executable action.
- $ActionTypeExec = 0
- # Create and connect to the service
- $service = New-Object -com schedule.service
- $service.Connect()
- # Get a folder to create a task definition in.
- $rootFolder = $service.GetFolder("\")
- # The taskDefinition variable is the TaskDefinition object.
- # The flags parameter is 0 because it is not supported.
- $taskDefinition = $service.NewTask(0)
- # Define information about the task.
- # Set the registration info for the task by
- # creating the RegistrationInfo object.
- $regInfo = $taskDefinition.RegistrationInfo
- $regInfo.Description = "Start notepad at a certain time"
- $regInfo.Author = "Thomas Lee"
- # Set the principal for the task
- $principal = $taskDefinition.Principal
- # Set the logon type to interactive logon
- $principal.LogonType = 3
- # Set the task setting info for the Task Scheduler by
- # creating a TaskSettings object.
- $settings = $taskDefinition.Settings
- $settings.Enabled = $True
- $settings.StartWhenAvailable = $True
- $settings.Hidden = $False
- # Create a time-based trigger.
- $triggers = $taskDefinition.Triggers
- $trigger = $triggers.Create($TriggerTypeTime)
- # Trigger variables that define when the trigger is active.
- $time = ([system.datetime]::now).addseconds(30)
- $startTime = XmlTime($time)
- $time = ([system.datetime]::now).addminutes(5)
- $endTime = XmlTime($time)
- "Time Now : {0}" -f (Get-Date -display time)
- "Task startTime : {0}" -f $startTime
- "Task endTime : {0}" -f $endTime
- $trigger.StartBoundary = $startTime
- $trigger.EndBoundary = $endTime
- $trigger.ExecutionTimeLimit = "PT5M" #Five minutes
- $trigger.Id = "TimeTriggerId"
- $trigger.Enabled = $True
- # Create the action for the task to execute.
- # Add an action to the task to run notepad.exe.
- $Action = $taskDefinition.Actions.Create( $ActionTypeExec )
- $Action.Path = "C:\Windows\System32\notepad.exe"
- "Task definition created. About to submit the task..."
- # Register (create) the task.
- $rootFolder.RegisterTaskDefinition("Test TimeTrigger", $taskDefinition, 6,"" ,"" , 3)
- # all done!
- "Task submitted."
Labels:
COM,
powershell,
PowerShell V2,
Schedule.Service
Monday, 20 September 2010
Get-VirtualMachine.ps1
- <#
- .SYNOPSIS
- This script gets the list of Virtual Machines on a Hyper-V Host
- .DESCRIPTION
- This script uses WMI to get the VMs defined on a Hyper-V host
- then displays them.
- .NOTES
- File Name : Get-VirtualMachine.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot
- http://msdn.microsoft.com/en-us/library/cc136822%28VS.85%29.aspx
- .EXAMPLE
- PSH [c:\foo\: Get-VirtualMachine
- 5 Virtual Machines on: COOKHAM2
- PSMC-DC1
- PSMC-EXCH1
- PSMC-SQL
- PSMC-SRV1
- PSMC-SRV2
- #>
- # Get list of VMs from WMI
- $vmbase = get-wmiobject Msvm_ComputerSystem -namespace root\virtualization -ComputerName Cookham2
- # Get hosting computer System Name
- $HostName = $vmbase | ? {$_.Caption -eq "Hosting Computer System"} | select name
- # Print results
- "{0} Virtual Machines on: {1}" -f $($vmbase.count-1),$Hostname.name
- $vmbase | where {$_.Caption -ne "Hosting Computer System"} | sort elementname | ft elementname -HideTableHeaders
Sunday, 19 September 2010
Provision-LyncDnsRecords.ps1
- <#
- .SYNOPSIS
- This script creates the DNS entries needed for a Lync 2010 SE deployment.
- .DESCRIPTION
- This script creates a couple of wrapper functions to create DNS A and SRV records, then
- calls those functions to create the needed A and SRV records for a simple
- Lync 2010 SE deployment.
- .NOTES
- File Name : Provision-LyncDnsRecords.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/09/provision-lync-dnsrecordsps1.html
- #>
- function Get-DnsARecord {
- Get-WmiObject -ComputerName DC1 -Namespace 'root\MicrosoftDNS' -class MicrosoftDNS_AType -filter "ContainerName ='reskit.org'" | ft ownername, ipaddress -AutoSize
- }
- Set-Alias gda get-DNSARecord
- function New-DnsARecord {
- param (
- $zone = "reskit.org",
- $name = "Testxxx.reskit.org",
- $address = "131.107.2.200",
- $server = "dc1.reskit.org",
- $class = 1,
- $ttl = 3600
- )
- # get class, and create the record
- $rec = [WmiClass]"\\DC1\root\MicrosoftDNS:MicrosoftDNS_AType"
- $result = $rec.CreateInstanceFromPropertydata($server, $zone, $name, $class, $ttl, $address)
- }
- function Get-DnsSrvRecord {
- Get-WmiObject -ComputerName DC1 -Namespace 'root\MicrosoftDNS' -class MicrosoftDNS_SrvType -filter "ContainerName ='reskit.org'" |
- ft ownername,port,srvdomainname -AutoSize
- }
- Set-Alias gds get-DNSsrvRecord
- function New-DnsSrvRecord {
- param (
- $zone = "reskit.org",
- $name = "SRVFake.reskit.org",
- $target = "se.reskit.org",
- $port = 9999,
- $priority = 0,
- $weight = 0,
- $server = "dc1.reskit.org",
- $class = 1,
- $ttl = 3600
- )
- $rec = [WmiClass]"\\DC1\root\MicrosoftDNS:MicrosoftDNS_SRVType"
- $result = $rec.CreateInstanceFromPropertydata($server, $zone, $name, $class, $ttl, $priority, $weight, $port, $target)
- }
- # Create the needed records
- # The machines
- # DC - Domain Controller, CA, IIS
- New-DNSARecord reskit.org dc1.reskit.org 10.100.100.25
- # SE - SE Server
- New-DNSARecord reskit.org dc1.reskit.org 10.100.100.20
- # Monitoring Server
- New-DNSARecord reskit.org monitor.reskit.org 10.100.100.210
- # Archive Server
- New-DNSARecord reskit.org archive.reskit.org 10.100.100.220
- # Reverse Proxy - Inside edge
- New-DNSARecord reskit.org proxy.reskit.org 10.100.100.200
- # Edge Consolidated - Inside edge
- New-DNSARecord reskit.org edge.reskit.org 10.100.100.201
- # Services
- # Dialin URL
- New-DNSARecord reskit.org dialin.reskit.org 10.100.100.20
- # Meeting URL
- New-DNSARecord reskit.org meet.reskit.org 10.100.100.20
- # Administration URL
- New-DNSARecord reskit.org admin.reskit.org 10.100.100.20
- # Web Services external URL
- New-DNSARecord reskit.org external.reskit.org 10.100.100.200
- New-DNSARecord reskit.org external1.reskit.org 10.100.100.200
- # Client Autodiscover SRV
- New-DnsSrvRecord reskit.org _SipInternalTls._tcp.reskit.org se.reskit.org 5061
- # Display A records
- gda
- # Display Srv Records
- gds
Labels:
Lync Server 2010,
powershell,
PowerShell scripts
Saturday, 18 September 2010
Get-ColourRGB.ps1
- <#
- .SYNOPSIS
- This script displays the Windows Color dialog box, then
- displays the r, g, b values of the colours selected
- .DESCRIPTION
- This script calls windows.forms to show the color dialog. You then
- select a colour, and hit OK. The script then displays the values you selected.
- .NOTES
- File Name : Get-ColourRGB.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/09/get-colourrgbps1.html
- #>
- # Define worker function
- function Get-ColourRGB {
- param(
- $R = 0, # Red value
- $G = 0, # Green value
- $B = 0 # Blue value
- )
- # Show colour dialog, get colour
- $colorDialog = New-Object Windows.Forms.ColorDialog -Property @{
- FullOpen = $true
- Color = [Drawing.Color]::FromArgb($R,$G,$B)
- }
- if ($colorDialog.ShowDialog() -eq "OK") {
- $R = $colordialog.color.R
- $G = $colordialog.color.G
- $B = $colordialog.color.B
- $R;$G;$B
- }
- }
- # Call the worker function
- $R1,$G1,$B1 = Get-ColourRGB 128 128 128
- # Describe
- " You set:"
- " R to: {0}" -f $R1
- " G to: {0}" -f $G1
- " B to: {0}" -f $B1
Labels:
ColorDialog,
powershell,
PowerShell scripts,
Windows.Forms
Friday, 17 September 2010
Get-ArrayList2.ps1
- <#
- .SYNOPSIS
- This script demonstrates using an ArrayList
- .DESCRIPTION
- This script re-implements an MSDN Sample using
- a System.Collection.ArrayList
- .NOTES
- File Name : Get-Arraylist2.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/system.collections.arraylist.insert.aspx
- .EXAMPLE
- Left as an exercise for the reader!
- #>
- $myAL = New-Object system.Collections.ArrayList
- $myAL.Insert( 0, "The" )
- $myAL.Insert( 1, "fox" )
- $myAL.Insert( 2, "jumps" )
- $myAL.Insert( 3, "over" )
- $myAL.Insert( 4, "the" )
- $myAL.Insert( 5, "dog" )
- # Create and initializes a new Queue.
- $myQueue = New-Object system.Collections.Queue
- $myQueue.Enqueue( "quick" )
- $myQueue.Enqueue( "brown" )
- # Displays the ArrayList and the Queue.
- "The ArrayList initially contains the following:"
- $myAL
- "The Queue initially contains the following:"
- $myQueue
- # Copy the Queue elements to the ArrayList at index 1.
- $myAL.InsertRange( 1, $myQueue )
- # Displays the ArrayList.
- "After adding the Queue, the ArrayList now contains:"
- $myAL
- # Search for "dog" and add "lazy" before it.
- $myAL.Insert( $myAL.IndexOf( "dog" ), "lazy" )
- # Display the ArrayList.
- "After adding `"lazy`", the ArrayList now contains:"
- $myAL
- # Add "!!!" at the end.
- $myAL.Insert( $myAL.Count, "!!!" )
- # Display the ArrayList.
- "After adding `"!!!`", the ArrayList now contains:"
- $myAL
- # Inserting an element beyond Count throws an exception.
- try {
- $myAL.Insert( $myAL.Count+1, "anystring" )
- }
- catch {
- "Exception: " + $Error[0]
- }
Thursday, 16 September 2010
Get-ArrayList.ps1
- <#
- .SYNOPSIS
- This script demonstrates the use of the Systems.Collections.Arraylist class
- .DESCRIPTION
- This script is a re-write of an MSDN sample script
- .NOTES
- File Name : get-arraylist.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/09/get-arraylistps1.html
- MSDN sample posted tot:
- http://msdn.microsoft.com/en-us/library/system.collections.arraylist.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ArrayList.ps1
- 0
- 1
- 2
- myAL
- Count : 3
- Capacity : 4
- Fixed Len? : False
- Read only? : False
- Values :
- Hello
- World
- !
- #>
- # Creates and initializes a new ArrayList.
- $myAL = New-Object System.Collections.ArrayList
- # Add three values
- $myAL.Add("Hello")
- $myAL.Add("World")
- $myAL.Add("!")
- # Display the properties and values of the ArrayList.
- "myAL"
- "Count : {0}" -f $myAL.Count
- "Capacity : {0}" -f $myAL.Capacity
- "Fixed Len? : {0}" -f $myAL.IsFixedSize
- "Read only? : {0}" -f $myAL.IsReadOnly
- "Values :"
- $myAL
Thursday, 2 September 2010
Get-UmAlQuraCalendar.ps1
- <#
- .SYNOPSIS
- This script displays details of a UmAlQura Calendar in PowerShell
- .DESCRIPTION
- This script shows the various aspects of this calendar including key properties,
- fields and selected methods.
- .NOTES
- File Name : Get-UmAlQuraCalendar.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.globalization.umalquracalendar.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-UmAlQuraCalendar.ps1'
- Um Al Qura Calendar
- Algorithm Type : LunarCalendar
- Eras in Calendar : 1
- Is read only? : False
- Max Supported Date/Time : 5/13/2029 11:59:59 PM
- Min Supported Date/Time : 4/30/1900 12:00:00 AM
- Two Digit Year Max : 1451
- April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar:
- Era: 1
- Year: 1423
- Is Leap Year? False
- Days In Year: 354
- Month: 1
- Months in Year: 12
- Days in Month: 29
- Leap Month: 0
- DayOfYear: 20
- DayOfMonth: 20
- DayOfWeek: Wednesday
- After adding two years and ten months and one day:
- Era: 1
- Year: 1425
- Is Leap Year? True
- Days In Year: 355
- Month: 11
- Months in Year: 12
- Days in Month: 30
- Leap Month: 0
- DayOfYear: 317
- DayOfMonth: 21
- DayOfWeek: Sunday
- #>
- # Helper Function
- Function DisplayValues {
- param ($MyCal, $MyDT )
- " Era: {0}" -f $MyCal.GetEra($MyDT)
- " Year: {0}" -f $MyCal.GetYear($MyDT)
- " Is Leap Year? {0}" -f $MyCal.IsLeapYear($MyCal.GetYear($MyDT))
- " Days In Year: {0}" -f $MyCal.GetDaysInYear($MyCal.GetYear($MyDT))
- " Month: {0}" -f $MyCal.GetMonth($MyDT)
- " Months in Year: {0}" -f $MyCal.GetMonthsInYear($MyCal.GetYear($MyDT))
- " Days in Month: {0}" -f $MyCal.GetDaysInMonth($MyCal.GetYear($MyDT), $MyDT.Month)
- " Leap Month: {0}" -f $MyCal.GetLeapMonth($MyCal.GetYear($MyDT))
- " DayOfYear: {0}" -f $MyCal.GetDayOfYear($MyDT)
- " DayOfMonth: {0}" -f $MyCal.GetDayOfMonth($MyDT)
- " DayOfWeek: {0}" -f $MyCal.GetDayOfWeek($MyDT)
- ""
- }
- # Sets a DateTime to April 3, 2002 of the Gregorian calendar.
- $MyDT = New-Object System.DateTime 2002, 4, 3, (New-Object System.Globalization.GregorianCalendar)
- # Creates an instance of the UmAlQuraCalendar.
- $MyCal = New-Object System.Globalization.UmAlQuraCalendar
- # Display properties of the calendar
- "Um Al Qura Calendar"
- "Algorithm Type : {0}" -f $MyCal.AlgorithmType
- "Eras in Calendar : {0}" -f $MyCal.Eras.count
- "Is read only? : {0}" -f $MyCal.IsReadOnly
- "Max Supported Date/Time : {0}" -f $MyCal.MaxSupportedDateTime
- "Min Supported Date/Time : {0}" -f $MyCal.MinSupportedDateTime
- "Two Digit Year Max : {0}" -f $MyCal.TwoDigitYearMax
- ""
- # Display the values of the DateTime.
- "April 3, 2002 of the Gregorian calendar equals the following in the UmAlQura calendar:"
- DisplayValues $MyCal $MyDT
- # Adds two years and ten months and one Day.
- $MyDT = $MyCal.AddYears( $MyDT, 2 )
- $MyDT = $MyCal.AddMonths($MyDT, 10 )
- $MyDT = $MyCal.AddDays($MyDT, 1 )
- # Display the values of the DateTime.
- "After adding two years and ten months and one day:"
- DisplayValues $MyCal $MyDT
Wednesday, 1 September 2010
Get-PersianCalendar.ps1
- <#
- .SYNOPSIS
- This script displays details of a Persian calendar in PowerShell
- .DESCRIPTION
- This script re-implements an MSDN C# sample.
- .NOTES
- File Name : Get-PersianCalendar.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell Version 2.0
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2010/09/get-persiancalendarps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.globalization.persiancalendar.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-PersianCalendar.ps1
- ................. Today ..........................
- Today is:
- Wednesday, 9/1/2010 03:25:17 PM in the Gregorian calendar.
- Wednesday, 6/10/1389 15:25:17 in the Persian calendar.
- ............... Fields ............................
- PersianEra = 1
- ............... Properties ........................
- Eras:
- era = 1
- Gregorian Date Range Supported by the Persian Calendar:
- From 3/21/0622 12:00:00 AM
- To 12/31/9999 11:59:59 PM
- TwoDigitYearMax = 99
- ............ Selected Methods ......................
- GetDayOfYear: day = 165
- GetDaysInMonth: days = 30
- GetDaysInYear: days = 366
- GetLeapMonth: month = 0
- GetMonthsInYear: months = 12
- IsLeapDay: = False
- IsLeapMonth: = False
- IsLeapYear: 1370 is a leap year = True
- ToFourDigitYear:
- If TwoDigitYearMax = 99, ToFourDigitYear(99) = 99
- If TwoDigitYearMax = 2010, ToFourDigitYear(99) = 1999
- #>
- # Get today's date.
- $Jc = new-object system.Globalization.PersianCalendar
- $ThisDate = [System.DateTime]::Now
- # Display the current date using the Gregorian and Persian calendars.
- "Today is:"
- " {0:dddd}, {0} in the Gregorian calendar." -f $ThisDate
- " {0}, {1}/{2}/{3} {4}:{5}:{6} in the Persian calendar." -f $jc.GetDayOfWeek($thisDate),
- $jc.GetMonth($thisDate),
- $jc.GetDayOfMonth($thisDate),
- $jc.GetYear($thisDate),
- $jc.GetHour($thisDate),
- $jc.GetMinute($thisDate),
- $jc.GetSecond($thisDate)
- # Fields
- "............... Fields ............................"
- "PersianEra = {0}" -f [System.Globalization.PersianCalendar]::PersianEra
- # Properties
- "............... Properties ........................"
- "Eras:"
- foreach ($era in $jc.Eras){
- " era = {0}" -f $era
- }
- "Gregorian Date Range Supported by the Persian Calendar:"
- " From {0:G}" -f $jc.MinSupportedDateTime
- " To {0:G}" -f $jc.MaxSupportedDateTime
- "TwoDigitYearMax = {0}" -f $jc.TwoDigitYearMax
- # Methods
- "............ Selected Methods ......................"
- "GetDayOfYear: day = {0}" -f $jc.GetDayOfYear($thisDate)
- "GetDaysInMonth: days = {0}" -f $jc.GetDaysInMonth($thisDate.Year, $thisDate.Month,
- [System.Globalization.PersianCalendar]::PersianEra)
- "GetDaysInYear: days = {0}" -f $jc.GetDaysInYear($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra)
- "GetLeapMonth: month = {0}" -f $jc.GetLeapMonth($thisDate.Year, [System.Globalization.PersianCalendar]::PersianEra)
- "GetMonthsInYear: months = {0}" -f $jc.GetMonthsInYear($thisDate.Year,[System.Globalization.PersianCalendar]::PersianEra)
- "IsLeapDay: = {0}" -f $jc.IsLeapDay($thisDate.Year, $thisDate.Month, $thisDate.Day,
- [System.Globalization.PersianCalendar]::PersianEra)
- "IsLeapMonth: = {0}" -f $jc.IsLeapMonth($thisDate.Year, $thisDate.Month,
- [System.Globalization.PersianCalendar]::PersianEra)
- "IsLeapYear: 1370 is a leap year = {0}" -f $jc.IsLeapYear(1370, [System.Globalization.PersianCalendar]::PersianEra)
- # Get the 4-digit year for a year whose last two digits are 99. The 4-digit year
- # depends on the current value of the TwoDigitYearMax property.
- "ToFourDigitYear:"
- " If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99)
- $jc.TwoDigitYearMax = $thisDate.Year
- " If TwoDigitYearMax = {0}, ToFourDigitYear(99) = {1}" -f $jc.TwoDigitYearMax, $jc.ToFourDigitYear(99)
Subscribe to:
Posts (Atom)