Thursday, 9 July 2009

Copy-File.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     This script displays the usage of the Exists and the 
  4.     copy methods of System.IP.File   
  5. .DESCRIPTION 
  6.     This script sets up two file names, then checks to see if 
  7.     the sorucefile exists. if so, it's copied to a new file. 
  8. .NOTES 
  9.     File Name  : copy-file.ps1 
  10.     Author     : Thomas Lee - tfl@psp.co.uk 
  11.     Requires   : PowerShell V2 CTP3 
  12. .LINK 
  13.     This script posted to: 
  14.         http://www.pshscripts.blogspot.com 
  15.     MSDN Sample posted at: 
  16.         http://msdn.microsoft.com/en-us/library/system.io.file.exists.aspx 
  17. .EXAMPLE 
  18.     PSH [C:\foo]: .\Copy-File.PS1' 
  19.     Source File (c:\foo\Test.txt) copied to (c:\foo\Test2.txt) 
  20. #> 
  21.  
  22. ## 
  23. # start of script 
  24. ## 
  25.  
  26. # Setup source and destination files 
  27. $SourceFile = "c:\foo\Test.txt"
  28. $NewFile    = "c:\foo\Test2.txt"
  29.  
  30. # Now - check to see if $Sourcefile exists, and if so, 
  31. # copy it to $newfile  
  32. if ([System.IO.File]::Exists($SourceFile))  { 
  33.    [System.IO.File]::Copy($SourceFile, $NewFile
  34.    "Source File ($SourceFile) copied to ($newFile)" 
  35. else
  36. "Source file ($Sourcefile) does not exist." 
  37. # End of script 

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:

 

image

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:

image

Technorati Tags:

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

  1. <# 
  2. .SYNOPSIS 
  3.     This script illustrates the CompareExchange Method 
  4. .DESCRIPTION 
  5.     This script creates three values, and calls CompareExchange method, 
  6.     and displays the results. The first time, we compare two non-equal values 
  7.     so no exchange is done. The second time, the comparison succeeds and the 
  8.     value us updated.    
  9. .NOTES 
  10.     File Name  : Compare-Int32.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  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/801kt583.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Compare-int32.PS1 
  20.     Before 1st call: 
  21.     42 
  22.     69 
  23.     104 
  24.   
  25.     After 1st call, before 2nd 
  26.     42 
  27.     69 
  28.     104 
  29.   
  30.     After 2nd call 
  31.     69 
  32.     69 
  33.     104 
  34. #> 
  35.  
  36. ## 
  37. # Start of Script 
  38. ## 
  39.  
  40. # Create 3 int32 values 
  41.  
  42. [int32] $a=42 
  43. [int32] $b=69 
  44. [int32] $c=104 
  45.  
  46. # Display values before 
  47. "Before 1st call:";$a,$b,$c 
  48. "" 
  49.  
  50. # Call CompareExchange and print results 
  51. # This call compares $a with $c, so there are not equal 
  52. $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c
  53. "After 1st call, before 2nd" 
  54. $a,$b,$c 
  55. "" 
  56.  
  57. # Call CompareExchange and print results 
  58. # This call compares $a with $A, so this call 
  59. # returns $a updated to $b 
  60. $result = [System.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a
  61. "After 2nd call" 
  62. $a,$b,$c 

Wednesday, 1 July 2009

Compare-Double

  1. <# 
  2. .SYNOPSIS 
  3.     This script illustrates the CompareExchange Method 
  4. .DESCRIPTION 
  5.     This script creates three values, and calls CompareExchange method, 
  6.     and displays the results. The first time, we compare to non-equal values 
  7.     so no exchange is done. The second time, the comparison succeeds and the 
  8.     value us updated.    
  9. .NOTES 
  10.     File Name  : Compare-Double.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  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/cd0811yf.aspx 
  18. .EXAMPLE 
  19.     PSH [C:\foo]: .\Compare-Double.PS1
  20.     Before 1st call: 
  21.     42.42 
  22.     69.69 
  23.     104.4 
  24.   
  25.     After 1st call, before 2nd 
  26.     42.42 
  27.     69.69 
  28.     104.4 
  29.   
  30.     After 2nd call 
  31.     69.69 
  32.     69.69 
  33.     104.4 
  34. #> 
  35.  
  36. ## 
  37. # Start of Script 
  38. ## 
  39.  
  40. # Create 3 double values 
  41.  
  42. [double] $a=42.42 
  43. [double] $b=69.69 
  44. [double] $c=104.4 
  45.  
  46. # Display values before 
  47. "Before 1st call:";$a,$b,$c 
  48. "" 
  49.  
  50. # Call CompareExchange and print results 
  51. # This call compares $a with $c, so there are not equal 
  52. $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $c
  53. "After 1st call, before 2nd" 
  54. $a,$b,$c 
  55. "" 
  56. # Call CompareExchange and print results 
  57. # This call compares $a with $A, so this call 
  58. # returns $a updated to $b 
  59. $result = [system.Threading.Interlocked]::CompareExchange([ref]$a, $b, $a
  60. "After 2nd call" 
  61. $a,$b,$c