Wednesday, February 20, 2019

Check process is running or not on windows machine from Power-Shell


Some of the scripter wanted to check process  is running or not in particular windows machine and if process is running so admin can perform required task on system for example kill process, execute any installation file. I tried to create the Power-Shell function to get those process is running on the machine. This function could help somewhere. I have added the array logic so you can pass multiple process as a argument in the function.




################################ Check in local machine #############################

Function CheckProcess($sEnterProccessNameHere)

{

    # Calling Aarray
    @($sEnterProccessNameHere) | `    
    Foreach-Object `
    {
        If (Get-Process $_ -ErrorAction SilentlyContinue)
        { 
        Write-Output "$_ is running" 
        }
        else 
        { 
        Write-Output "$_ is not running" 
        }
    }

}

$sEnterProccessNameHere = @("Services","Outlook","Word","SMSS")  # Pass the process agreement here

CheckProcess $sEnterProccessNameHere      # Calling function 


#################################################################################

Note:- Not able to pass multiple value in below code but single parameter its working fine.Still searching solution for multiple parameter value

########################### Check process on remote machine ##########################

Function CheckProcess([String[]]$sEnterComputerNameHere,[String[]]$sEnterProccessNameHere)
{
    #Write-host " $sEnterComputerNameHere hello"

        @($sEnterComputerNameHere) | ` 
    Foreach-Object `
    {
        # Calling Aarray
        @($sEnterProccessNameHere) | ` 
        Foreach-Object `
        {
             
            If (get-process -computername $sEnterComputerNameHere | where {$_.ProcessName -eq $sEnterProccessNameHere})
            {
            Write-Output "$_ is running"
            }
            else
            {
            Write-Output "$_ is not running"
            }
        }
    }
}

$Script:sEnterProccessNameHere = @("VPNUI")  # Pass the process agreement here
$Script:sEnterComputerNameHere = @("CRP-APPTESTW64")  # Pass the process agreement here

CheckProcess $sEnterComputerNameHere $sEnterProccessNameHere     # Calling function


#################################################################################

############################### Calling multiple process on multiple machine ############

Function CheckProcess([String[]]$sEnterComputerNameHere,[String[]]$sEnterProccessNameHere) 
{ #Write-host " $sEnterComputerNameHere"

    @($sEnterComputerNameHere) | Foreach-Object {
        $computer = $_
        Write-Host $computer        
        @($sEnterProccessNameHere) | Foreach-Object {
            $process = $_
            Write-Host $process
            try{
                $x = get-process -computername $computer #Save all processes in a variable
                If ($x.ProcessName -contains $process) #use contains instead of equals
                {
                    Write-Output "$process is running"
                }
                else
                {
                    Write-Output "$process is not running"
                }
            }
            catch
            {
                Write-Host "Computer $computer not found" -ForegroundColor Yellow
            }
        }
    }
}

$script:sEnterProccessNameHere = @("VPNUI","Notepad++","SMSS") 
$script:sEnterComputerNameHere = @("remotecomputer1","remotecomputer2") 

CheckProcess -sEnterComputerNameHere $sEnterComputerNameHere -sEnterProccessNameHere $sEnterProccessNameHere

#################################################################################


Note:- Please test script in your test environment before running in production directly.

No comments:

Post a Comment