windows - 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;}
解决方案
这实际上是一条评论,但因为它需要格式化为多行,所以我添加了它,就好像一个答案
如果你这样做会发生什么
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 物理服务器章节
推荐阅读
- c# - C# 在轮次之间等待而不暂停脚本的其余部分
- scheme - Racket 博士支持 MiniKanren
- php - SHA512 不正确的登录名从数据库中检索密码
- angular - 如何在 mat-calendar 上更改当前的样式
- php - 由于设置了标头授权,未发送 $_POST 变量
- winapi - 为什么 fopen 在 CreateFile 失败的地方成功?
- angular - Angular中的动画元素位置交换
- javascript - 将 SIP.js 与 React Native 集成
- routing - 有没有办法创建一个仅包含路由及其在 Rocket 中的功能的中央路由文件?
- java - 在 oracle 瘦驱动支持的 tnsnames.ora 中声明多个相同的服务