为Test-Connection创建一个空白对象

时间:2014-10-23 00:59:09

标签: object powershell

我最近回复了关于Test-Connection Powershell script: create loop for ResponseTime

的SO帖子

Test-Connection无法连接时,它将返回System.Net.NetworkInformation.PingException,这很好,但我想将其记录为输出中的空对象,而不是跳过它。我知道我只能select我想要的属性,只需在命令行上创建一个自定义对象即可输出。这就是我接触相关问题的方法,但我觉得我可以做得更好。

我的愿望是拥有这样的输出

Source        Destination     IPV4Address      IPV6Address                              Bytes    Time(ms) 
------        -----------     -----------      -----------                              -----    -------- 
WYVERN        localhost       127.0.0.1        ::1                                      32       0        
              failed host     169.254.158.1
WYVERN        localhost       127.0.0.1        ::1                                      32       0   

Test-Connection插入虚线后,两个返回正确。它具有Test-Connection正确返回的所有属性,但由于失败,因此只有部分属性具有值。我尝试实现此目的的唯一方法是创建另一个类似类型的对象。 注意 (Test-Connection -Count 1 localhost).GetType().FullName已返回System.Management.ManagementObject

$servers = "10.50.10.100","169.254.54.1"
$servers | ForEach-Object{
    Test-Connection $_ -Count 1 -ErrorAction SilentlyContinue
    If(!$testconnection){
        $blank = New-Object -TypeName System.Management.ManagementObject
        $blank.Destination = $_
    }
} 

Test-Connection返回的不仅仅是基本的System.Management.ManagementObject。所以问题是新对象不会具有相同的属性,因此$blank.Destination = $_将失败,因为“无法在此对象上找到'Destination'”。我还尝试使用Test-Connection -Count 1 127.0.0.1 | gm -MemberType Property来尝试创建一个属性集合,我可以使用它来构建我的空白对象但是没有结果。很可能因为我做得不对。

FYI

我希望在我的脚本的其他地方应用这个逻辑。虽然test-connection是我在这个问题中所讨论的cmdlet,但我正在寻找更广泛的解决方案。

尝试

我尝试过这样的事情但没有成功,但是这个物体没有一起输出。

$props = @{}
Test-Connection -Count 1 127.0.0.1 | gm -MemberType Property | %{$props.($_.Name) = ""}
$props.destination = "FailedHostAddress"
New-Object -TypeName PSCustomObject -Property $props

2 个答案:

答案 0 :(得分:0)

不确定这是否有帮助:

$base = Test-Connection 127.0.0.1 -Count 1 
$blank = $base | select *
foreach ($prop in $base.psobject.Properties.Name)
{$blank.$prop = $null}

select *将保留所有属性,但将它们转换为note属性,以便它们可写,并且您可以将它们设置为您想要的任何属性。

答案 1 :(得分:0)

我可能会这样做:

$servers = '10.50.10.100', '169.254.54.1', 'somehost'

$servers | % {
  $dst = $_
  try {
    Test-Connection $dst -Count 1 -ErrorAction Stop | % {
      $props = [ordered]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $_.IPV4Address
        'IPv6Address' = $_.IPV6Address
        'Available'   = $true
      }
    }
  } catch {
    try {
      $addr = [ipaddress]$dst
      $props = [ordered]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $addr.MapToIPv4()
        'IPv6Address' = $addr.MapToIPv6()
        'Available'   = $false
      }
    } catch {
      $props = [ordered]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $null
        'IPv6Address' = $null
        'Available'   = $false
      }
    }
  }
  New-Object -Type PSObject -Property $props
}

[ordered]哈希会生成属性appear in the given order

使用PowerShell v3或更新版本可以简化为:

$servers | % {
  $dst = $_
  try {
    Test-Connection $dst -Count 1 -ErrorAction Stop | % {
      [PSCustomObject]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $_.IPV4Address
        'IPv6Address' = $_.IPV6Address
        'Available'   = $true
      }
    }
  } catch {
    try {
      $addr = [ipaddress]$dst
      [PSCustomObject]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $addr.MapToIPv4()
        'IPv6Address' = $addr.MapToIPv6()
        'Available'   = $false
      }
    } catch {
      [PSCustomObject]@{
        'Source'      = $env:COMPUTERNAME
        'Destination' = $dst
        'IPv4Address' = $null
        'IPv6Address' = $null
        'Available'   = $false
      }
    }
  }
}

输出看起来有点像这样:

Source  Destination   IPv4Address   IPv6Address                   Available
------  -----------   -----------   -----------                   ---------
WYVERN  10.50.10.100  10.50.10.100  fe80::3a8f:4854:248d:787f%11       True
WYVERN  169.254.54.1  169.254.54.1  ::ffff:169.254.54.1               False
WYVERN  somehost                                                      False