Learning IIS Powershell Provider (Step By Step)

Posted: Apr 22, 2008  6 comments

Average Rating

Tags
Powershell

 

This article is for the users who have never experienced the Windows Powershell and want to use IIS Powershell Provider.
I tried to explain the basic feature of Powershell here for you to understand the Powershell and IIS Powershell Provider together with various examples. :-)

If you don't install IIS Powershell, I'd like to recommend you to do all the IIS Powershell walkthrough.
You will install IIS Powershell provider and you will see some big picture about what you can do using IIS Powershell provider

NOTE:

1. IIS Powershell Walkthroughs:
http://learn.iis.net/page.aspx/447/managing-iis-with-the-iis-70-powershell-provider/

2. We maintain a forum in this IIS.Net web site for IIS Powershell Provider, where you can ask any questions regarding IIS Powershell:
http://forums.iis.net/1151.aspx


How to start Powershell
=======================

You can launch Powershell.exe from Start/Run menu for Windows 2008 or Start Search menu for Vista SP1.
(IIS Powershell Provider can be installed only Vista SP1 and Windows 2008, not for Vista nor Windows 2003)

Most examples will be shown with the famous Powershell prompt "PS C:\ >". So, just run the examples in order and you will understand the usage of Powershell cmdlet.
(NOTE: I tried to put some explanation with "#" mark; you don't need to type those strings)

Let's start!!!


Let's learn Powershell with what you could not in DOS world
===========================================================

#. You can create list of numbers
PS C:\> 1,2,3,4,5

Or.
PS C:\> (1..5)

#. You can create string array
PS C:\> "a","b","c"

#. You can create a variable which should be started with "$" prefix
PS C:\> $a = "My First Variable"

#. Bind your data to Powershell cmdlet using pipeline
PS C:\> 1,2,3,4,5 | write-host

#. You can echo any string with different color easily
PS C:\> 1,2,3,4,5 | write-host -ForegroundColor Green

#. You can get the actual cmdlet name of alias command
PS C:\> get-command cd

CommandType     Name                            Definition
-----------     ----                            ----------
Alias           cd                              Set-Location


PS C:\> get-command md

CommandType     Name                            Definition
-----------     ----                            ----------
Function        md                              param([string[]]$paths); New...

(NOTE:  We just learned that we can use new-item, which will be used below examples, for creating a folder)

#. You can get the help document
PS C:\> New-Item -?

Or

PS C:\> Get-help New-Item -full

#. Of course you can create directory without using pipeline
PS C:\> new-item myDir -type directory

#. You can create file with the same command with specifying different type name
PS C:\> new-item myFile -type file

#. But you can't combine string value to new-item. You will see this kind of error message
PS C:\> "myDir2" | new-item -type directory
New-Item : The input object cannot be bound because it did not contain the information required to bind all mandatory parameters:  Path
At line:1 char:17
+ "abc" | new-item  <<<< -type directory

#. Let's look at what happens. With the -full option of Help-Command you can learn the detail information of Parameter information of the New-Item cmdlet

<snip>
PARAMETERS
    -path <string[]>
        Specifies the path to the location of the new item. Wildcards are permi
        tted.

        Required?                    true
        Position?                    1
        Default value                String.empty
        Accept pipeline input?       true (ByPropertyName)
        Accept wildcard characters?  false
<snip>

(NOTE: See the Accept pipeline input? part)

Because -path has a condition of "(ByPropertyName)", that's why you could not bind the string value via pipeline operator.
Now, it is time how to create your first object data in which you add a new property with any property names

#. You can create your new object data using new-object cmdlet
PS C:\> $a = new-object psobject

(NOTE: PSObject is the built-in class in Powershell and it is good for creating powerful data)

#. You can add a new property onto an existing object using add-member cmdlet
PS C:\> add-member -inputObject $a noteproperty path "myDir2"

#. Bind your object data to Powershell cmdlets (Now you have learned how to bind object to Powershell cmdlet!!!)
PS C:\> $a | new-item -type directory

#. If you want to control the pipeline binding more programatically, use foreach cmdlet
PS C:\> $a | new-item -type directory

VS.

PS C:\> "dir1","dir2" | foreach {new-item -path $_ -type directory}

#. Even you can pipe conditionally using if statement

PS C:\> "dir1","dir2" | foreach { if ($_ -eq "dir1") {new-item -path $_ -type directory}}

#. If statement can be replaced with where-object cmdlet

PS C:\> "dir1","dir2" | foreach { if ($_ -eq "dir1") {new-item -path $_ -type directory}}

VS.

PS C:\> "dir1","dir2" | where-object {$_ -eq "dir1"} | foreach { new-item -path $_ -type directory}

#. I'd like to remind that foreach-object and where-object requires scriptblock which is a script inside of "{" and "}", not "(" and ")". The $_ means the current item in the array of items for Pipeline passes each element at a time.

PS C:\> 1,2,3 | foreach-object { echo $_}
PS C:\> 1,2,3 | where-object { $_ -eq 1}

#. where-object and foreach-object have alias which are commonly used in Powershell

PS C:\> 1,2,3 | foreach { echo $_}
PS C:\> 1,2,3 | where { $_ -eq 1}

#. Let's learn how to add method to your custom object also
PS C:\> add-member -inputObject $a scriptmethod HellowWorld {echo "hello world!!!" }

#. Let's learn how to look my object
PS C:\> $a

Or

PS C:\> $a | format-list

(NOTE: You won't see all the properties and methods)
It is important to know the Powershell don't show all the properties and methods by default.
But we can customize the output with creating some xml files.

#. Let's learn to list all the properties and methods which were hidden in the default output
PS C:\> $a | get-member


   TypeName: System.Management.Automation.PSCustomObject

Name             MemberType   Definition
----                 ----------            ----------
Equals            Method       System.Boolean Equals(Object obj)
GetHashCode  Method       System.Int32 GetHashCode()
GetType          Method       System.Type GetType()
ToString          Method       System.String ToString()
path                NoteProperty System.String path=MyDir2
HellowWord      ScriptMethod System.Object HellowWord();

#. Let's learn even the values have members
PS C:\> 1 | get-member

Or

PS C:\> "a" | get-member

Or

PS C:\> "a","b","c" | get-member


#. It's time to think about objects in Powershell world
In file systems, you have already a lot of files and directories. You must be curious which object type was used for them.

(NOTE: In Powershell, we call the class name of an object as "TYPE" because the object was actually implemented from a type of managed code like creating object in C#)

# Let's get the object type using the built-in method of gettype()
PS C:\> $a.gettype()

# Let's browse the root directory of C: drive

PS C:\> get-item *

OR

PS C:\> dir

#. Let's get an existing object for a single directory in file system using get-item cmdlet
PS C:\> get-item C:\Windows

#. Let's see which object type was used for the C:\WIndows directory object using the result of get-item cmdlet by wrapping the command execution using "(" and ")"
PS C:\> (get-item c:\Windows).gettype()

IsPublic IsSerial Name                                BaseType
--------    --------    ----                                     --------
True      True      DirectoryInfo                      System.IO.FileSys...

(NOTE: Now you know it is "DirectoryInfo")

#. Let's learn how to extract Name property only
PS C:\> (get-item c:\Windows).gettype() | Select-Object Name

OR

PS C:\> (get-item c:\Windows).gettype() | Select-Object * 

#. Let's learn the select-object is not that meaningful
PS C:\> 1 | Select-Object ???   => Not meaningful
PS C:\> 1 | get-member | select name   => This becomes meaningful

#. Let's learn the select-object actually returns a new object, not the original object
PS C:\> ((get-item c:\Windows).gettype() | Select-Object Name).gettype()

IsPublic IsSerial  Name                                     BaseType
--------    --------     ----                                          --------
True      False     PSCustomObject                    System.Object

(NOTE: the type name is "PSCustomObject", not "DirectoryInfo" anymore)

#. Let's extract the string value of Name property
PS C:\> (((get-item c:\Windows).gettype() | Select-Object Name).gettype()).Name

More tips
=========

When I started Powershell, I felt some inconvience because I was not able to do what I used to do easily in the DOS prompt.
Here is the list of examples I'd like to share with you for your information. With this tips, you will like Powershell more because you will learn the fact that you can do almost everything you did in DOS prompt.
Of course, you should read a good Powershell book to learn more skills or tools later. :-)

#. You can change the window size and back ground color of the IIS Management Console window.
1. Click the Powershell icon which is on the left-top corner and select "Properties" context menu.
2. Enable "QuickEdit Mode" checkbutton
3. Select Layout tab and adjust the width of the "windows size" group box with your favorite number. I use 120 for that.
4. Select Colors tab and set 1, 36, 86 for Red:, Green: and Blue: which is the same setting of Powershell prompt.

#. "Set" command. I was able to use "Set" command to show enviromnet variables in DOS but not in Powershell anymore.
You can use "Get-Command" instead or "Dir env:"

#. How to use environment variable in Powershell
You can use ENV: drive. Please note that you should add $ mark unlike other drive name and you should not add "\" after the drive name)
EX.)

 dir "$env:Systemdrive\windows\Microsoft.NET\Framework\v2.0.50727\config\machine.config"
 dir "$env:Systemdrive\windows\Microsoft.NET\Framework\v2.0.50727\config\web.config"
 dir "$env:Systemdrive\windows\system32\inetsrv\config\applicationHost.config"
 dir "$env:Systemdrive\inetpub\wwwroot\web.config"

#. You can run DOS command easily from Powershell if you don't know corresponding cmdlet yet
Ex.)
 cmd /c c:\windows\system32\notepad.exe
 cmd /c set

