简体   繁体   中英

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.

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. 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 ?

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'

$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. This issue is occured only for the OS Version 'Microsoft Windows Server 2008 R2 Enterprise' and not for the other OS Version.

Conclusion there is no issue with the below command. Issue with the OS Version '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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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