Do complex IIS management tasks easily with AppCmd command piping

As much as I hate to re-post someone else's content, I've been real busy in the past few weeks and havent had a chance to post new stuff – and today I saw Kanwal's post on a very near and dear subject of mine: AppCmd command pipelining.

Things you can do by piping appcmd commands

The post covers command pipelining, the advanced AppCmd feature that enables complicated management tasks to be expressed simply by combining multiple related appcmd commands, that operate on a pipeline of results generated by prevous commands.

For example:

AppCmd list apppools /state:Stopped finds the application pools that are stopped.

AppCmd start apppool XYZ starts the specified apppool.

What if you wanted to find the stopped apppools, and start them?  You could write a script, with a FOR loop, etc – or you can use AppCmd piping to do it like a pro:

AppCmd list apppools /state:Stopped /XML | AppCmd stop apppools /in

The /XML switch in the first command outputs the result of the first command in XML, and the /IN switch of the second command allows the list of apppools to stop to come in from STDIN.

This can be applied as many times as you want with most AppCmd commands.  Since appcmd allows you to query for objects with any of its properties, you can turn it the command piping ability into a very powerful way to do complex tasks – for example:

appcmd list sites /state:started /traceFailedRequestsLogging.enabled:true /XML |
     appcmd list apps /IN /XML |
        appcmd list apppools /IN /XML | 
            appcmd recycle apppools /IN

This finds all sites that are started and have Failed Request Tracing enabled, finds their applications, then finds the apppools to which they belong, and recycles them.

Before you ask – I designed and implemented this feature before I was exposed to Powershell, and was happy to see that Powershell supports this generically for most of its commands.  If I had known that ahead of time, I may have never implemented the support in AppCmd … but as history would have it, I did, and now you get to take advantage of this capability in IIS7, without Powershell installed or the requirement to write your own powershell commandlets.

Read Kanwal's article for more examples of commands – I'll be sure to post my favorite ones, and some cool tricks using pipelining, in the near future.

9 Comments

  1. Anonymous

    As much as I hate to re-post someone else’s content, I’ve been real busy in the past few weeks and havent

  2. Anonymous

    So with IIS7, there is a new way to get data out about your web server, using the new appcmd.exe process.

  3. Anonymous

    Nice article!

    I am aware of how the powershell IIS provider can be used to create websites.

    Do you know of a similar mechanism for the powershell approach?

    – exporting XML configuration of a website definition.

    – importing XML configuration of a website definition.

  4. Anonymous

    Hi Mike,

    Thank you very much for creating the appcmd tool! It should be quite helpful.

    I seem to be having trouble adding a virtual directory to each IIS site. I thought this should work:

    appcmd list site -xml | appcmd add vdir -in /path:/mydir /physicalpath:c:mydir

    I thought SITE.NAME, from “list site”, would automatically be used as the required app.name, in “add vdir”. It is not.

    Was the task of ‘adding a virtual directory to each site’ considered, in the creation of the appcmd tool?

    If so, could you please share with me how to do this? If not, could it please be considered for the next version of appcmd? I understand you’re no longer with Microsoft, but hopefully someone is still maintaining this helpful tool.

    I’ve also posted this question to forums.iis.net: http://forums.iis.net/p/1176391/1974795.aspx#1974795

    I’ve also asked Kanwal the same question at: http://blogs.iis.net/ksingla/archive/2007/06/17/things-you-can-do-by-piping-appcmd-commands.aspx

    Thanks very much, in advance for your time!,

    -Aaron

  5. Anonymous

    Hi Mike,

    Does ‘set config’ support the ‘/in’ switch?

    As in this example:

    C:Windowssystem32inetsrvappcmd.exe list site /xml | C:Windowssystem32inetsrvappcmd.exe set config /in /section:isapiFilters /+”[name=’MyFilter’,path=’C:my_filter.dll’]”

    I’ve also asked this question in the IIS.net forums: http://forums.iis.net/p/1182209/1998492.aspx#1998492

    Thanks!,
    -Aaron

  6. chandu

    Hi,

    Can you please explain the code for Build Automation in IIS 8.5

    I have Build folder and in it has contains around 10 solutions and i want to build those solutions and host in to IIS 8.5 (10 solutions are working in IIS 6.0 and it is automated in vb script code)

    I need some solution either in c# or Powershell

    Please help me on this.

  7. Fabio

    Hello,

    I need one command to list all sites + physicalPath + LogphysicalPath + bindings + appPool.

    Can you help me?

    thanks

Leave a Reply

Your email address will not be published. Required fields are marked *