#. When you capture screen of Powershell window, you will notice the table is not aligned in your word processor or mail.
Try to use the "Courier New" font to avoid the problem

#. You can use TAB command-line completion or get-command cmdlet when you don't remember the exact cmdlet names.
If you remember part of the cmdlet name, then try to use get-command. For the example, get-command *item* will return all the cmdlemt which contains "item" in their name.

#. Use "Get-Comand *keyword* when you forget the name of a certain command name.
Ex.) "Get-Command *web*" will return all the cmdlets which name contains web

#. Use "Get-Help <cmdletname> -full" as much as possible.
For the first time, I thought it provides too much information but I realize it is not that too much information soon. Now I complains the help document is not enought for many times.

#. Use "Get-Help <cmdletname> -full" to get the related cmdlets
It is very important to enlarge your knowledge about each cmdlet. The get-help cmdlet will be your friend to let you know all the insteresting cmdlet which is directly related to the specified cmdlet.

#. Please note, the Tech Preview 1 IIS Powershell release does not have enough information for each cmdlet which you can apply for IIS: drive and its contents such as sites/applications/virtual directoriesa and apppools.
We couldn't find the solution yet. Please use IIS.Net forum to get any kind of questions because you feel the help does not that helpful for your situation.


What does it mean to install IIS Powershell Provider?
=====================================================

