Wednesday 25 March 2009

Get-PingStatus.ps1


  1. <#  
  2. .SYNOPSIS  
  3.     Demonstrates use of the Win32_PingStatus WMI class    
  4. .DESCRIPTION  
  5.     This script is a community content MSDN sample,using PowerShell  
  6. .NOTES  
  7.     File Name  : Get-PingStatus 
  8.     Author     : Thomas Lee - tfl@psp.co.uk  
  9.     Requires   : PowerShell V2 
  10. .LINK  
  11.     Sample posted to:  
  12.         http://pshscripts.blogspot.com/2009/03/get-pingstatusps1.html    
  13.       
  14. .PARAMETER Comp 
  15.     The computer you want to ping - should be resolvable by DNS 
  16. .EXAMPLE  
  17.     PSH [C:\foo]:  .\Get-PingStaus.ps1 blogger.com 
  18.     Computer to ping:       blogger.com 
  19.     Computer responded in:  127ms 
  20. .EXAMPLE 
  21.     PSH [C:\foo]: "blogger.com", "Localhost" | . 'C:\foo\to post\get-pingstatus.PS1' 
  22.     Computer to ping:       blogger.com 
  23.     Computer responded in:  127ms 
  24.     Computer to ping:       Localhost 
  25.     Computer responded in:  0ms 
  26. .EXAMPLE 
  27.     PSH [C:\foo]:  .\Get-PingStaus.ps1  
  28.     Computer to ping:       localhost 
  29.     Computer responded in:  0ms 
  30. #>  
  31.  
  32. [Cmdletbinding()] 
  33. param (  
  34. [Parameter(Position=0,mandatory=$false,ValueFromPipeline=$true)] 
  35. [string] $comp = "localhost"
  36.  
  37. ###  
  38. # Start of Script  
  39. ###  
  40.  
  41. Process { 
  42. # Display intput 
  43. "Computer to ping:       $comp" 
  44.  
  45. # Now ping the system 
  46. $ping = get-wmiobject -Query "select * from win32_pingstatus where Address='$comp'" 
  47.  
  48. # Display Results 
  49. if ($ping.statuscode -eq 0) { 
  50. "Computer responded in:  {0}ms" -f $ping.responsetime 
  51. else
  52. "Computer did not respond" 

Saturday 14 March 2009

Get-HTTPVersion.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Demonstrates use of the HTTPVersion class   
  4. .DESCRIPTION 
  5.     This script is a re-write of an MSDN sample,using PowerShell 
  6. .NOTES 
  7.     File Name  : Get-HTTPVersion.ps1 
  8.     Author     : Thomas Lee - tfl@psp.co.uk 
  9.     Requires   : PowerShell V2 CTP3 
  10. .LINK 
  11.     Sample posted to: 
  12.     http://pshscripts.blogspot.com/2009/03/get-httpversionps1.html 
  13.     Original MSDN sample at: 
  14.     http://msdn.microsoft.com/en-us/library/system.net.httpversion.aspx 
  15. .EXAMPLE 
  16.     PSH [C:\foo]: .\Get-HTTPVersion.ps1' 
  17.     The 'ProtocolVersion' of the protocol before assignment is :1.1 
  18.     The 'ProtocolVersion' of the protocol after  assignment is :1.0 
  19.     The 'ProtocolVersion' of the response object is :1.1 
  20. #> 
  21.  
  22. ### 
  23. # Start of Script 
  24. ### 
  25.   
  26. # Create a 'HttpWebRequest' object and display  
  27. $myHttpWebRequest=[system.net.webrequest]::Create("http://www.microsoft.com") 
  28. "The 'ProtocolVersion' of the protocol before assignment is :{0}" -f $myHttpWebRequest.ProtocolVersion 
  29.  
  30. # Assign Version10 to ProtocolVersion 
  31. $myHttpWebRequest.ProtocolVersion=[system.net.HttpVersion]::Version10 
  32.  
  33. #  Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable 
  34. $myHttpWebResponse=$myHttpWebRequest.GetResponse(); 
  35.  
  36. "The 'ProtocolVersion' of the protocol after  assignment is :{0}" -f $myHttpWebRequest.ProtocolVersion 
  37. "The 'ProtocolVersion' of the response object is :{0}" -f $myHttpWebResponse.ProtocolVersion 
  38.  
  39. # End of script 

Thursday 12 March 2009

Get-DaysInMonth.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Displys number of days in a particular month.   
  4. .DESCRIPTION 
  5.     This scrips uses System.DateTime to determine, then 
  6.     display, the numbe of days in some months. 
  7. .NOTES 
  8.     File Name  : Get-DaysInMonth.ps1 
  9.     Author     : Thomas Lee - tfl@psp.co.uk 
  10.     Requires   : PowerShell V2 CTP3 
  11. .LINK 
  12.     http://www.pshscripts.blogspot.com 
  13. .EXAMPLE 
  14.     PSH [C:\foo]: . 'C:\foo\Get-daysinmonth.ps1' 
  15.     There are 31 days in July 2001 
  16.     There are 28 days in February 1998 
  17.     There are 29 days in February 1996 
  18.     There are 31 days in August 1950 
  19. #> 
  20.  
  21. ## 
  22. # Start of script 
  23. ## 
  24.  
  25. # Create variables representing months 
  26. $July      = 7 
  27. $February  = 2 
  28. $August    = 8 
  29.  
  30. # daysInJuly should be 31. 
  31. $daysInJuly = [System.DateTime]::DaysInMonth(2001, $July
  32. "There are $daysinjuly days in July 2001" 
  33.  
  34. # DaysInFeb should be 28 because the year 1998 was not a leap year. 
  35. $daysInFeb = [System.DateTime]::DaysInMonth(1998, $February
  36. "There are $daysinfeb days in February 1998" 
  37.  
  38. # daysInFebLeap gets 29 because the year 1996 was a leap year. 
  39. $daysInFebLeap = [System.DateTime]::DaysInMonth(1996, $February
  40. "There are $daysinfebleap days in February 1996" 
  41.  
  42. # An august year indeed. 
  43. $daysInAugust = [System.DateTime]::DaysInMonth(1950, $August
  44. "There are $daysinAugust days in August 1950" 
  45. # End of Script 

Wednesday 11 March 2009

Get-CountryForIPAddress.ps1

  1. <# 
  2. .SYNOPSIS 
  3.     Gets country for a Geo-IP address 
  4. .DESCRIPTION 
  5.     This function uses the New-WebServiceProxy cmdlet to 
  6.     create a web service proxy. Then it calls that proxy 
  7.     to get the country for a given IP address. After the 
  8.     function definition are two examples! 
  9. .NOTES 
  10.     File Name  : Get-CountryForIPAddress.ps1 
  11.     Author     : Thomas Lee - tfl@psp.co.uk 
  12.     Requires   : PowerShell V2 CTP3 
  13. .LINK 
  14.    Script posted: 
  15.    http://www.pshscripts.blogspot.com 
  16. .INPUTTYPE 
  17.    String representing IP Adress 
  18. .RETURNVALUE 
  19.    XML Element, holding details of the country 
  20. .EXAMPLE 
  21.     Run from PowerShell Prompt: 
  22.     Looking up: 131.107.2.200 
  23.     Country: UNITED STATES (US) 
  24. .EXAMPLE 
  25.     Run from PipelineL 
  26.     "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress 
  27.     Looking up: 16.0.0.1 
  28.     Country: UNITED STATES (US) 
  29.     Looking up: 200.200.200.1 
  30.     Country: BRAZIL (BR) 
  31. .PARAMETER IPAddress 
  32.     A string, or array of strings representing IP Addresses 
  33.     The function gets country details for each IPAddress provided 
  34. #> 
  35.  
  36. function Get-CountryForIPAddress { 
  37. param
  38. [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]  
  39. [String] $IPAddress="131.107.2.200"
  40. process { 
  41.    "Looking up: {0}" -f $IPAddress 
  42.    $s = new-webserviceproxy -uri http://www.webservicex.net/geoipservice.asmx 
  43.    foreach ($addr in $IPAddress) { 
  44.       $result = $s.GetGeoIP($addr
  45.       "Country: {0} ({1})" -f $result.countryname, $result.countrycode 
  46.     } # end foreach 
  47. } #end process block 
  48. } # end function 
  49.  
  50.  
  51. "Example 1:" 
  52. "==========" 
  53. Get-CountryForIPAddress "131.107.2.200" 
  54.  
  55. "Example 2:" 
  56. "==========" 
  57. "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress 
  58. # End of Script 

Tuesday 10 March 2009

SET-Extension.ps1

  1. <#
  2. .SYNOPSIS
  3. Changes file extension
  4. .DESCRIPTION
  5. This script uses the ChangeExtension method to change a file extension
  6. .NOTES
  7. File Name : Change-Extension.ps1
  8. Author : Thomas Lee - tfl@psp.co.uk
  9. Requires : PowerShell V2 CTP3
  10. .LINK
  11. http://pshscripts.blogspot.com/2009/03/change-extensionps1.html
  12. .EXAMPLE
  13. PSH [C:\foo]: .\Change-Extension.ps1
  14. Set Extension(C:\mydir\myfile.com.extension,'.old') returns 'C:\mydir\myfile.com.old'
  15. Set Extension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
  16. Set Extension(C:\mydir\, '.old') returns 'C:\mydir\.old'
  17. #>
  18. ##
  19. # Start of Script
  20. ##
  21. # Setup file names
  22. $goodFileName = "C:\mydir\myfile.com.extension"
  23. $badFileName = "C:\mydir\"
  24. # Change file name extensions
  25. $result = [System.IO.Path]::ChangeExtension($goodFileName, ".old")
  26. Set Extension({0},'.old') returns '{1}'" -f $goodFileName, $result
  27. $result = [System.IO.Path]::ChangeExtension($goodFileName, "")
  28. Set Extension({0}, '') returns '{1}'" -f $goodFileName, $result
  29. $result = [System.IO.Path]::ChangeExtension($badFileName, ".old")
  30. Set Extension({0}, '.old') returns '{1}'" -f $badFileName, $result
  31. # End of Script

PSHScript Blot Scripts - Conversion to V2 CTP 3 Complete!

Back in December, I blogged that I was putting all the scripts I post here into a library you could download. At that point, there were 65 scripts, today the count is 112. In January this year, I also blogged that I was moving over to change the way I posted scripts – I planned to start using the auto-help format for all the new scripts, making them self-documenting (it also made posting scripts faster). All new scripts were posted using that format, and tested under CTP3, but the work of converting older scripts would have to be a work in progress.

Well today, I’ve finally finished upgrading all the scripts to auto-help format. The script library, which you can download from http://www.reskit.net/powershell/ScriptLib.ZIP contains the scripts posted to http://pshscripts.blogspot.com but now fully converted to use the new format – and tested fully on PowerShell V3. At some point, I’ll get around to updating the older blog posts, but as that is a lot of work, it’ll take some time. But all new scripts are posted using this new format – so I’ve just got to update 65 or so blog posts!

I don’t know how valuable these scripts are, but I get just over 50 visitors a day, and just under 100 page hits/day so someone is finding them useful! The other thing I note from looking at the stats, most of the hits come from Google – where people are looking for a script do something specific, which to some degree supports the decision to publish the blog the way I do!

One other interesting thing – those nice folks over at www.powershell.com have asked for (and given!) permission to grab the whole library and post it to WWW.PowerShell.com.

Sadly, I’ve injured my shoulder badly and am finding most typing very painful – which has cut down dramatically on my posting level. Hopefully, I’ll recover and start to publish more!