Find computers without specific app

This topic contains 2 replies, has 3 voices, and was last updated by  Sam Boutros 1 week, 2 days ago.

  • Author
    Posts
  • #91409

    Arik Cher
    Participant

    Hi

    I have this :

    $servers =Get-Content -Path "C:\servers.txt"
    Invoke-command -ea SilentlyContinue -computer $servers -ScriptBlock {Get-WmiObject -class win32_product | where {$_.Name -eq "app1"} | select Name}

    the output is :
    Name PSComputerName RunspaceId
    App1 xxx ddd

    I want to find all the computers that doesn't contain "app1" .
    the output should contains also Name PSComputerName RunspaceId

    I tried ForEach with If but I didn't get the computers names

    Thanks

  • #91438

    Don Jones
    Keymaster

    Simply saying you "tried ForEach" isn't really helpful; you'll want to show your code so that we can help you spot the problem.

    In your case, you won't get any results from computers that don't have the app installed. So if you compare the computer names you started with to the ones that give you results, the "missing" ones are the ones that don't have the app.

    $servers =Get-Content -Path "C:\servers.txt"
    
    $hasapp = Invoke-command -ea SilentlyContinue -computer $servers -ScriptBlock {Get-WmiObject -class win32_product | where {$_.Name -eq "app1"} } | Select -Expand PSComputerName
    
    Compare-Object $servers $hasapp |
    Where SideIndicator -eq "< ="
    

    You will have to experiment with that. It might need => instead, for example. This will also run a little faster if you use -Filter on Get-WmiObject instead of getting everything and then running Where-Object.

  • #91451

    Sam Boutros
    Participant

    I would account for the 3 scenarios, server unreachable, app present, app not present, and normalize the output like

    $ServerList = Get-Content -Path 'C:\servers.txt'
    $AppName = 'App1'
    
    $AppReport = $ServerList | foreach {
        try {
            $Result = Invoke-command -ComputerName $PSItem -ErrorAction Stop -ScriptBlock {
                Get-WmiObject -Class win32_product | where { $PSItem.Name -eq $Using:AppName } | select Name
            }
            if ($Result) {
                $ServerStatus = 'Online'
                $AppStatus = $Result.Name
            } else {
                $ServerStatus = 'Online'
                $AppStatus = 'Not Present'
            }
        } catch {
            $ServerStatus = 'Unreachable'
            $AppStatus = 'Presense Unknown'
        }
        [PSCustomObject][Ordered]@{
            ComputerName = $PSItem
            Status       = $ServerStatus
            $AppName     = $AppStatus
        }
    }
    
    $AppReport | Format-Table -AutoSize 
    
    
    

You must be logged in to reply to this topic.