0

I am looking for a way to query in powershell the processes that each AD user has active in a Windows Server 2019 creating an array with the users whose Enabled is "True" and iterating the search for several factors of the processes (name, Id, cpu, etc.) divided into two queries so that a too large table does not come out and is more visible. My problem is that, having the array, and the Foreach working, I can't find a way to condition the search of the processes according to the username in each step of the loop. I have tried it with an if like the one below, with where as the pipe of each query, ... but I have not been able to solve it. I would appreciate any help.

function procUsers {

    $usuariosTrue = Get-ADUser -Filter * | select Name, Enabled | where Enabled -Match True
   
    ForEach ($usuario in $usuariosTrue) {
        if (**********) {        
            Get-Process -IncludeUserName | Select-Object UserName, Id, cpu, StartTime, TotalProcessorTime | where $usuario.name -ieq UserName | sort UserName| Format-Table * | Where $usuario.name match UserName
    
            Get-WmiObject win32_process | select CommandLine, Status | sort Username | Format-Custom *        
        }            
    }    
}
Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • PLEASE, do not post images of code/errors/data. why? lookee ... Why not upload images of code/errors when asking a question? - Meta Stack Overflow — https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question – Lee_Dailey Dec 04 '20 at 11:17
  • Sorry, I think I have fixed it by editing the query. – pedro vazquez garcía Dec 04 '20 at 11:26
  • 3
    thank you for fixing that! [*grin*] ///// one thing to do is to REMOVE the `Format-*` cmdlets from your pipeline. those are for FINAL output to the display OR to a file. you have a call to `Where-Object` after one of the `Format-*` calls ... and the objects you want to work with _do not exist anymore_. ///// as for how to get the processes belonging to a username, this demos the idea >>> `Get-Process -IncludeUserName | Where-Object -Property UserName -Match $env:USERNAME` << – Lee_Dailey Dec 04 '20 at 12:48
  • To elaborate on @Lee's comment: `Format-*` cmdlets return objects whose sole purpose is to provide _formatting instructions_ to PowerShell's output-formatting system - see [this answer](https://stackoverflow.com/a/55174715/45375). In short: only ever use `Format-*` cmdlets to format data _for display_, never for subsequent _programmatic processing_. – mklement0 Dec 04 '20 at 15:15

0 Answers0