WMIにはいくつか面白いオブジェクトがあるので使ってみる。
Win32_PingStatus : 指定されたアドレスにPingを飛ばして詳細な結果オブジェクトを返す
試しに www.google.com にPingを投げてみるには、
PS C:\Users\imksoo> $ping_status = Get-WmiObject -Namespace "root\cimv2" -Query "SELECT * FROM Win32_PingStatus WHERE Address='www.google.com'"
PS C:\Users\imksoo> $ping_status | Get-Member
TypeName: System.Management.ManagementObject#root\cimv2\Win32_PingStatus
Name MemberType Definition
---- ---------- ----------
PSComputerName AliasProperty PSComputerName = __SERVER
Address Property string Address {get;set;}
BufferSize Property uint32 BufferSize {get;set;}
NoFragmentation Property bool NoFragmentation {get;set;}
PrimaryAddressResolutionStatus Property uint32 PrimaryAddressResolutionStatus {get;set;}
ProtocolAddress Property string ProtocolAddress {get;set;}
ProtocolAddressResolved Property string ProtocolAddressResolved {get;set;}
RecordRoute Property uint32 RecordRoute {get;set;}
ReplyInconsistency Property bool ReplyInconsistency {get;set;}
ReplySize Property uint32 ReplySize {get;set;}
ResolveAddressNames Property bool ResolveAddressNames {get;set;}
ResponseTime Property uint32 ResponseTime {get;set;}
ResponseTimeToLive Property uint32 ResponseTimeToLive {get;set;}
RouteRecord Property string[] RouteRecord {get;set;}
RouteRecordResolved Property string[] RouteRecordResolved {get;set;}
SourceRoute Property string SourceRoute {get;set;}
SourceRouteType Property uint32 SourceRouteType {get;set;}
StatusCode Property uint32 StatusCode {get;set;}
Timeout Property uint32 Timeout {get;set;}
TimeStampRecord Property uint32[] TimeStampRecord {get;set;}
TimeStampRecordAddress Property string[] TimeStampRecordAddress {get;set;}
TimeStampRecordAddressResolved Property string[] TimeStampRecordAddressResolved {get;set;}
TimestampRoute Property uint32 TimestampRoute {get;set;}
TimeToLive Property uint32 TimeToLive {get;set;}
TypeofService Property uint32 TypeofService {get;set;}
__CLASS Property string __CLASS {get;set;}
__DERIVATION Property string[] __DERIVATION {get;set;}
__DYNASTY Property string __DYNASTY {get;set;}
__GENUS Property int __GENUS {get;set;}
__NAMESPACE Property string __NAMESPACE {get;set;}
__PATH Property string __PATH {get;set;}
__PROPERTY_COUNT Property int __PROPERTY_COUNT {get;set;}
__RELPATH Property string __RELPATH {get;set;}
__SERVER Property string __SERVER {get;set;}
__SUPERCLASS Property string __SUPERCLASS {get;set;}
ConvertFromDateTime ScriptMethod System.Object ConvertFromDateTime();
ConvertToDateTime ScriptMethod System.Object ConvertToDateTime();
IPV4Address ScriptProperty System.Object IPV4Address {get=$iphost = [System.Net.Dns]::GetHostEntr...
IPV6Address ScriptProperty System.Object IPV6Address {get=$iphost = [System.Net.Dns]::GetHostEntr...
PS C:\Users\imksoo> $status | Format-Table -AutoSize @{Label="Date";Expression={Get-Date -Format "G"}},Address,ResponseTime,ResponseTimeToLive,IPV4Address,IPV6Address
Date Address ResponseTime ResponseTimeToLive IPV4Address IPV6Address
---- ------- ------------ ------------------ ----------- -----------
2014/05/12 11:17:13 www.google.com 81 52 173.194.117.177
1秒おきにPingを打ち続けるとかであれば、以下の様にFormat-Tableで現在時刻を付与しながら表示してあげると、後々整形しやすいかと。
(ExPingもどきを目指すのであれば、タイムアウト値とかをGet-WmiObjectコマンドレットでPingStatusオブジェクトを作るときに渡しておくとかでしょうか)
PS C:\Users\imksoo> while ( -not (Start-Sleep 1) ) {
>> $ping_status = Get-WmiObject -Namespace "root\cimv2" -Query "SELECT * FROM Win32_PingStatus WHERE Address='www.googl
e.com'"
>> $ping_status | Format-Table -AutoSize @{Label="Date";Expression={Get-Date -Format "G"}},Address,ResponseTime,Respons
eTimeToLive,IPV4Address,IPV6Address
>> }
>>
Date Address ResponseTime ResponseTimeToLive IPV4Address IPV6Address
---- ------- ------------ ------------------ ----------- -----------
2014/05/12 11:19:40 www.google.com 112 52 173.194.117.177
Date Address ResponseTime ResponseTimeToLive IPV4Address IPV6Address
---- ------- ------------ ------------------ ----------- -----------
2014/05/12 11:19:41 www.google.com 94 52 173.194.117.177
Date Address ResponseTime ResponseTimeToLive IPV4Address IPV6Address
---- ------- ------------ ------------------ ----------- -----------
2014/05/12 11:19:42 www.google.com 179 52 173.194.117.177
Date Address ResponseTime ResponseTimeToLive IPV4Address IPV6Address
---- ------- ------------ ------------------ ----------- -----------
2014/05/12 11:19:43 www.google.com 203 52 173.194.117.177