Author Posts

January 14, 2018 at 4:11 pm


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


January 14, 2018 at 6:12 pm

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.

January 14, 2018 at 10:10 pm

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'
        ComputerName = $PSItem
        Status       = $ServerStatus
        $AppName     = $AppStatus

$AppReport | Format-Table -AutoSize