Powershell.EXE has built-in providers such as registry key provider.
You can get the list of the providers which are installed in your machine with Get-PsProvider cmdlet.

With IIS provider, you can read files and directories under web contents such as web sites/appplications/virtual directories and you can browse physical files and directories as well.
IIS Provider is built as one single DLL file, which name is iisprovider.dll.
When you install Powershell provider, the dll file is copied to GAC directory and the dll file is registered to Powershell environment.
Even the dll is registered, you should know that whenever you open a Powershell window, which is powershell.exe shell, you need to add the registered provider to your newly opened shell environment.

#. How to add IIS Provider

1. Execute Powershell.exe to open a new powershell window
2. Run "add-pssnapin IIsProviderSnapIn"

NOTE: Inside of IISProvider.dll, the snapin name is decided as "IIsProviderSnapIn". Snapin is another name of Powershell dll file. IISProvider.dll has the IIsProviderSnapIn snapin where IIS Provider and IIS cmdlet are implemented.
Sometimes a single snapin can contain only custom cmdlets or only custom provider without custom cmdlets.


What kinds of objects are supported in IIS Powershell Provider?
===============================================================

After installing IIS Powershell provider, you can manage all the IIS configuration system objects. So, it is important to know which kind of objects you can manipulate with IIS Powershell provider.

#. All the items under the IIS: drive are ConfigurationElement except container nodes such as IIS:\, IIS:\Sites and IIS:\AppPools
For the example, let's see the type of "Default Web Site" and "DefaultAppPool" and they were made from the type ConfigurationElement object.
PS C:\> (get-item 'iis:\sites\default web site').gettype()

