Query not returning expected results

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Gerald Gerald 3 years, 7 months ago.

  • Author
    Posts
  • #11595
    Profile photo of Gerald
    Gerald
    Participant

    Running PS v2, and have a very simple query that isn't returinng what I expect.

    (Get-WmiObject -class Win32_OperatingSystem).Caption
    If ((Get-WmiObject -class Win32_OperatingSystem).Caption -eq 'Microsoft Windows 7 Enterprise') {
        Write-Host "Equals"
        } Else {
        write-host "not equals"
        }

    It returns this –

    Microsoft Windows 7 Enterprise
    not equals

    The WMI Caption info is as expected. however when passed throguh the compare in my IF, its not returning what I expect(returns 'not equal' rather than 'equal').

    No doubt something simple, but I just can't see it.

    Thanks

  • #11599
    Profile photo of Kitt Holland
    Kitt Holland
    Participant

    The WMI query returns a string with a trailing space.

    'Microsoft Windows 7 Enterprise '

    You can use that string to compare, or do a trim on your WMI query result

    (Get-WMIObject -class Win32_OperatingSystem).Caption.Trim()

  • #11603
    Profile photo of Gerald
    Gerald
    Participant

    Perfect. Thanks

You must be logged in to reply to this topic.