How to properly get System Model in PowerShell and set it as a parameter?

Arthur Durand 171 Reputation points
2023-02-20T19:02:36.3366667+00:00
I need to use PowerShell to properly get the System Model of a PC and use the model number as a parameter so that I can run tasks specific to the model number. My code is a little more complex due to the fact that some of the methods returns values like 'Not Available', 'To be filled by O.E.M.'or  'System Product Manufacturer' - in these instances I do not want the $Model parameter to be 'Not Available', 'To be filled by O.E.M.'or  'System Product Manufacturer'.

I have the following, but it doesn't work as desired

$BaseBoardManufacturer = Get-WmiObject Win32_BaseBoard | Select Manufacturer

if ((-not ([string]::IsNullOrWhiteSpace($BaseBoardManufacturer))) -and ($BaseBoardManufacturer.Manufacturer -ne 'Not Available') -or ($BaseBoardManufacturer.Manufacturer -ne 'System manufacturer')) { [String]$Manufacturer = ($BaseBoardManufacturer.Manufacturer).ToString() }

$ProductManufacturer = Get-WmiObject -Class:Win32_ComputerSystemProduct | select Vendor
if ((-not ([string]::IsNullOrWhiteSpace($ProductManufacturer))) -and ($ProductManufacturer.Vendor -ne 'Not Available') -or ($ProductManufacturer.Vendor -ne 'System manufacturer') -or ($ProductManufacturer.Vendor -ne 'To be filled by O.E.M.'))
{
    [String]$Manufacturer = ($ProductManufacturer.Vendor).ToString()
}

$SystemManufacturer = Get-WmiObject -Class:Win32_ComputerSystem | select Manufacturer
if ((-not ([string]::IsNullOrWhiteSpace($SystemManufacturer))) -and ($SystemManufacturer.Manufacturer -ne 'Not Available') -or ($SystemManufacturer.Manufacturer -ne 'System manufacturer') -or ($SystemManufacturer.Manufacturer -ne 'To be filled by O.E.M.'))
{
    [String]$Manufacturer = ($SystemManufacturer.Manufacturer).ToString()
}

$BaseBoardProduct = Get-WmiObject Win32_BaseBoard | Select Product
if ((-not ([string]::IsNullOrWhiteSpace($BaseBoardProduct))) -and ($BaseBoardProduct.Product -ne 'Not Available'))
{
    [String]$Model = ($BaseBoardProduct.Product).ToString()
}

$ProductVersion = Get-WmiObject -Class:Win32_ComputerSystemProduct | select Version
if ((!($ProductVersion.Version -ne 'System Version')) -xor (!($ProductVersion.Version -ne 'To be filled by O.E.M.')) -and (-not ([string]::IsNullOrWhiteSpace($ProductVersion))))
{
    [String]$Model = ($ProductVersion.Version).ToString()
}

$SystemModel = Get-WmiObject -Class:Win32_ComputerSystem | select Model
if ((-not ([string]::IsNullOrWhiteSpace($SystemModel))) -and ($SystemModel.Model -ne 'System Product Name') -or ($SystemModel.Model -ne 'To be filled by O.E.M.'))
{
    [String]$Model = ($SystemModel.Model).ToString()
}

$SystemManufacturer = Get-WmiObject -Class:Win32_ComputerSystem | select Manufacturer
if ((-not ([string]::IsNullOrWhiteSpace($SystemManufacturer))) -and ($SystemManufacturer.Manufacturer -ne 'Not Available') -or ($SystemManufacturer.Manufacturer -ne 'System manufacturer') -or ($SystemManufacturer.Manufacturer -ne 'To be filled by O.E.M.'))
{
    [String]$Manufacturer = ($SystemManufacturer.Manufacturer).ToString()
}

if ($Manufacturer -like '*Lenovo*')
{
    $ProductVersion = Get-WmiObject -Class:Win32_ComputerSystemProduct | select Version
[String]$Model = ($ProductVersion.Version).ToString()
}

I want e.g.

((!($ProductVersion.Version -ne 'System Version')) -or (!($ProductVersion.Version -ne 'To be filled by O.E.M.')) -and (-not ([string]::IsNullOrWhiteSpace($ProductVersion))))
to return false when the ProductVersion returns 'To be filled by O.E.M.' or 'System Version', but it returns true!

I've tried

((!($ProductVersion.Version -ne 'System Version')) -xor (!($ProductVersion.Version -ne 'To be filled by O.E.M.')) -and (-not ([string]::IsNullOrWhiteSpace($ProductVersion))))
and
if ((-not ($SystemModel.Model -eq 'System Product Name')) -or (-not ($SystemModel.Model -eq 'To be filled by O.E.M.')) -and (-not ([string]::IsNullOrWhiteSpace($SystemModel))))
but I get 'System Version' set as the parameter instead of the Model Number, in other words the argument returns true when it is suppoesed to return false
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2023-02-20T20:23:20.5766667+00:00

    Is this the set of conditions you want?

    ( ($ProductVersion.Version -ne 'System Version') -AND 
    ( ($ProductVersion.Version -ne 'To be filled by O.E.M.')) -AND
    ( -NOT ([string]::IsNullOrWhiteSpace($ProductVersion))))
    
    

    The original used "NOT (X not equal Y)" which is actually "X equal Y" once you remove the double negative.

    Also, mixing negative tests with the OR condition will almost aways never work because if the first condition is true the second condition can't be true when referring to the same object.


  2. Arthur Durand 171 Reputation points
    2023-02-20T21:15:36.87+00:00
    
    $BaseBoardProduct = Get-WmiObject Win32_BaseBoard | Where-Object {$_.Product -ne 'Not Available'} | Select-Object -ExpandProperty Product
    if (-not ([string]::IsNullOrWhiteSpace($BaseBoardProduct)))
    {
        $Model = $BaseBoardProduct
    }
    
    $ProductVersion = Get-WmiObject -Class:Win32_ComputerSystemProduct | Where-Object {$_.Version -ne 'System Version' -and $_.Version -ne 'To be filled by O.E.M.'} | Select-Object -ExpandProperty Version
    if (-not ([string]::IsNullOrWhiteSpace($ProductVersion)))
    {
        $Model = $ProductVersion
    }
    
    $SystemModel = Get-WmiObject -Class:Win32_ComputerSystem | Where-Object {$_.Model -ne 'System Product Name' -and $_.Model -ne 'To be filled by O.E.M.'} | Select-Object -ExpandProperty Model
    if (-not ([string]::IsNullOrWhiteSpace($SystemModel)))
    {
        $Model = $SystemModel
    }
    
    $SystemManufacturer = Get-WmiObject -Class:Win32_ComputerSystem | Where-Object {$_.Manufacturer -ne 'Not Available' -and $_.Manufacturer -ne 'System manufacturer' -and $_.Manufacturer -ne 'To be filled by O.E.M.'} | Select-Object -ExpandProperty Manufacturer
    if (-not ([string]::IsNullOrWhiteSpace($SystemManufacturer)))
    {
        $Manufacturer = $SystemManufacturer
    }
    
    if ($Manufacturer -like '*Lenovo*')
    {
        $ProductVersion = Get-WmiObject -Class:Win32_ComputerSystemProduct | Select-Object -ExpandProperty Version
        $Model = $ProductVersion
    }
    

    solves my issue!


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.