Working with AppPools with PowerShell
PSProviders, Get-ItemProperty
Table of Contents
Summary
Import Module
Listing App Pools
Getting properties
Sub Properties
PsProperty or name Parameter
Conclusion
Summary
I was tasked to create a simple tool to get information from app pools and also tools to set different properties. Changing the settings from the IIS UI is very cumbersome and not ideal. In this post I explain how to use the Get-ItemProperty command which is used to work with the data exposed by any provider.
Import Module
First you will need to import the PowerShell module for Web Administration.
Import-Module WebAdministrationApart from loading all the cmdlets you need, importing the module creates a new PS drive called IIS.
PS C:\powershell> Get-PSDrive
Name Used (GB) Free (GB) Provider Root
---- --------- --------- -------- ----
Alias Alias
C 52.57 26.91 FileSystem C:\
Cert Certificate \
D FileSystem D:\
Env Environment
Function Function
HKCU Registry HKEY_CURRENT_USER
HKLM Registry HKEY_LOCAL_MACHINE
IIS WebAdminis... \\Server1
Variable Variable
WSMan WSManListing App Pools
To view all app pools use:
Get-ChildItem -Path iis:\apppoolsGetting properties
To get some basic properties you can use:
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy
Name State Applications
---- ----- ------------
My_Dummy Started /ServiceMy_Dummy
To see what other properties this type of object has do the following:
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy | get-member
TypeName: Microsoft.IIs.PowerShell.Framework.ConfigurationElement#system.applicationHost/applicationPools#add
Name MemberType Definition
---- ---------- ----------
Recycle CodeMethod void Recycle()
Start CodeMethod void Start()
Stop CodeMethod void Stop()
...
processModel NoteProperty Microsoft.IIs.Pow..
....
recycling NoteProperty Microsoft.IIs..
...
I have summarised the output above to just show the properties I needed. I was interested in the processModel and recycling note properties. NoteProperty is a static value.
Sub Properties
The recycling property is a collection so you have to use the ExpandProperty parameter of Select-Object. If you just state the property name using the Property parameter, PowerShell will output the following:
Get-ItemProperty -Path IIS:\AppPools\My_Dummy |
Select-Object -Property recycling
recycling
---------
Microsoft.IIs.PowerShell.Framework.ConfigurationElementSo you need to use the following:
Get-ItemProperty -Path IIS:\AppPools\My_Dummy |
select-object -ExpandProperty recyclingYou can achieve the same result with the following, which you would normally use for executing methods:
PS C:\powershell> (Get-ItemProperty -Path IIS:\AppPools\My_Dummy).recycling
disallowOverlappingRotation : False
disallowRotationOnConfigChange : False
logEventOnRecycle : Time,Memory,PrivateMemory
periodicRestart : Microsoft.IIs.PowerShell.F..
Attributes : {disallowOverlappingRotation, di..
ChildElements : {periodicRestart}
ElementTagName : recycling
Methods :
Schema : Microsoft.IIs.PowerShell.Fr...Next, from the output we can see the periodicRestart note property. To make it simple I just reference the property in the same way as before and pipe that to select-object to get just the property I need which is privateMemory.
(Get-ItemProperty -path IIS:\AppPools\My_Dummy).recycling.periodicRestart |
Select-Object -Property privatememoryYou could also do it this way, but I found this makes the line too long
Get-ItemProperty -Path IIS:\AppPools\My_Dummy |
Select-Object -ExpandProperty recycling |
Select-Object -ExpandProperty periodicrestart |
Select-Object -property privatememoryIn the end I found a better was of doing the above, I explain that in the following section using the Name parameter of Get-ItemProperty.
PsProperty or name Paramter
I accidently discovered the PSProperty parameter of Get-ItemProperty which specifies the name of the property or properties to retrieve. You can also use this to get properties from collections. This makes the line of code shorter and there is no need to pipe it to select-object. The PSProperty is actually an Alias of the Name parameter of Get-ItemProperty.
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy -PSProperty recycling
disallowOverlappingRotation : False
disallowRotationOnConfigChange : False
logEventOnRecycle : Time,Memory,PrivateMemory
....Furthermore you can select sub properties by just adding it to the property parameter using the dots. In the following example I reference the periodicRestart properties
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy -PSProperty recycling.periodicRestart
memory : 0
privateMemory : 700000
requests : 0
time : 1.05:00:00
...Finally I just needed the privatememory value:
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy -PSProperty recycling.periodicRestart |
Select-Object -Property privatememory
privateMemory
-------------
700000or this if you just want the value:
PS C:\powershell> (Get-ItemProperty -Path IIS:\AppPools\My_Dummy -PSProperty recycling.periodicRestart).privatememory
700000or using select-object -expandproperty:
PS C:\powershell> Get-ItemProperty -Path IIS:\AppPools\My_Dummy -PSProperty recycling.periodicRestart |
Select-Object -expandProperty privatememory
700000Conclusion
I was surprised that this module didn't seem to have specific commands to set different properties like I have seen with other modules. But then again the commands to work with different PS Providers make sense.
But it was a good exercise on how to use Select-Object and it's parameters as well as Get-Member. I suppose if you understand how to use Get-ChildItem , Get-ItemProperty and related commands then it makes sense.
I also discovered the name parameter to specify properties which I’ve probably used before but forgot about. The lesson I learnt is to read the whole help file for PowerShell commands before starting to use them.
I then learned about DSC for working with configurations…
PS: when setting these properties using Set-ItemProperty the property names need to be case sensitive, so for example:
Set-ItemProperty -Path "IIS:\AppPools\$name" -Name recycling.periodicRestart.privateMemory -Value $valueIn some of my examples above is simply used periodicrestart which works with Get-ItemProperty.