IsPublic IsSerial Name                                     BaseType
--------    --------    ----                                          --------
True      False    ConfigurationElement               System.Object

PS C:\> (get-item 'iis:\apppools\defaultapppool').gettype()

IsPublic IsSerial Name                                     BaseType
--------     --------    ----                                         --------
True       False    ConfigurationElement              System.Object

#. What's the type name of container node?
For the example, let's see the type of IIS:\Sites node; it is "PSCustomObject" and it is the same for AppPools container node and the root IIS:\ as well.
PS C:\> (get-item 'IIS:\Sites').gettype()

IsPublic IsSerial Name                                     BaseType
--------    --------     ----                                         --------
True      False    PSCustomObject                     System.Object

#. Let's look at all the methods and properties of each IIS node such as Default Web Site
PS C:\> get-item 'IIS:\Sites\Default Web Site'

Name                 ID   State      Physical Path                               Bindings
----                      --   -----      -------------                                          --------
Default Web Site 1    Started    %SystemDrive%\inetpub\wwwroot  http *:80:

(NOTE: As I told before, it does not show all the properties or methods)

#. Let's use Get-Member instead to see all the methods and properties
PS C:\> (get-item 'IIS:\Sites\Default Web Site') | get-member

<snip>
applicationDefaults        NoteProperty          System.Management.Automatio...
applicationPool            NoteProperty          System.String applicationPo...
bindings                   NoteProperty          System.Management.Automatio...
enabledProtocols           NoteProperty          System.String enabledProtoc...
id                         NoteProperty          System.Int64 id=1
limits                     NoteProperty          System.Management.Automatio...
logFile                    NoteProperty          System.Management.Automatio...
name                       NoteProperty          System.String name=Default ...
password                   NoteProperty          System.String password=
physicalPath               NoteProperty          System.String physicalPath=...
PSChildName                NoteProperty          System.String PSChildName=D...
PSDrive                    NoteProperty          Microsoft.IIs.PowerShell.Pr...
PSIsContainer              NoteProperty          System.Boolean PSIsContaine...
PSParentPath               NoteProperty          System.String PSParentPath=...
PSPath                     NoteProperty          System.String PSPath=IIsPro...
PSProvider                 NoteProperty          Microsoft.IIs.PowerShell.Pr...
serverAutoStart            NoteProperty          System.Boolean serverAutoSt...
traceFailedRequestsLogging NoteProperty          System.Management.Automatio...
userName                   NoteProperty          System.String userName=
virtualDirectoryDefaults   NoteProperty          System.Management.Automatio...
As I explained, Let's look at all the method and properties of each IIS node such as Default Web Site
<snip>

(NOTE: I have shown only NoteProperty properties here on purpose because they are actually used to configure IIS configurations)

#. What is the type of each property then?
For the example, let's see what is it for the Binding property of Default Web Site
PS C:\> ((get-item 'IIS:\Sites\Default Web Site').Bindings).gettype()

IsPublic IsSerial  Name                                     BaseType
--------     --------    ----                                          --------
True       False    ConfigurationElement               System.Object

(NOTE: It is "ConfigurationElement" again)

#. Why all the existing object in the IIS Powershell provider are instantiated from the same type?
The answer is because IIS Provider is built on top of MWA (Microsoft Web Administration) which is used by IIS UI. The MWA provides unique object hierachy for all the configurable IIS properties and items.
TP1 IIS Powershell provider utilize it and that's why we see the same object type from all the existing IIS Powershell world objects.
Now, let's learn about the MWA object hierachy more.

#. How to get the value of physicalPath property of Default Web Site
PS C:\> (get-item 'IIS:\Sites\Default Web Site').physicalPath
%SystemDrive%\inetpub\wwwroot

(NOTE: it returns string value and it is because the physicalPath is not a ConfigurationElement object)

#. How to get the value of Bindings property of Default Web Site
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings
Collection      : {binding}
Attributes      : {}
ChildElements   : {}
ElementTagName  : bindings
IsLocallyStored :
Methods         :
RawAttributes   : {}
Schema          : Microsoft.Web.Administration.ConfigurationElementSchema

(NOTE:  BTW, Bindings property is shown differently considering the format of what we see for the Default Web Site which was from the same object type. It is becasue we have maintain some xml files to handle sites node)

