Getting specific app pool's worker process in PowerShell returns value but process is already stopped

1.2k views Asked by At

I have multiple websites - each on a separate app pool. The app pool I'm referring to has 1 worker process. After stopping the app pool, I'm trying to wait and verify that the worker process has stopped.

$appPoolName = $appPool.name;
    
    Write-Host "appPoolName: $appPoolName";

    $w3wp = Get-ChildItem "IIS:\AppPools\$appPoolName\WorkerProcesses\";

    while($w3wp -and $retrys -gt 0)
    {
        Write-Host "w3wp value is: $w3wp";

        Start-Sleep -s 10;
        $retrys--;
        
        $w3wp = Get-ChildItem "IIS:\AppPools\$appPoolName\WorkerProcesses\";

        Write-Host "w3wp value(2) is: $w3wp";

        if(-not $w3wp)
        {
            break;
        }
    }

The print of both values is always "Microsoft.IIs.PowerShell.Framework.ConfigurationElement", even when I see the process is stopped and no longer in Task Manager.

Also strange: When I open another PowerShell session while the code runs and call

 $w3wp = Get-ChildItem "IIS:\AppPools\$appPoolName\WorkerProcesses\";

w3wp has no value (because it is no longer exist).

Any ideas why the value isn't changing? Or maybe how to do that differently?

Thanks in advance :)

1

There are 1 answers

1
MisterSmith On BEST ANSWER

I think the IIS: provider is caching data. I dont know of a fix, but heres a couple of alternatives:

use WMI from powershell: gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | select AppPoolName,ProcessId

Run appcmd appcmd list wp