- <#
- .SYNOPSIS
- This script displays the usage of the Exists and the
- copy methods of System.IP.File
- .DESCRIPTION
- This script sets up two file names, then checks to see if
- the sorucefile exists. if so, it's copied to a new file.
- .NOTES
- File Name : copy-file.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.io.file.exists.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Copy-File.PS1'
- Source File (c:\foo\Test.txt) copied to (c:\foo\Test2.txt)
- #>
- ##
- # start of script
- ##
- # Setup source and destination files
- $SourceFile = "c:\foo\Test.txt";
- $NewFile = "c:\foo\Test2.txt";
- # Now - check to see if $Sourcefile exists, and if so,
- # copy it to $newfile
- if ([System.IO.File]::Exists($SourceFile)) {
- [System.IO.File]::Copy($SourceFile, $NewFile)
- "Source File ($SourceFile) copied to ($newFile)"
- }
- else {
- "Source file ($Sourcefile) does not exist."
- }
- # End of script
Thursday, 9 July 2009
Copy-File.ps1
Monday, 6 July 2009
Happy 1st Birthday To HTTP://PshScripts.Blogspot.Com!!!
I’ve been posting scripts here for just over a year – the first script was posted on July 5th 2008. There have been 127 posts, and the downloadable script library now has 141 scripts.
Here are some monthly stats for the past year:
The readership of the blog was not great till I started posting in ernest in November. Since then, there’s been a slow rise each month as more folks find the blog. When I first started this blog, the idea was to post just scripts – on the assumption that folks would find them using Google, et al. And that’s pretty much what has happened. Looking at the last 20 blog hits as an example: 3 of the 20 were direct access, with 17 coming from a referral. Of the 17 referrals, 12 came from Google (google.com, google.it, google.co.uk, google.bg, google.pl), 2 from Bing and one each from Hal Rotenbergs blog, Technet and PowerShell.com. It’s also clear that the readership is pretty international:
I hope this blog and the scripts posted here have been of use. Please feel free to comment on other scripts you’d like to see here!
Compare-Int32.ps1
- <#
- .SYNOPSIS
- This script illustrates the CompareExchange Method
- .DESCRIPTION
- This script creates three values, and calls CompareExchange method,
- and displays the results. The first time, we compare two non-equal values
- so no exchange is done. The second time, the comparison succeeds and the
- value us updated.
- .NOTES
- File Name : Compare-Int32.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/801kt583.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Compare-int32.PS1
- Before 1st call:
- 42
- 69
- 104
- After 1st call, before 2nd
- 42
- 69
- 104
- After 2nd call
- 69
- 69
- 104
- #>
- ##
- # Start of Script
- ##
- # Create 3 int32 values
- [int32] $a=42
- [int32] $b=69
- [int32] $c=104
- # Display values before
- "Before 1st call:";$a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $c, so there are not equal
- $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c)
- "After 1st call, before 2nd"
- $a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $A, so this call
- # returns $a updated to $b
- $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a)
- "After 2nd call"
- $a,$b,$c
Wednesday, 1 July 2009
Compare-Double
- <#
- .SYNOPSIS
- This script illustrates the CompareExchange Method
- .DESCRIPTION
- This script creates three values, and calls CompareExchange method,
- and displays the results. The first time, we compare to non-equal values
- so no exchange is done. The second time, the comparison succeeds and the
- value us updated.
- .NOTES
- File Name : Compare-Double.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/cd0811yf.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Compare-Double.PS1
- Before 1st call:
- 42.42
- 69.69
- 104.4
- After 1st call, before 2nd
- 42.42
- 69.69
- 104.4
- After 2nd call
- 69.69
- 69.69
- 104.4
- #>
- ##
- # Start of Script
- ##
- # Create 3 double values
- [double] $a=42.42
- [double] $b=69.69
- [double] $c=104.4
- # Display values before
- "Before 1st call:";$a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $c, so there are not equal
- $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c)
- "After 1st call, before 2nd"
- $a,$b,$c
- ""
- # Call CompareExchange and print results
- # This call compares $a with $A, so this call
- # returns $a updated to $b
- $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a)
- "After 2nd call"
- $a,$b,$c
Sunday, 28 June 2009
Get-TypeTest.ps1
- <#
- .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
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 1: Auto
- Value 2: Fragment
- Value 3: 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=1
- $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
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
