- <#
- .SYNOPSIS
- This script displays the value, and type, of an expression.
- .DESCRIPTION
- This script is a rewrite of the second example on this page, The
- script illustrates how to use the GetType method to return
- the type that results from a calculation. in this case, two
- multiplying two int32 with the Pi field results in a System.Double.
- .NOTES
- File Name : Get-TypeTest.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/06/get-typetestps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/58918ffs.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-TypeTest.PS1
- The type of $Radius is : System.Int32
- Area = 28.2743338823081
- The type is the expression is: System.Double
- #>
- ##
- # Start of Script
- ##
- # Set and display type of radius
- [int] $radius = 3
- "The type of `$Radius is : {0}" -f $radius.GetType()
- # Display Area, and the type of an expression.
- "Area {0}" -f ($radius * $radius * [System.Math]::PI)
- "The type is the expression is: {0}" -f ($radius * $radius * [System.Math]::PI).GetType()
- # End of Script
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Sunday, 28 June 2009
Get-TypeTest.ps1
Labels:
GetType,
powershell,
PowerShell scripts,
System.Type
Wednesday, 17 June 2009
Get-ConformanceLevelEnum.ps1
- <#
- .SYNOPSIS
- This script demonstrates the confirmance level enum.
- .DESCRIPTION
- This script displays the values form the conformancelevel enum
- and checks it against a value.
- .NOTES
- File Name : Get-ConformanceLevelEnum.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/h2344bs2(VS.85).aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ConformanceLevelEnum.ps1'
- System.XML.ConformanceLevel enum has 3 possible values
- Value 0: Auto
- Value 1: Fragment
- Value 2: Document
- $ToCheck1 is NOT document
- $ToCheck2 is Document
- #>
- ##
- # Start of script
- ##
- # Demonstrates the ConformanceLevel enum
- # Thomas Lee - tfl@psp.co.uk
- # Enumerate the enum
- $enums=[enum]::GetValues([System.Xml.ConformanceLevel])
- # Display
- "System.XML.ConformanceLevel enum has {0} possible values" -f $enums.count
- $i=0
- $enums| %{"Value {0}: {1}" -f $i,$_.tostring();$i++}
- ""
- # Checking against a an enum value
- $ToCheck1 = "somethingelse"
- $Tocheck2 = "document"
- if ($ToCheck1 -eq [System.XML.ConformanceLevel]::Document) {
- "`$ToCheck1 is document"}
- else {
- "`$ToCheck1 is NOT document"
- }
- if ($ToCheck2 -eq [System.XML.ConformanceLevel]::Document) {
- "`$ToCheck2 is Document"}
- else {
- "`$ToCheck2 is NOT document"
- }
- #End of Script
Monday, 15 June 2009
Get-TimeTick.ps1
- <#
- .SYNOPSIS
- This script gets and displays the static Tick values from TimeSpan Object
- .DESCRIPTION
- This script
- .NOTES
- File Name : Get-TimeTick.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.timespan.tickspersecond(VS.85).aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-TimeTick.ps1
- Ticks Per:
- Day : 864,000,000,000
- Hour : 36,000,000,000
- Minute : 600,000,000
- Second : 10,000,000
- Millisecond : 10,000
- #>
- ##
- # Start of script
- ##
- # Create the values
- $tsticksperday = [system.timespan]::ticksperday
- $tsticksperhour = [system.timespan]::ticksperhour
- $tstickspermillisecond = [system.timespan]::TicksPerMillisecond
- $tsticksperminute = [system.timespan]::ticksperminute
- $tstickspersecond = [system.timespan]::tickspersecond
- # Display results nicely formatted
- "Ticks Per:"
- "Day : {0, 15}" -f $tsticksperday.tostring("###,###")
- "Hour : {0, 15}" -f $tsticksperhour.tostring("###,###")
- "Minute : {0, 15}" -f $tsticksperminute.tostring("###,###")
- "Second : {0, 15}" -f $tstickspersecond.tostring("###,###")
- "Millisecond : {0, 15}" -f $tstickspermillisecond.tostring("###,###")
- #End of Script
Labels:
powershell,
PowerShell scripts,
PowerShell V2,
System.TImespan
Sunday, 14 June 2009
Get-JapaneseDate.ps1
- <#
- .SYNOPSIS
- This script displays dates using the Japanese calander.
- .DESCRIPTION
- This script creates a new date and time object, then displays
- things using the Japanese calendar. I have changed the original
- script slightly to show the different eras in use.
- .NOTES
- File Name : Get-JapaneseDate.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://pshscripts.blogspot.com/2009/06/get-japanesedateps1.html
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.globalization.japanesecalendar.getyear.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-Japanesedate.ps1'
- April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar:
- Era: 1
- Year: 8
- Month: 4
- DayOfYear: 93
- DayOfMonth: 3
- DayOfWeek: Saturday
- After adding 138 years and 10 months:
- Era: 4
- Year: 26
- Month: 2
- DayOfYear: 34
- DayOfMonth: 3
- DayOfWeek: Monday
- #>
- ##
- # start of script
- ##
- # Helper function
- function DisplayJapaneseDateValue{
- Param ( $DT ) #
- # create a Japenese calendar
- $cal=new-object System.Globalization.JapaneseCalendar
- # display dates using that calendar
- " Era: {0}" -f $Cal.GetEra($DT)
- " Year: {0}" -f $Cal.GetYear($DT)
- " Month: {0}" -f $Cal.GetMonth($DT)
- " DayOfYear: {0}" -f $Cal.GetDayOfYear($DT)
- " DayOfMonth: {0}" -f $Cal.GetDayOfMonth($DT)
- " DayOfWeek: {0}" -f $Cal.GetDayOfWeek($DT)
- ""
- }
- # Set a DateTime to April 3, 1875 of the Gregorian calendar.
- # This date is in the Meiji era (era 1)
- $myDT = new-object System.DateTime 1875, 4, 3,(New-Object System.Globalization.GregorianCalendar)
- # Display the values of the DateTime
- "April 3, 1875 of the Gregorian calendar equals the following in the Japanese calendar:"
- DisplayJapaneseDateValue $myDT
- # Add 138 years and 10 months
- # This takes the date into the Heisei era (era 4)
- $myDT = $myCal.AddYears( $myDT, 138 )
- $myDT = $myCal.AddMonths($myDT, 10 )
- # Displays the values of the DateTime.
- "After adding 138 years and 10 months:"
- DisplayJapaneseDateValue $myDT
- # end of script
Sunday, 7 June 2009
Get-Cookie.ps1
- <#
- .SYNOPSIS
- This script gets and displays the cookies returned from a site.
- .DESCRIPTION
- This script calls System.Net.WebRequest to get a page. in the
- response there may be cookies returned which this script then
- displays. By default, the cookies returned from the MSDN home page
- are displayed. To improve production orientation, the creation of
- the request and getting the response are protected by try/catch
- blocks.
- .NOTES
- File Name : get-cookie.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.net.cookie.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-Cookie.ps1
- 3 Cookies returned from: http://www.microsoft.com/msdn
- Cookie:
- A = I&I=AxUFAAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1
- Domain : .microsoft.com
- Path : /
- Port :
- Secure : False
- When issued : 6/7/2009 10:06:30 AM
- Expires :
- Expired? : False
- Don’t save : False
- Comment :
- URI for Comments:
- Version : 0
- String : A=I&I=AxUFAAAAADmCwAA/fh06TVFlKw6GSuBKKj6Yg!!&M=1 :
- Cookie:
- ADS = SN=175A21EF
- Domain : .microsoft.com
- Path : /
- Port :
- Secure :
- When issued : 6/7/2009 10:06:30 AM
- Expires :
- Expired? : false
- Don’t save : false
- Comment :
- Uri for comments:
- Version : 0
- String: ADS=SN=175A21EF :
- Cookie:
- mediaRssUrl = http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml
- Domain : msdn.microsoft.com
- Path : /
- Port :
- Secure : False
- When issued : 6/7/2009 10:06:30 AM
- Expires :
- Expired? : False
- Don’t save : False
- Comment :
- Uri for comments :
- Version : 0
- String : mediaRssUrl=http://www.microsoft.com/feeds/msdn/en-us/HDI/Home-HDI.xml :
- #>
- param (
- $site = “http://www.microsoft.com/msdn”
- )
- ##
- # Start of script
- ##
- # Create the web request object, catching any errors
- try {
- $request = [System.Net.WebRequest]::Create($site)
- $request.CookieContainer = New-Object System.Net.CookieContainer
- }
- catch {
- "Error creating request";$requst
- return
- }
- # Now get response
- try {
- $response = $request.GetResponse()
- }
- catch {
- "Error getting response from $site - try later"
- return
- }
- # Print number of cookies
- if ($response.Cookies.Count -gt 0) {
- "{0} Cookies returned from: {1}" -f $Response.Cookies.Count,$site
- ""
- }
- # Print the properties of each cookie.
- foreach ($cook in $Response.Cookies) {
- "Cookie:"
- "{0} = {1}" -f $cook.Name, $cook.Value
- "Domain : {0}" -f $cook.Domain
- "Path : {0}" -f $cook.Path
- "Port : {0}" -f $cook.Port
- "Secure : {0}" -f $cook.Secure
- "When issued : {0}" -f $cook.TimeStamp
- "Expires : {0}" -f $cook.expireds
- "Expired? : {0}" -f $cook.expired
- "Don't save : {0}" -f $cook.Discard
- "Comment : {0}" -f $cook.Comment
- "Uri for comments: {0}" -f $cook.CommentUri
- "Version : {0}" -f $cook.Version
- "String: {0} :" -f $cook.ToString()
- ""
- }
- # End of Script
Saturday, 6 June 2009
Get-ProcessPerfCounter.ps1
- <#
- .SYNOPSIS
- This script creates a process, then displays some performance counters.
- .DESCRIPTION
- This script calls System.Diagnostics.Process's Start static
- method to create a process. Then it displays perf stats till the
- process is stopped. Then it prints final stats out.
- .NOTES
- File Name : Get-ProcessPerfCounter
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- <#
- .LINK
- This script posted to:
- http://www.pshscripts.blogspot.com
- MSDN Sample posted at:
- http://msdn.microsoft.com/en-us/library/system.diagnostics.process.peakworkingset64.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-ProcessPerfCounter.ps1
- System.Diagnostics.Process (notepad) -
- -------------------------------------
- physical memory usage : 2,512
- base priority : 8
- priority class : Normal
- user processor time : 00:00:00.0156001
- privileged processor time : 00:00:00.0156001
- total processor time : 00:00:00.0312002
- ** Loads more - snipped for brevity **
- System.Diagnostics.Process (notepad) -
- -------------------------------------
- physical memory usage : 12,424
- base priority : 8
- priority class : Normal
- user processor time : 00:00:00.0156001
- privileged processor time : 00:00:00.0624004
- total processor time : 00:00:00.0780005
- Process has ended
- Process exit code: 0
- Peak physical memory usage of the process : 12,424 kb
- Peak paged memory usage of the process : 2,740 kb
- Peak virtual memory usage of the process : 88,832 kb
- #>
- ##
- # Start of script
- ##
- # Start up Notepad, catching issues
- try {
- $myproc = [System.Diagnostics.Process]::Start("c:\windows\notepad.exe")
- }
- catch {
- "Error starting process"
- return
- }
- # Now print perf stats until Notepad.exe is closed
- do {
- if ( ! $myproc.HasExited ) {
- $myproc.Refresh()
- ""
- "{0} -" -f $myProc.ToString()
- "-------------------------------------"
- " physical memory usage : {0}" -f $($MyProc.WorkingSet64/1kb).tostring("###,###")
- " base priority : {0}" -f $MyProc.BasePriority
- " priority class : {0}" -f $MyProc.PriorityClass
- " user processor time : {0}" -f $MyProc.UserProcessorTime
- " privileged processor time : {0}" -f $MyProc.PrivilegedProcessorTime
- " total processor time : {0}" -f $MyProc.TotalProcessorTime
- # calculate overall peak
- $peakPagedMem = $MyProc.PeakPagedMemorySize64
- $peakVirtualMem = $MyProc.PeakVirtualMemorySize64
- $peakWorkingSet = $MyProc.PeakWorkingSet64
- } # end of if
- } # end of do
- while (!$myproc.WaitForExit(1000)) # Wait a second and do it again
- # Here process has exited
- # Print out final results
- ""
- "Process has ended"
- "Process exit code: {0}" -f $MyProc.ExitCode
- # Display peak memory statistics for the process.
- "Peak physical memory usage of the process : {0,7} kb" -f $($peakWorkingSet/1kb).ToString("###,###")
- "Peak paged memory usage of the process : {0,7} kb" -f $($peakPagedMem/1kb).ToString("###,###")
- "Peak virtual memory usage of the process : {0,7} kb" -f $($peakVirtualMem/1kb).ToString("###,###")
- # End of script
Subscribe to:
Posts (Atom)