为 Test-Connection 创建一个空白对象 [英] Create a blank object for Test-Connection

查看:78
本文介绍了为 Test-Connection 创建一个空白对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近回答了一篇关于 Test-Connection 的 SO 帖子 Powershell 脚本:为 ResponseTime 创建循环

I recently answered a SO post about Test-Connection Powershell script: create loop for ResponseTime

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

When a Test-Connection cannot connect it will return a System.Net.NetworkInformation.PingException which is fine but I would like to record that as an empty object in output instead of skipping over it. I am aware that I could just select the properties I want and just create a custom object to output on the command line. That is how I approached the linked question but I feel I could do better.

我的愿望是有这样的输出

My desire is to have output like this

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

The two returns are proper from Test-Connection with a dummy line inserted. It has all the properties of a proper return from Test-Connection but, since it failed, only some of the properties have values. The only approach that I tried to accomplish this was to create another object of a similar type. Note (Test-Connection -Count 1 localhost).GetType().FullName returned 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 尝试创建一个属性集合,我可以用它来构建我的空白对象,但没有结果.很可能是因为我做得不对.

Test-Connection returns more than just a basic System.Management.ManagementObject. So the problem is that a new-object will not have the same properties and, as a result, $blank.Destination = $_ will fail since "'Destination' cannot be found on this object". I also experimented with Test-Connection -Count 1 127.0.0.1 | gm -MemberType Property to try and create a property collection that I could use to build my blank object but that was not bearing an fruit. Most likely since I am not doing it right.

仅供参考

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

I am hoping to apply this logic in other places in my scripts. While test-connection is the cmdlet I am dwelling on in this question I am hunting for a broader solution.

尝试

我尝试过类似这样的事情,但没有成功,但是对象没有一起输出.

I have tried, unsuccessfully, something like this but the object are not being outputted together.

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

推荐答案

我可能会这样做:

$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] 散列使属性 按给定的顺序出现.

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

With PowerShell v3 or newer it can be simplified to this:

$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
      }
    }
  }
}

输出看起来有点像这样:

The output will look somewhat like this:

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

这篇关于为 Test-Connection 创建一个空白对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