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 *
}
}
}