Powershell Invoke命令添加NoteProperty PSComputerName和异常处理

Powershell Invoke命令添加NoteProperty PSComputerName和异常处理,powershell,Powershell,我正在迭代主机名数组,需要检查它们运行的PowerShell版本。如果检查失败(机器关闭、无PowerShell、未配置远程处理等),则我希望生成主机查询失败的消息 即使$PSVersionTable.PSVersion没有PSComputerName属性,似乎Invoke命令将其添加为NoteProperty 为了处理失败情况,我创建了一个新的System.Version对象,并向其中添加了一个PSComputerNameNoteProperty。这是最好的OO或PowerShell方法吗?有

我正在迭代主机名数组,需要检查它们运行的PowerShell版本。如果检查失败(机器关闭、无PowerShell、未配置远程处理等),则我希望生成主机查询失败的消息

即使
$PSVersionTable.PSVersion
没有
PSComputerName
属性,似乎
Invoke命令
将其添加为
NoteProperty

为了处理失败情况,我创建了一个新的
System.Version
对象,并向其中添加了一个
PSComputerName
NoteProperty
。这是最好的OO或PowerShell方法吗?有更好或更简洁的吗

Try {
    $v = Invoke-Command -ComputerName $thishost {$PSVersionTable.PSVersion} -ErrorAction Stop
}
Catch
{
    $v = New-Object -TypeName "System.Version"
    $v | Add-Member -MemberType NoteProperty -Name PSComputerName -Value "$thishost connect failed"
}
$v
输出如下所示:

"BUFFALO","98b47c10-3dcf-4da2-xxxx-xxxx335c7daa","True","2","0","-1","-1","-1","-1"
"BISON","79d4d003-d740-4474-xxxx-xxxx7d801b3a","True","2","0","-1","-1","-1","-1"
"ZEBRA connect failed",,,"0","0","-1","-1","-1","-1"
"WOLF connect failed",,,"0","0","-1","-1","-1","-1"
"FUGU","b26f0c5c-1549-411b-xxxx-xxxxef53ecce","True","5","0","10586","117","0","117"

我认为最常用的powershell方法是将标准输出和错误消息分开。我认为最常用的powershell方法是将标准输出和错误消息分开