0

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 :)

Idan Levi
  • 388
  • 3
  • 18
  • I also think it is caused by the cache, you can reset the iis through the iisreset command, and then try again. right-click on cmd.exe and select run as administrator, at the command prompt, type `IISRESET`, press Enter. – samwu Apr 23 '21 at 02:59

1 Answers1

1

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

MisterSmith
  • 2,884
  • 1
  • 10
  • 13
  • It worked for me (just swiched the aliases to the actual commands: `$w3wp = Get-WmiObject -NS 'root\WebAdministration' -class 'WorkerProcess' | Select-Object AppPoolName,ProcessId | Where-Object { $_.AppPoolName -eq $appPoolName }`. Thanks! – Idan Levi Apr 25 '21 at 12:02