#. How to get the properties values inside of Bindings object then?
We should follow the same way. First look at all the properties and methods of the object

PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings | get-member

<snip>
Collection          NoteProperty          System.Management.Automation.PSObj...
<snip>

(NOTE: In the same way, I showed only NoteProperty properties here because they are only configurable items in the IIS Powershell Provider world)

# Let's see the type of the Collection object again

PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection.gettype()

IsPublic IsSerial Name                                BaseType
--------    --------    ----                                     --------
True      True      PSObject[]                         System.Array

(NOTE: it is PSObject array,not a single PSObject object and now it is time to learn how to manipulate array object in the Powershell world)

# How to know the number of items in a certain array object?
Answer is to use Length or Count property
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection.length
1
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection.count
1

# What is the type of each entry of the array object? (You should use "[" and "]" operatoer to specify the index of array here)
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection[0].gettype()

IsPublic IsSerial Name                                BaseType
--------    --------    ----                                     --------
True      False    ConfigurationElement          System.Object

We get "ConfigurationElement" again!!! Let's see all the methods and properties of the 1st element.

PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection[0] | get-member

<snip>
bindingInformation  NoteProperty          System.String bindingInformation=*...
protocol            NoteProperty          System.String protocol=http
<snip>

(NOTE: I show only NoteProperty properties again because we can use them for modifying configuration)

# Now, let's try to get the value of bindingInformation and protocol
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection[0].bindingInformation
*:80:
PS C:\> (get-item 'IIS:\Sites\Default Web Site').Bindings.collection[0].protocol
http

Finally we reached the final leaf node, string values!!! It is a long journey right? But you can apply the same rule for all the IIS properites and you will realize this is not that complicated job soon.


Let's learn about the Filter parameter
======================================

NOTE: Filter is the 1st parameter for most of IIS cmdlets

#. Learn the fact that some Cmdlets accept XFilter parameter as well as PSPATH.
XFilter is used to specify a certain section element. In order to understand the IIS sections, you should understand the IIS configuration architecture.
In my opinion, the best way to learn the section is to use the Configration Editor of Admin Pack, which is available in the web site, www.IIS.Net.
If you go to the Configuration Editor page of Inetmgr.exe, you will see "Section:" dropdown control in UI. It shows the list of IIS Sections and you can expand them to see whole config section informations.
Because IIS Powershell provider uses the same engine on which the Configuraion Editor is based to manipulate the config sections, if you fully understand the configuration editor, you can use the IIS Powershell provider more effectively.
So, if you don't know well about IIS section informations, please try to use the configuration editor and you will learn how the IIS config system should be managed.
And each IIS Config sections should be mapped to a certain commit path.
In IIS Powershell provider, the commit path is actually considered as PSPATH, which is explained below again.
I will explain about this more with sample scripts below.


Let's learn what the PSPath means in IIS Powershell provider world
==================================================================

NOTE: PSPATH is the second parameter for most of IIS cmdlets

#. There are two types of PSPATH and they are 100% equivalent each other
If you have experienced to use AppCmd.exe, it would make sense to you. If not, it just looks complicated.
I will explain why there are two types of PSPATH here.

Type1: 'MACHINE/WEBROOT/APPHOST/iisstart.htm'
Type2: 'iis:\Sites\Default Web Site\iisstart.htm'
Type3: '..\Default Web Site' Or "."

The Type1 was used first for AppCmd.exe and the MWA world. IIS Powershell provider was invented at last. So, it had to use the same format for users.
The Type2 is a newly introduced one with IIS Powershell. It looks very similar to the File system path and most of users will like it because it is easy to use.
If Type2 is an absolute path, the Type3 is non-absolute path. You can use that kind of relative path as you do for DOS directory path.

#. For a certain case, we must use the Type1 PSPATH.
It is the case when you want to manipulate the IIS configuration settings in the web.config under %windir%\Windows\Microsoft.NET\Framework\v2.0.50727\CONFIG directory.
And the value for the PSPATH parameter should be MACHINE/WEBROOT.


Location Parameter and Metadata Parameter
=========================================

NOTE: Location and Metadata are optional parameters for most of IIS cmdlets

