appcmd pipelining to add virtual directory to each site [Answered]RSS

5 replies

Last post Mar 16, 2011 06:13 AM by itisdesign

  • appcmd pipelining to add virtual directory to each site

    Mar 13, 2011 05:01 AM|itisdesign|LINK

    I'm trying to use the pipelining feature, of IIS7's appcmd.exe, to loop each site and add a virtual directory to each.

    Please consider this example:
    appcmd list site -xml | appcmd add vdir -in /path:/test /physicalpath:c:\test /app.name:

    What should I type, after "app.name:", to access the SITE.NAME from the list command's xml?

    Thanks!,
    -Aaron

  • Re: appcmd pipelining to add virtual directory to each site

    Mar 13, 2011 08:28 PM|itisdesign|LINK

    Here are some notes..

    1. The "list site" command returns information for every IIS site.

    2. The "-xml" causes the output from "list site" to be formatted as:

    <?xml version="1.0" encoding="UTF-8"?>
    <appcmd>
        <SITE SITE.NAME="Default Web Site" SITE.ID="1" bindings="http/*:80:" state="Started" />
    </appcmd>

    Here is the related page: http://mvolo.com/blogs/serverside/archive/2007/10/06/Create-IIS7-websites-and-application-pools-fast-with-AppCmd.aspx 

    3. The "-in" allows the output from "list site" to become the input for "add vdir".

    Question: How do I pass SITE.NAME's value into /app.name parameter of "add vdir"?

    Thanks!,
    -Aaron

  • Re: appcmd pipelining to add virtual directory to each site

    Mar 14, 2011 01:41 AM|itisdesign|LINK

  • Re: appcmd pipelining to add virtual directory to each site

    Mar 15, 2011 05:15 AM|itisdesign|LINK

    Microsoft, 

    It appears the 'add vdir' command incorrectly handles the '-in' parameter.  When the '-in' parameter is used, a piped appcmd command should utilize the previous command's xml output.  Thus, an 'add vdir -in' piped after a 'list site -xml', should seemingly cause 'add vdir' to use the SITE.NAME as app.name.  However, this is currently not the case.

    Basically, this 1 line should add a virtual directory to each site, but currently does not:
    appcmd list site -xml | appcmd add vdir -in /path:/foo /physicalpath:c:\foo

    For anyone wanting to use appcmd.exe to add a virtual directory to each IIS7 site, the following batch script works:
    (note: just replace "/foo" with the desired virtual directory path, and replace "C:\foo" with the desired physical directory path)

    --------------------------------------------
    @echo off
    SetLocal EnableDelayedExpansion

    REM Use the first loop to replace all " w/ ', and remove all )
    REM Since site name is wrapped w/ " and for /f can't use " as a delimiter, replace all " w/ '
    REM Since string ends w/ ) and string will be echo'd w/in (), remove all )

    set pathToEXE=%systemroot%\system32\inetsrv\appcmd.exe
    set vdirVirtualPath=/foo
    set vdirPhysicalPath=C:\foo

    for /f "delims=" %%i in ('%pathToEXE% list sites') do (
      set lineToProcess=%%i
      set lineToProcess=!lineToProcess:"='!
      set lineToProcess=!lineToProcess:^)=!
      call :SECONDLOOP
    )
    set lineToProcess=
    goto EXIT

    :SECONDLOOP
    for /f "usebackq tokens=2 delims='" %%i in (`echo %lineToProcess%`) do (
      %pathToEXE% add vdir /path:%vdirVirtualPath% /physicalpath:%vdirPhysicalPath% /app.name:"%%i/"
    )
    set lineToProcess=

    :EXIT
    --------------------------------------------

    Is that a bug in appcmd.exe?  If so, where are appcmd.exe bugs filed?

     Thanks!,
    -Aaron Neff

    appcmd IIS 7 Virtual directory appcmd.exe

  • Re: appcmd pipelining to add virtual directory to each site

    Mar 16, 2011 05:08 AM|Leo Tang - MSFT|LINK

    Hi,

    itisdesign

    Basically, this 1 line should add a virtual directory to each site, but currently does not:
    appcmd list site -xml | appcmd add vdir -in /path:/foo /physicalpath:c:\foo

    This is by design. "IIS 7 formalizes the concepts of sites, applications, and virtual directories. Virtual directories and applications are now separate objects, and they exist in a hierarchical relationship in the IIS 7 configuration schema. Briefly, a site contains one or more applications, an application contains one or more virtual directories, and a virtual directory maps to a physical directory on a computer. "

    http://learn.iis.net/page.aspx/150/understanding-sites-applications-and-virtual-directories-on-iis-7/

    Hence, we can't add a virtual directory directly to a site. It seems you want to add virtual directory to each root application, then you can use the following command:

    appcmd list app /path:"/" /xml | appcmd add vdir /in /path:/foo /physicalpath:c:\foo

    Thanks.

    Please mark the replies as answers if they help or unmark if not.
    Feedback to us


  • Re: appcmd pipelining to add virtual directory to each site

    Mar 16, 2011 06:13 AM|itisdesign|LINK

    Hi Leo,

    Thank you very much for that explanation.  You are exactly correct - I was attempting to add a virtual directory to each root application.  The command you wrote is so powerful!  And it really helps clarify the new relationships.  In my opinion, that command would be excellent for Microsoft to use in examples.

    Thanks again very much!,
    -Aaron