[英]PowerShell - Select-Object from Win32_OperatingSystem displays rather oddly
First time poster here, I'm a bit of a beginner and I've been keen to get my PowerShell scripting skills up to scratch and I'm come across something rather confusing... 第一次来这里,我是个初学者,我一直热衷于掌握PowerShell脚本编写技巧,并且遇到了一些令人困惑的事情……
I've made a script to query a collection of computers and I want to query Win32_OperatingSystem but only extrapolate the Build number so I can populate my PSObject with it. 我已经编写了一个查询计算机集合的脚本,并且我想查询Win32_OperatingSystem,但仅推断构建号,以便可以用它填充PSObject。 I'm trying to add some If logic so that if the build number is 7601, I can write a message under my OS column.
我正在尝试添加一些If逻辑,以便内部版本号为7601时,可以在OS列下写一条消息。
The problem I'm having is that the BuildNumber values are coming out as @{BuildNumber=7601} instead of 7601 for instance. 我遇到的问题是BuildNumber值显示为@ {BuildNumber = 7601}而不是例如7601。 That, and my If statement is borked.
那,我的If语句很令人讨厌。
$Machines = Get-Content .\Computers.txt
Foreach($Machine in $Machines)
{
$sweet = (Get-WmiObject -Class Win32_OperatingSystem -computer $Machine | Select-Object BuildNumber)
$dversion = if ($sweet -eq "@{BuildNumber=7601}") {Yes!} else {"Nooooo!"}
New-Object PSObject -Property @{
ComputerName = $Machine
Sweet = $sweet
OS = $dversion
}
}
The issue is that the Get-WMIObject cmdlet is returning a Hash Table. 问题是Get-WMIObject cmdlet返回哈希表。 Then the Select-Object is returning just the BuildNumber section you want, the BuildNumber property and it's value.
然后,选择对象仅返回所需的BuildNumber部分,BuildNumber属性及其值。 You need to add the -ExpandProperty parameter to only get the value back, not the name/value pair.
您需要添加-ExpandProperty参数以仅返回值,而不是名称/值对。
Get-WMIObject -Class Win32_OperatingSystem | Select-Object BuildNumber
Returns 退货
@{BuildNumber=7601}
With ExpandProperty 带有ExpandProperty
Get-WMIObject -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber
Returns 退货
7601
Just another option with a ping test to skip unavailable machines. ping测试的另一种选择是跳过不可用的计算机。
Get-Content .\Computers.txt | Where-Object {Test-Connection -ComputerName $_ -Count 1 -Quiet} | Foreach-Object {
$sweet = Get-WmiObject -Class Win32_OperatingSystem -ComputerName $_ | Select-Object -ExpandProperty BuildNumber
New-Object PSObject -Property @{
ComputerName = $_.__SERVER
Sweet = $sweet
OS = if ($sweet -eq 7601) {'Yes!'} else {'Nooooo!'}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.