繁体   English   中英

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

当我尝试将操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”与Win32_OperatingSystem选择字幕结果进行比较时,结果为false。

例:

$Server='Computername' #Computername which has OS Version 'Microsoft Windows Server 2008 R2 Enterprise'
$OSVersion = Invoke-command -computername $Server -scriptblock {Get-WmiObject -class Win32_OperatingSystem | select Caption}
#OS Version comparing with Win32_OperatingSystem caption
If ($OSVersion.Caption -eq 'Microsoft Windows Server 2008 R2 Enterprise')
{
Write-Host 'True'
}
Else
{ Write-Host 'False'}

结果:

False

但这应为True。 有人可以解释为什么即使我的操作系统版本和选择的标题正确也导致错误。

如果运行此命令

Get-WmiObject -class Win32_OperatingSystem -ComputerName insert the computername | select Caption

结果真的是Microsoft Windows Server 2008 R2 Enterprise吗?

如果我在计算机上运行这样的脚本

$Server='laptop090' #Computername which has OS Version 'Microsoft Windows Server 2008 R2 Enterprise'
$OSVersion = Get-WmiObject -class Win32_OperatingSystem -ComputerName $Server | select Caption
#OS Version comparing with Win32_OperatingSystem caption
If ($OSVersion.Caption -eq 'Microsoft Windows 10 Pro Insider Preview')
{
Write-Host 'True'
}
Else
{ Write-Host 'False'}

这行得通

我执行了以下脚本,以了解为什么操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”会产生“ False”

$Server='ComputerName'
$OSVersion = 'Microsoft Windows Server 2008 R2 Enterprise'
$OSVersionCaption = Invoke-command -computername $Server -scriptblock {Get-WmiObject -class Win32_OperatingSystem | select Caption}
$OSVersionCaption.Caption.length
$OSVersion.length

Result:
43
44

看到结果,我真的很惊讶。 长度不匹配,并且发现操作系统版本'Microsoft Windows Server 2008 R2 Enterprise'的末尾有空格导致此问题。 仅对于操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”,而不对于其他操作系统版本,才会发生此问题。

结论以下命令没有问题。 操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”的问题

Get-WmiObject -class Win32_OperatingSystem | select Caption

有人可以尝试使用操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”并进行确认。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM