locked
Getting a specific installed software using WMI Object Win32_Object RRS feed

  • Question

  • Hello,

    I have a list of servers where in I am trying to see if a specific version of software is installed or not.

    Software Name: Microsoft Monitoring Agent

    Software Version: 7.1*

    Below is my code:

    Set-Location C:\temp
    
    $servers = Get-Content Servers.txt
    
    $creds = Get-Credential
    
     $srvInfo = New-Object PSObject -Property @{
                    Name = ''
                    Version = ''
                    }
    
    foreach ($server in $servers)
    {
        $info = Get-WmiObject -Credential $creds -Class Win32_Product -ComputerName $server | Where-Object -FilterScript {$_.Name -eq "Microsoft Monitoring Agent" -and $_.Version -like "7.1*"} | Select-Object Name, Version 
       
        
    
        $srvInfo += $info 
    }
    
    $srvInfo | Out-GridView


    I am getting the below error msg:

    Method invocation failed because [System.Management.Automation.PSObject] does not contain a method named 'op_Addition'.
    At line:18 char:5

    +     $srvInfo += $info
    +     ~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidOperation: (op_Addition:String) [], RuntimeException
        + FullyQualifiedErrorId : MethodNotFound

    Please help.

    Tuesday, March 5, 2019 2:54 PM

Answers

  • You could have figured this out easily:

    $filter =  'Name="Microsoft Monitoring Agent" AND Version like "7.1%"'
    $creds = Get-Credential
    Get-Content Servers.txt |
        ForEach-Object{
            $result = Get-WmiObject Win32_Product -Filter $filter -ComputerName $_ -Credential $creds
            [pscustomobject]@{
                ComputerName =  $_
                Name = $result.Name
                Version = $result.Version
            }
        } |
        Out-GridView



    \_(ツ)_/


    • Edited by jrv Tuesday, March 5, 2019 4:01 PM
    • Marked as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Unmarked as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Proposed as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Marked as answer by Rajiv IR Wednesday, March 6, 2019 5:00 AM
    Tuesday, March 5, 2019 4:00 PM

All replies

  • Nice guess but:

    $filter =  'Name="Microsoft Monitoring Agent" AND Version like "7.1%"'
    $creds = Get-Credential
    Get-Content Servers.txt |
        ForEach-Object{
            Get-WmiObject Win32_Product -Filter $filter -Credential $creds -ComputerName $_ 
        } |
        Select PsComputerName, Name, Version |
        Out-GridView


    \_(ツ)_/



    • Edited by jrv Tuesday, March 5, 2019 4:02 PM fixed mistakes
    Tuesday, March 5, 2019 3:19 PM
  • $filter =  'Name="Microsoft Monitoring Agent" AND Version like "7.1%"'
    $creds = Get-Credential
    Get-Content Servers.txt |
        ForEach-Object{
            Get-WmiObject -Credential $creds -Class Win32_Product -ComputerName $_ |  Where-Object -FilterScript {$_.Name -eq "Microsoft Monitoring Agent" -and $_.Version -like "7.1*"}
        } |
        Select PsComputerName, Name, Version |
        Export-Csv -Path C:\temp\MMA.csv
    I modified due to some syntax errors. I am unable to get the list of machines that do not have the app installed. Even they don't, atleast it would be nice to have ComputerName and other properties as None.
    Tuesday, March 5, 2019 3:53 PM
  • You could have figured this out easily:

    $filter =  'Name="Microsoft Monitoring Agent" AND Version like "7.1%"'
    $creds = Get-Credential
    Get-Content Servers.txt |
        ForEach-Object{
            $result = Get-WmiObject Win32_Product -Filter $filter -ComputerName $_ -Credential $creds
            [pscustomobject]@{
                ComputerName =  $_
                Name = $result.Name
                Version = $result.Version
            }
        } |
        Out-GridView



    \_(ツ)_/


    • Edited by jrv Tuesday, March 5, 2019 4:01 PM
    • Marked as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Unmarked as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Proposed as answer by jrv Tuesday, March 5, 2019 9:04 PM
    • Marked as answer by Rajiv IR Wednesday, March 6, 2019 5:00 AM
    Tuesday, March 5, 2019 4:00 PM
  • The error in the original code was that $srvInfo wasn't initialized as an array.  Arrays have an addition method but psobjects don't.

    $srvInfo = @()

    • Edited by JS2010 Tuesday, March 5, 2019 9:32 PM
    Tuesday, March 5, 2019 8:51 PM