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...
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]}}
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
This returned the correct pid for the first WorkerProcess
If you need to refresh the cache of worker processes, you can just remove and import the Web Administration Module again.
Probably not the best solution as the one utilizing WMI, but if for some reason you don't have access to the IIS WMI objects or can't install them, this will work.
Unless I'm totally missing something, I'm guessing that you storing the first get-process in the variable
$wp
then referencing the variable again will return the old data?What if you do either of:
get-childitem
again store in$wp
(overwriting the value)?erase the variable before using it again:
remove-variable
wp
store the second
get-childitem
in a variable with a totally different name?I'm a big fan of
remove-variable
myself, as old values really can give rise to unexpected weirdnesses.[EDIT] The answer of Peter Hahndorf seems to be the alternative endorsed by Microsoft, though I have now misplaced the link for that. But as the cause of the behaviour indeed seems to be a possible bug, I'll just complement with the following:
This guy seems to have solved the problem of recycling application pools in a most efficient manner using C#. If speed is of essence (WMI is typically on the time consuming side), such as if one must do a large number of recycles in a short time frame, this could be worth looking at.
It looks not too hard to port the solution to Powershell, if one just loads the
%WinDir%\system32\Inetsrv\Microsoft.Web.Administration.dll
into the shell first to access the namespace.The original reference point for the code seems to come from Microsoft here.
Answering an old question as this is the top thing in a Google search for my problem, and does not have my solution.
I am watching a file for updates and using Get-ChildItem to get a reference to it.
I simply repeat the line above every time I want to refresh $output
While I know the file is increasing in size, $output.length would stick. It would be fine if I hit F5 in the directory.
Using $output.refresh refreshed the object without needing to re-assign it.