Wright Robert
Wright Robert

Reputation: 1

Retrieve list of PCs that are not running a specific process

How do I get a list of PCs that don't have a process running with this script that I wrote?

<#
Searches AD for all computers that can ping and checks to see if a process 
is running 
#>

Import-Module active*

$PingTest = $null
$Clist = @()

Get-ADComputer -Filter *  -Properties * | ? {$_.operatingsystem -like  "*windows 7*"} |
    ForEach-Object {

        # test to see if the computer is on the network
        $PingTest = Test-Connection -ComputerName $_.name -Count 1 -BufferSize 16 -Quiet 

        # If test is $true adds each computer to the array $Clist
        If ($PingTest) {$Clist += $_.name}
        Else {}

}#ForEach

#check for process running on each computer in the array $Clist

Invoke-Command -ComputerName $Clist -ScriptBlock {Get-Process -Name mcshield} 

Upvotes: 0

Views: 150

Answers (1)

BenH
BenH

Reputation: 10034

Use Get-Process inside an If statement. If a process is returned it will evaluate to true. You could then export the list out as a spreadsheet using Export-Csv

$Computers = Get-ADComputer -Filter "OperatingSystem -like '*Windows 7*'"
$ProcessRunning =  $Computers | 
    ForEach-Object {
        If ( Test-Connection -ComputerName $_.name -Count 1 -BufferSize 16 -Quiet ) {
            If (Get-Process -ComputerName $_.name -Name mcshield -ErrorAction SilentlyContinue) {
                [pscustomobject]@{
                    'ComputerName' = $_.name
                    'Process Running' = $True
                }
            } Else {
                [pscustomobject]@{
                    'ComputerName' = $_.name
                    'Process Running' = $False
                }
            }
        }
    }

$ProcessRunning | Export-Csv C:\example\path.csv -NoTypeInformation

Upvotes: 1

Related Questions