首页 > 解决方案 > PowerShell 函数。Foreach + If 查询、正确顺序或更多

问题描述

我正在寻找一种在 powershell 中查询每个 AD 用户在 Windows Server 2019 中活动的进程的方法,创建一个包含 Enabled 为“True”的用户的数组,并迭代搜索进程的多个因素(名称、ID、 cpu等)分成两个查询,这样太大的表就不会出来,更显眼。我的问题是,让数组和 Foreach 工作,我无法找到一种方法来根据循环的每个步骤中的用户名来调整进程的搜索。我已经用下面的 if 进行了尝试,其中每个查询的管道是 where,......但我无法解决它。我将不胜感激任何帮助。

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

标签: functionpowershell

解决方案


推荐阅读