Iis – How to force powershell to refresh cached data? Get-ChildItem results for PSDrive not current

iispowershell

Import-Module WebAdministration
$pool = "ChannelServices"
$wp = Get-ChildItem "IIS:\AppPools\$pool\WorkerProcesses"
Get-Process -Id $wp.processId
Restart-WebAppPool $pool
sleep 5
Get-ChildItem "IIS:\AppPools\$pool\WorkerProcesses"
Get-Process -Id $wp.processId

So when I do a Get-ChildItem for the WorkerProcesses of a particular AppPool, then restart that AppPool, I should get a new PID, which I do. However, rerunning Get-ChildItem still shows the old PID. If I close Powershell and reopen, it will not show the correct new PID.

How can I force Powershell to clear it's cache, or whatever is holding onto that incorrect information…

Shows how Get-ChildItem is not updating with the new PID.

UPDATE
Just to be clear, my question is how can I clear the local cache when using the IIS PSDrive.

I noticed the TypeName: Microsoft.IIs.PowerShell.Framework.ConfigurationElement#workerProcesses#workerProcess

Has a method:
Name MemberType Definition
—- ———- ———-
ClearLocalData Method void ClearLocalData()

But it hasn't really worked for me… Like someone below stated, this might just be a bug.

I see answers below on how to use WMI, which I'm aware of.

@theJasonHelmick provided a good one:

GWMI win32_process -filter "name='w3wp.exe'" | Select Name, ProcessId, @{n='AppPool';e={$_.GetOwner().user}}

That gives me the Username the AppPool is running under. I modified that a bit to pull the actual apppool name from the CommandLine:

Get-WmiObject -Class win32_process -filter "name='w3wp.exe'" | Select Name, ProcessId, @{n='AppPool';e={($_.CommandLine).Split("`"")[1]}}

Best Answer

I could reproduce this behavior on IIS 8.5, it looks like a bug to me.

Using WMI worked for me in the same PowerShell session where Get-ChildItem returned the out-dated PID

(gwmi -NS 'root\WebAdministration' -class 'WorkerProcess' | ? AppPoolName -eq $pool | Select -First 1).ProcessId

This returned the correct pid for the first WorkerProcess