Sukhjeevan
Sukhjeevan

Reputation: 3156

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

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.

enter image description here

Upvotes: 1

Views: 2613

Answers (1)

wp78de
wp78de

Reputation: 18950

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/

Upvotes: 1

Related Questions