Finally, you need to know understand IIS config files can be configured using "Location" tag.
By default, Configuration sections will be saved without using Location information.
If you want to manipulate sections under location path and some locking attributes, you should use the location parameter and the metadata parameter as well.
I will explain about this more with sample scripts below.

#. Filter parameter value samples

1) //*    => All the elements in IIS Configuration XML system
2) //*[@attributename]  => All the elements which contains a certain attribute

#. Filter parameter Examples

Ex.1) In order to dump all the sections
PS C:\> get-webconfiguration //* machine/webroot/apphost | where {$_.gettype().tostring() -eq "Microsoft.Web.Administration.ConfigurationSection"} | select SectionPath | sort SectionPath

FYI, IIS Config system has three types and those types has this relationship:
Microsoft.Web.Administration.SectionGroup  > Microsoft.Web.Administration.ConfigurationSection > Microsoft.Web.Administration.ConfigurationEntry

Ex.2) Dump all the sections which contains enabled attribute
PS C:\> get-webconfiguration //*[@enabled] machine/webroot/apphost

Ex.3) Dump all the sections which contains name attribute and its value should be "Default Web Site"
PS C:\> get-webconfiguration '//*[@name="Default Web Site"]' machine/webroot/apphost  

Ex.4) Dump all the sections for Default Web Site using absolute path
PS C:\> get-webconfiguration '/system.applicationHost/sites/site[@name="Default Web Site" and @id=1]' machine/webroot/apphost

Ex.5) Dump all the sections for Default Web Site using "//"
PS C:\> get-webconfiguration '//site[@name="Default Web Site" and @id=1]' machine/webroot/apphost

#. Location, Metadata parameter Samples

1) Clear appSettings section under "iisstart.htm" location section
PS C:\> clear-webconfiguration /appSettings 'iis:\sites\default web site' -location iisstart.htm

2) Add an appsetting key under "iisstart.htm" location section and set overrideMode attribute with "Deny"
PS C:\> add-webconfiguration /appSettings 'iis:\sites\default web site' -value @{key="test";vale="test"} -at 0  -location iisstart.htm
PS C:\> set-webconfiguration /appSettings 'iis:\sites\default web site' -location iisstart.htm -metadata overrideMode -value Deny

3) Get the appSettings section under iisstart.htm; By this way, you can get the current metadata property values for the section as well
PS C:\> get-webconfiguration /appSettings 'iis:\sites\default web site' -location iisstart.htm | select *

#. Advanced Filter parameter Examples (FYI, I've learned this from William Moy)

Ex.1) Let's prepare the work environment
PS C:\> add-webconfiguration /appSettings 'iis:\sites\default web site' -value @{key="test";value="test"} -at 0
PS C:\> add-webconfiguration /appSettings 'iis:\sites\default web site' -value @{key="test2";value="test2"} -at 0

Ex. 2) You can get two "add" element nodes
PS C:\> get-webconfiguration /appSettings/add 'iis:\sites\default web site'

Ex. 3) You can get use [] to specify the index inside of XPATH like this and you will get 1st add element
PS C:\> get-webconfiguration /appSettings/add[0] 'iis:\sites\default web site'

Ex. 4) You can get use [@attributename="value"] to meet the specific attribute value
PS C:\> get-webconfiguration '/appSettings/add[@key="test"]' 'iis:\sites\default web site'

Ex. 5) You can even use != operator instead of "=" and you will get the other "add" element node
PS C:\> get-webconfiguration '/appSettings/add[@key!="test"]' 'iis:\sites\default web site'

Ex. 6) You can even get each property by specifying @attribute after the section path
PS IIS:\> get-webconfiguration /appSettings/add/@key 'iis:\sites\default web site' | select *

Ex. 7) You can even get all the attributes using @*
PS IIS:\> get-webconfiguration /appSettings/add/@* 'iis:\sites\default web site' | select *

Ex. 8) You can get all "site" elements with a virtual directory that starts with "C:\" string
PS C:\> get-webconfiguration "/system.applicationHost/sites/site[application/virtualDirectory[starts-with(string(@physicalPath),'C:\')]]" IIS:\

