PowerShell ::如何按用户名筛选工作进程列表

时间:2018-04-21 03:38:39

标签: powershell iis-8

基本上按照屏幕截图,IIS中的计算机上运行了多个工作进程,但我们需要在 Sitecore用户用户名下运行的w3wp。 我们尝试了以下PS脚本,但在用户名列中获取空白值

$processlist  = get-process  | where {$_.cpu -gt 5 -and $_.Name -eq 'w3wp'} |select Name, @{l="User name";e={$_.getowner().user}} | ft -AutoSize
foreach($proc in $processlist){
if($proc -eq "Sitecore User" ){
     C:\Users\Administrator\Documents\someexe.exe $proc.Id "C:\Users\Administrator\Documents\output.dmp" 
     }
}

最后我们需要对进程ID执行一些操作。

enter image description here

1 个答案:

答案 0 :(得分:1)

我建议使用以下PoSh-Script,它应该为您提供所有必要的信息以及更多信息:

# Ensure to import the WebAdministration module
Import-Module WebAdministration

# Declare the variables
$server = "localhost"
$search = "*"

$wmiQuery=[wmisearcher]"SELECT * FROM __Namespace where NAME like 'WebAdministration' or NAME like 'MicrosoftIISv2'"
$wmiQuery.Scope.Path = "\\" + $server + "\root"
$WebNamespace = $wmiQuery.Get()

# Checking if the the server has IIS installed
if($WebNamespace -like '*WebAdministration*')
{
    "IIS  found on $server"
    $WPlist=Get-WmiObject -NameSpace 'root\WebAdministration' -class 'WorkerProcess' -ComputerName 'LocalHost'
    # Loop through the list of active IIS Worker Processes w3wp.exe and fetch the PID, AppPool Name and the startTime

    forEach ($WP in $WPlist)
    {
        if ($WP.apppoolname -like$search)
        {
           write-host "Found:""PID:"$WP.processid  "AppPool_Name:"$WP.apppoolname
           (get-process -ID $WP.processid|select starttime)
        }
    }
}
Else
{
   write-host"WARNING: IIS not detected."
}

参考:https://blogs.msdn.microsoft.com/webtopics/2015/11/28/query-the-active-worker-process-information-in-iis-7-x-using-powershell/