Powershell Get-Process behavior - Can't find process under certain conditions

78 views Asked by At

I am working on a powershell script that looks for a defined process in order to stop it (and restart it, if it was running before) in order to perform an update of that software.

We use the Microsoft System Center Configuration Manager to perform that task.

Running the Powershell directly with user rights with the user accounts, finds the process. Running the Powershell directly script with admin rights with the user account finds the porcess. Running the Powershell via SCCM with user rights with the user accounts, does NOT find the process.

Here is a snippet that we use for debugging:

$fileName = "MyFile.exe"
$processes = Get-Process

foreach ($process in $processes) {
    
    # Process found?
    if ($process.MainModule.FileName -like "*\$fileName") {
        Write-Host "Process found" -ForegroundColor Green
    }
    else{
        Write-Host "Not the right process" $process.MainModule.FileName -ForegroundColor Red
    }
}

Any ideas?

1

There are 1 answers

2
DominikAmon On

Based on @mklement0 I created a workaround:

$taskListOutput = & tasklist /fo csv /FI "imagename eq $fileName"

$splitTaskListOutput = $taskListOutput -split "`r`n"
if($splitTaskListOutput.Count -gt 1)
{
    Write-Host "Process found"
     try {      
        $columns = $splitTaskListOutput[1].Trim('"') -split '","'
        # Stop by process ID
        Stop-Process -Id $columns[1]
        Write-Host "Process terminated"
     } catch {
        return -1 # Process could not be terminated
    }
}

The actual reason why the process can not be found in powershell by SCCM remains a mystery.