Ex. 9) You can get all "application" elements with a virtual directory that starts with "C:\" string by adjusting the location of "["
PS C:\> get-webconfiguration "/system.applicationHost/sites/site/application[virtualDirectory[starts-with(string(@physicalPath),'C:\')]]" IIS:\

Ex. 10) Get Image path information of StaticFileModule
PS C:\> get-webconfiguration "/system.webServer/globalModules/add[@name='StaticFileModule']/@image" IIS:\
%windir%\System32\inetsrv\static.dll 

Ex. 11) You can get all the "add" elements which contains 'Authentication' in its name property that comes before WindowsAuthentication or BasicAuthenticationModule items
PS C:\> get-webconfiguration '/system.webServer/modules/add[@name="WindowsAuthentication"]//preceding-sibling::*[contains(string(@name),"Authentication")]' IIS:\ 
OR 
PS C:\> get-webconfiguration '/system.webServer/modules/add[@name="BasicAuthenticationModule"]//preceding-sibling::*[contains(string(@name),"Authentication")]' IIS:\ 

Ex. 12) You can get all the "application" elements which contains cerain application pools. The application pools were obtained with a nested xpath query.
PS C:\> get-webconfiguration '/system.applicationHost/sites/site/application[@applicationPool=/system.applicationHost/applicationPools/add[@managedPipelineMode="0"]/@name]' IIS:\


Let's look at which kind of Powershell cmdlets exist
====================================================

Let's categorize all the cmdlets using required parameters first.
FYI, I am going to write another blog with the detail information for each cmdlet later.

#. PSPATH only
Get-item
New-Item
Set-Item
Copy-Item
Move-Item
Remove-Item
Rename-Item
Clear-Item
Get-WetItemState
Start-WebItem
Stop-WebItem

#. Filter + PSPATH
Get-WebConfigurationItem
Set-WebConfigurationItem
Add-WebConfigurationItem
Clear-WebConfigurationItem
Get-WebConfigurationItemProperty
Set-WebConfigurationItemProperty
Add-WebConfigurationItemProperty
Remove-WebConfigurationItemProperty

#. Nothing
Start-Transaction
End-Transaction

Now, let's categorize the common cmdlet names which are applicable to other provider such as File system and Registry key providers

#. Cmdlets only applicable only to IIS world
Get-WetItemState
Start-WebItem
Stop-WebItem
Get-WebConfigurationItem
Set-WebConfigurationItem
Add-WebConfigurationItem
Clear-WebConfigurationItem
Get-WebConfigurationItemProperty
Set-WebConfigurationItemProperty
Add-WebConfigurationItemProperty
Remove-WebConfigurationItemProperty

#. Cmdlets which can be used for other providers
Get-item
New-Item
Set-Item
Copy-Item
Move-Item
Remove-Item
Rename-Item
Clear-Item


References
==========

1) Learn more XPATH examples:
 http://msdn2.microsoft.com/en-us/library/ms256086.aspx

2) Learn more Powershell programming knowledge by following Powershell SDK walkthrough:
 http://msdn2.microsoft.com/en-us/library/ms714469.aspx

3) Powershell Home page. You can get more useful information here.
http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx

Comments

  1. bills
    April 23, 2008

    welcome to blogosphere JeongHwan!  This is a very comprehensive first post on Powershell!  Thanks for contributing to the community!

  2. launch » Blog Archive » Learning IIS Powershell Provider (Step By Step)
    April 23, 2008

    Pingback from  launch  &raquo; Blog Archive   &raquo; Learning IIS Powershell Provider (Step By Step)

  3. ma_khan
    April 23, 2008

    This is a biiiiiiig article... but very good for learning...

    The way you used web configuration was good... will remember that...

  4. Shakeel Ahmad
    April 28, 2008

    Dear Sir/Madam,

    I don't have the PayPal account due to my country name not existed in the list to sign up. So, what would you suggest in this regard, please?

    Best Regards,

    Shakeel Ahmad,

    Islamabad (Pakistan)

  5. IIS PowerShell Provider - step-by-step at IIS Digest
    May 26, 2008

    Pingback from  IIS PowerShell Provider - step-by-step at IIS Digest

  6. Anonymous
    July 27, 2008

    qZZDsS  <a href="ebyutfpuuemi.com/.../a>, [url=http://vobehypogubs.com/]vobehypogubs[/url], [link=http://gvfczsckhmfg.com/]gvfczsckhmfg[/link], http://tpvgteiacpvj.com/

Page view counter