PowerShell :: How to filter worker processes list by User Name

我的梦境 提交于 2019-12-24 08:20:32

问题


Basically as per screen-shot there are multiple worker processes are running on machine in IIS but we need w3wp which is running under Sitecore User Username. We tried below PS script but getting blank value in User Name column

$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" 
     }
}

and finally we need to perform some action on process Id.


回答1:


I suggest the following PoSh-Script that should give you all the necessary info and more:

# 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."
}

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



来源:https://stackoverflow.com/questions/49952133/powershell-how-to-filter-worker-processes-list-by-user-name

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!