- <#
- .SYNOPSIS
- Demonstrates use of the Win32_PingStatus WMI class
- .DESCRIPTION
- This script is a community content MSDN sample,using PowerShell
- .NOTES
- File Name : Get-PingStatus
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2
- .LINK
- Sample posted to:
- http://pshscripts.blogspot.com/2009/03/get-pingstatusps1.html
- .PARAMETER Comp
- The computer you want to ping - should be resolvable by DNS
- .EXAMPLE
- PSH [C:\foo]: .\Get-PingStaus.ps1 blogger.com
- Computer to ping: blogger.com
- Computer responded in: 127ms
- .EXAMPLE
- PSH [C:\foo]: "blogger.com", "Localhost" | . 'C:\foo\to post\get-pingstatus.PS1'
- Computer to ping: blogger.com
- Computer responded in: 127ms
- Computer to ping: Localhost
- Computer responded in: 0ms
- .EXAMPLE
- PSH [C:\foo]: .\Get-PingStaus.ps1
- Computer to ping: localhost
- Computer responded in: 0ms
- #>
- [Cmdletbinding()]
- param (
- [Parameter(Position=0,mandatory=$false,ValueFromPipeline=$true)]
- [string] $comp = "localhost")
- ###
- # Start of Script
- ###
- Process {
- # Display intput
- "Computer to ping: $comp"
- # Now ping the system
- $ping = get-wmiobject -Query "select * from win32_pingstatus where Address='$comp'"
- # Display Results
- if ($ping.statuscode -eq 0) {
- "Computer responded in: {0}ms" -f $ping.responsetime
- }
- else {
- "Computer did not respond"
- }
- }
This blog contains PowerShell scripts, more PowerShell scripts and still more PowerShell scripts. Occasionally you may see some organisational posts.
Wednesday, 25 March 2009
Get-PingStatus.ps1
Saturday, 14 March 2009
Get-HTTPVersion.ps1
- <#
- .SYNOPSIS
- Demonstrates use of the HTTPVersion class
- .DESCRIPTION
- This script is a re-write of an MSDN sample,using PowerShell
- .NOTES
- File Name : Get-HTTPVersion.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Sample posted to:
- http://pshscripts.blogspot.com/2009/03/get-httpversionps1.html
- Original MSDN sample at:
- http://msdn.microsoft.com/en-us/library/system.net.httpversion.aspx
- .EXAMPLE
- PSH [C:\foo]: .\Get-HTTPVersion.ps1'
- The 'ProtocolVersion' of the protocol before assignment is :1.1
- The 'ProtocolVersion' of the protocol after assignment is :1.0
- The 'ProtocolVersion' of the response object is :1.1
- #>
- ###
- # Start of Script
- ###
- # Create a 'HttpWebRequest' object and display
- $myHttpWebRequest=[system.net.webrequest]::Create("http://www.microsoft.com")
- "The 'ProtocolVersion' of the protocol before assignment is :{0}" -f $myHttpWebRequest.ProtocolVersion
- # Assign Version10 to ProtocolVersion
- $myHttpWebRequest.ProtocolVersion=[system.net.HttpVersion]::Version10
- # Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable
- $myHttpWebResponse=$myHttpWebRequest.GetResponse();
- "The 'ProtocolVersion' of the protocol after assignment is :{0}" -f $myHttpWebRequest.ProtocolVersion
- "The 'ProtocolVersion' of the response object is :{0}" -f $myHttpWebResponse.ProtocolVersion
- # End of script
Thursday, 12 March 2009
Get-DaysInMonth.ps1
- <#
- .SYNOPSIS
- Displys number of days in a particular month.
- .DESCRIPTION
- This scrips uses System.DateTime to determine, then
- display, the numbe of days in some months.
- .NOTES
- File Name : Get-DaysInMonth.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://www.pshscripts.blogspot.com
- .EXAMPLE
- PSH [C:\foo]: . 'C:\foo\Get-daysinmonth.ps1'
- There are 31 days in July 2001
- There are 28 days in February 1998
- There are 29 days in February 1996
- There are 31 days in August 1950
- #>
- ##
- # Start of script
- ##
- # Create variables representing months
- $July = 7
- $February = 2
- $August = 8
- # daysInJuly should be 31.
- $daysInJuly = [System.DateTime]::DaysInMonth(2001, $July)
- "There are $daysinjuly days in July 2001"
- # DaysInFeb should be 28 because the year 1998 was not a leap year.
- $daysInFeb = [System.DateTime]::DaysInMonth(1998, $February)
- "There are $daysinfeb days in February 1998"
- # daysInFebLeap gets 29 because the year 1996 was a leap year.
- $daysInFebLeap = [System.DateTime]::DaysInMonth(1996, $February)
- "There are $daysinfebleap days in February 1996"
- # An august year indeed.
- $daysInAugust = [System.DateTime]::DaysInMonth(1950, $August)
- "There are $daysinAugust days in August 1950"
- # End of Script
Wednesday, 11 March 2009
Get-CountryForIPAddress.ps1
- <#
- .SYNOPSIS
- Gets country for a Geo-IP address
- .DESCRIPTION
- This function uses the New-WebServiceProxy cmdlet to
- create a web service proxy. Then it calls that proxy
- to get the country for a given IP address. After the
- function definition are two examples!
- .NOTES
- File Name : Get-CountryForIPAddress.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- Script posted:
- http://www.pshscripts.blogspot.com
- .INPUTTYPE
- String representing IP Adress
- .RETURNVALUE
- XML Element, holding details of the country
- .EXAMPLE
- Run from PowerShell Prompt:
- Looking up: 131.107.2.200
- Country: UNITED STATES (US)
- .EXAMPLE
- Run from PipelineL
- "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress
- Looking up: 16.0.0.1
- Country: UNITED STATES (US)
- Looking up: 200.200.200.1
- Country: BRAZIL (BR)
- .PARAMETER IPAddress
- A string, or array of strings representing IP Addresses
- The function gets country details for each IPAddress provided
- #>
- function Get-CountryForIPAddress {
- param(
- [Parameter(Position=0, Mandatory=$FALSE, ValueFromPipeline=$TRUE)]
- [String] $IPAddress="131.107.2.200" )
- process {
- "Looking up: {0}" -f $IPAddress
- $s = new-webserviceproxy -uri http://www.webservicex.net/geoipservice.asmx
- foreach ($addr in $IPAddress) {
- $result = $s.GetGeoIP($addr)
- "Country: {0} ({1})" -f $result.countryname, $result.countrycode
- } # end foreach
- } #end process block
- } # end function
- "Example 1:"
- "=========="
- Get-CountryForIPAddress "131.107.2.200"
- "Example 2:"
- "=========="
- "16.0.0.1","200.200.200.1" | Get-CountryForIPAddress
- # End of Script
Tuesday, 10 March 2009
SET-Extension.ps1
- <#
- .SYNOPSIS
- Changes file extension
- .DESCRIPTION
- This script uses the ChangeExtension method to change a file extension
- .NOTES
- File Name : Change-Extension.ps1
- Author : Thomas Lee - tfl@psp.co.uk
- Requires : PowerShell V2 CTP3
- .LINK
- http://pshscripts.blogspot.com/2009/03/change-extensionps1.html
- .EXAMPLE
- PSH [C:\foo]: .\Change-Extension.ps1
- Set Extension(C:\mydir\myfile.com.extension,'.old') returns 'C:\mydir\myfile.com.old'
- Set Extension(C:\mydir\myfile.com.extension, '') returns 'C:\mydir\myfile.com.'
- Set Extension(C:\mydir\, '.old') returns 'C:\mydir\.old'
- #>
- ##
- # Start of Script
- ##
- # Setup file names
- $goodFileName = "C:\mydir\myfile.com.extension"
- $badFileName = "C:\mydir\"
- # Change file name extensions
- $result = [System.IO.Path]::ChangeExtension($goodFileName, ".old")
- Set Extension({0},'.old') returns '{1}'" -f $goodFileName, $result
- $result = [System.IO.Path]::ChangeExtension($goodFileName, "")
- Set Extension({0}, '') returns '{1}'" -f $goodFileName, $result
- $result = [System.IO.Path]::ChangeExtension($badFileName, ".old")
- Set Extension({0}, '.old') returns '{1}'" -f $badFileName, $result
- # 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!