- <#
- .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
Labels:
powershell,
PowerShell scripts,
Win32_PingStatus
Subscribe to:
Post Comments (Atom)
2 comments:
Just curious why the WMI method versus System.Net.NetworkInformation.ping . When I first got into Powershell this question came up and specifically with the ping routine. Using the .Net method seems to be faster but that could be based on several factors (such as sloppy coding on my part).
There's no reason why one vs the other. The script was created as a sample for MSDN, and was published here!
I suppose it depends on your needs. If you are doing lots of WMI related stuff, then use the WMI interface. If you are writing managed code, then the .NET implementation is used.
I'll have to take a look at the .ping .NET class, recode this script using that interface.
Post a Comment