[英]Powershell Win32_OperatingSystem select Caption compare with OS Version 'Microsoft Windows Server 2008 R2 Enterprise' resulting wrong result
When I try to compare my OS version 'Microsoft Windows Server 2008 R2 Enterprise' with Win32_OperatingSystem select caption result it is resulting false. 当我尝试将操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”与Win32_OperatingSystem选择字幕结果进行比较时,结果为false。
Example: 例:
$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'}
Result: 结果:
False
But it should result True. 但这应为True。 Could someone explain why it is resulting false even my OS Version and select caption is correct.
有人可以解释为什么即使我的操作系统版本和选择的标题正确也导致错误。
If you run this command 如果运行此命令
Get-WmiObject -class Win32_OperatingSystem -ComputerName insert the computername | select Caption
Is the result really Microsoft Windows Server 2008 R2 Enterprise ? 结果真的是Microsoft Windows Server 2008 R2 Enterprise吗?
if i run you script like this on my computer 如果我在计算机上运行这样的脚本
$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'}
it work 这行得通
I executed below script to know why it is resulting 'False' for OS Version 'Microsoft Windows Server 2008 R2 Enterprise' 我执行了以下脚本,以了解为什么操作系统版本“ 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
I really surprised by seeing the result. 看到结果,我真的很惊讶。 Length is not matching and found there was a space at the end of OS Version 'Microsoft Windows Server 2008 R2 Enterprise ' caused this issue.
长度不匹配,并且发现操作系统版本'Microsoft Windows Server 2008 R2 Enterprise'的末尾有空格导致此问题。 This issue is occured only for the OS Version 'Microsoft Windows Server 2008 R2 Enterprise' and not for the other OS Version.
仅对于操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”,而不对于其他操作系统版本,才会发生此问题。
Conclusion there is no issue with the below command. 结论以下命令没有问题。 Issue with the OS Version 'Microsoft Windows Server 2008 R2 Enterprise'
操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”的问题
Get-WmiObject -class Win32_OperatingSystem | select Caption
Could someone try for OS version 'Microsoft Windows Server 2008 R2 Enterprise' and confirm. 有人可以尝试使用操作系统版本“ Microsoft Windows Server 2008 R2 Enterprise”并进行确认。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.