首页 > 解决方案 > PowerShell 脚本“Get-VMHost Hardware.SystemInfo.OtherIdentifyingInfo”将为服务标签返回 2 个值

问题描述

我有以下 power-shell 脚本,我想在其中获取我们服务器的 ServiceTag:-

((Get-VMHost | Get-View).Hardware.SystemInfo.OtherIdentifyingInfo | ?{$_.IdentifierType.Key -like "ServiceTag"}).IdentifierValue

但我注意到,对于某些服务器,我会得到 2 个代表服务标签的值,这是正常的吗?如果是这样,那么我可以使用哪一个来识别服务器?

当我运行这个脚本时编辑: -

((Get-VMHost | Get-View).Hardware.SystemInfo.OtherIdentifyingInfo | ?{$_.IdentifierType.Key -like "ServiceTag"}).IdentifierValue
foreach ($vmhost in Get-VMHost) {
    $ServiceTag = $vmhost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo; $ServiceTag | Get-Member

    if ([string]::IsNullOrEmpty($ServiceTag)) { $ServiceTag = 'N/A' }

    Write-Host "$($vmhost.Name) - $ServiceTag"
}

我懂了 :-

Name            MemberType Definition
----            ---------- ----------
Equals          Method     bool Equals(System.Object obj)
GetHashCode     Method     int GetHashCode()
GetType         Method     type GetType()
ToString        Method     string ToString()
DynamicProperty Property   VMware.Vim.DynamicProperty[] DynamicProperty {get;set;}
DynamicType     Property   string DynamicType {get;set;}
IdentifierType  Property   VMware.Vim.ElementDescription IdentifierType {get;set;}
IdentifierValue Property   string IdentifierValue {get;set;}

标签: windowspowershellvirtual-machinevmware

解决方案


这实际上是一条评论,但因为它需要格式化为多行,所以我添加了它,就好像一个答案

如果你这样做会发生什么

foreach ($vmhost in Get-VMHost) {
    $ServiceTag = $vmhost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | 
                  Where-Object { $_.IdentifierType.Key -eq "ServiceTag"}

    if ([string]::IsNullOrEmpty($ServiceTag)) { $ServiceTag = 'N/A' }

    Write-Host "$($vmhost.Name) - $ServiceTag"
}

上面似乎没有返回带有服务标签的字符串,而是一个对象。该对象有一个IdentifierValue属性,它保存带有实际服务标签的字符串。所以我们需要更深一步:

foreach ($vmhost in Get-VMHost) {
    $ServiceTag = $vmhost.ExtensionData.Hardware.SystemInfo.OtherIdentifyingInfo | 
                  Where-Object { $_.IdentifierType.Key -eq "ServiceTag"}

    if (($ServiceTag) -and $ServiceTag.IdentifierValue) { 
        $ServiceTag = $ServiceTag.IdentifierValue
    }

    Write-Host "$($vmhost.Name) - $ServiceTag"
}

编辑

从物理服务器获取序列号时,您似乎必须处理硬件供应商公开这些属性的方式。不同的供应商对待它们的方式不同。

您已经尝试过的各种代码为IdentifierValue. 这是因为硬件供应商(如 Cisco)提供每个处理器的序列号。

发现这里

向下滚动到ESXi 物理服务器章节


推荐阅读