windows - PowerShell 脚本 "Get-VMHost Hardware.SystemInfo.OtherIdentifyingInfo"将返回 2 个服务标记值

标签 windows powershell virtual-machine vmware

我有以下 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;}

最佳答案

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

如果你这样做会发生什么

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 值。 发生这种情况是因为硬件供应商(例如思科)提供了每个处理器的序列号。

发现here .

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

关于windows - PowerShell 脚本 "Get-VMHost Hardware.SystemInfo.OtherIdentifyingInfo"将返回 2 个服务标记值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51934516/

相关文章:

c# - 如何获取正在运行的 HTML Windows 8 应用程序的名称(不是 WWHOST)

linux - 错误:无法应用目录:nil:NilClass的未定义方法“祖先”

正则表达式与dir命令直接进入powershell

linux - 如何创建从 Azure Linux M 到 Azure 文件存储的符号链接(symbolic link)

linux - 使用 Windows 批处理通过 OpenSSH 远程连接到 Linux

powershell - 使用 PowerShell 替换文件中的字符串

function - Powershell 函数中的可重复参数(最好是链接参数集)

c - 模拟虚拟机,增加pc vs jumps有问题

linux - 在开始之前,我如何检查特定服务器是否正在运行(使用 virsh 命令)|重启了吗?

windows - 神秘的 git 行为