简体   繁体   English

PowerShell-Win32_OperatingSystem中的Select-Object显示异常

[英]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.

相关问题 Powershell GWMI win32_operatingsystem 修剪输出 - Powershell GWMI win32_operatingsystem trim output Powershell Win32_OperatingSystem选择标题与操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”进行比较导致错误的结果 - Powershell Win32_OperatingSystem select Caption compare with OS Version 'Microsoft Windows Server 2008 R2 Enterprise' resulting wrong result CIM,Win32_OperatingSystem和Win32ShutdownTracker - CIM, Win32_OperatingSystem, and Win32ShutdownTracker 正则表达式从字符串(gwmi Win32_OperatingSystem).Name中提取数字 - Regex to extract number from string (gwmi Win32_OperatingSystem).Name cordova 安装错误 - 命令失败:powershell (Get-CimInstance -ClassName Win32_OperatingSystem).caption 术语“Get-CimInstance”不是重新 - cordova installation error -Command failed: powershell (Get-CimInstance -ClassName Win32_OperatingSystem).caption The term 'Get-CimInstance' is not re 将Win32_OperatingSystem的CountryCode转换为国家/地区字符串 - Convert CountryCode of Win32_OperatingSystem to the country string Get-CimInstance -ClassName Win32_bios, Win32_operatingsystem,? - Get-CimInstance -ClassName Win32_bios, Win32_operatingsystem,? 如何避免Powershell选择对象将枚举显示为数字? - how to avoid that powershell select-object displays enum as number? 来自证书库的Powershell Select-object - Powershell Select-object from cert store 数组中的Powershell选择对象不起作用 - Powershell Select-Object from array not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM