我正在尝试使用 WMI 查询多个服务器,但我并不总是能够访问这些服务器。
代码如下。唉,它向控制台返回“访问被拒绝”,但我似乎无法摆脱它。那好吧。
但是,我am捕获我无法连接的服务器,以便我可以告诉其他人查看它们或请求访问。
但是当我运行代码时,它只返回第一个服务器列表;即使 $failed_servers 有值,也不会返回任何内容。如果我告诉两者都通过管道连接到 ogv,则会弹出两个窗口。
为什么“$variable|select”都不起作用?如果我删除 $failed_servers 上的选择,那么它就会显示出来,尽管只是位于成功服务器的正下方。这还可以,但不是很好。
$list = ("servera","serverb","serverc")
$failed_servers = @()
$final = foreach ($server_instance in $list)
{
$errors=@()
gwmi -query "select * from win32_service where name like '%SQLSERVER%'" -cn $server_instance -ErrorVariable +errors -ErrorAction SilentlyContinue
if ($errors.Count -gt 0) {$failed_servers += $server_instance
}
}
$final|select pscomputername, name, startmode, state |where {$_.pscomputername -ne $null}
$failed_servers |select @{N='Failed Servers'; E={$_}}
你所经历的是仅仅是一个display problem:
-
两者都是你的Select-Object https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object调用产生输出对象4
或更少的属性谁的类型没有与之关联的显式格式化数据(据报道Get-FormatData https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/get-formatdata).
-
这会导致 PowerShell 的 for-display 输出格式化系统 https://learn.microsoft.com/en-us/powershell/scripting/developer/format/formatting-file-overview通过隐式渲染它们Format-Table https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/format-table cmdlet.
-
The 显示列Format-Table
用途是锁定基于的属性第一个对象 that Format-Table
收到。
-
所以,your second Select-Object
调用,其输出对象不共享任何属性与第一个输出的对象一起,有效地产生无可见输出- 然而,对象are发送到成功输出流并且are可以用来程序化处理.
一个简单的演示:
& {
# This locks in Month and Year as the display columns of the output table.
Get-Date | Select-Object Month, Year
# This command's output will effectively be invisible,
# because the property set Name, Attributes does not overlap with
# Month, Year
Get-Item \ | Select-Object Name, Attributes
}
输出将如下所示 - 请注意第二条语句的输出实际上是不可见的(除了额外的空行):
Month Year
----- ----
9 2021
请注意,该问题甚至可能影响single输出不同类型对象的语句(其类型没有关联的格式化数据);例如。:
(Get-Date | Select-Object Year), (Get-Item \ | Select-Object Name)
解决方法:
未来潜在的改进:
GitHub 问题 #7871 https://github.com/PowerShell/PowerShell/issues/7871建议至少发行一个warning如果输出对象实际上变得不可见。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)