New features in RTW of PowerShell IIS snap-in

IIS team announced the release of PowerShell snapin at the MIX 09 conference in March. Since the last preview release we added number of features and changed format of XPath queries. In this post I will describe some of these changes.

Support of file system objects in provider.

In RTM release most of provider commands will work both on virtual namespace objects and on file system objects, located on the same path. In addition to navigation, you could now create new files and file system folders, copy those objects between file system and virtual containers, delete file system objects.

Let's take a look.

PS IIS:\Sites #> get-item test

Name ID State   Physical Path   Bindings
---- -- -----   -------------   --------
Test 7  Started c:\inetpub\test http *:809:

PS IIS:\Sites #> new-item test\folder -type directory


    Directory: WebAdministration::\\SERGEIA-64\Sites\Test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          5/8/2009  11:01 AM            folder

When we call new-item command with file system types IIS provider resolves required path to file system path and calls PowerShell file system provider. Commands copy-item, move-item, get-item, set-item, rename-item are processed the same way. Commands will return the same object types as file system provider does, therefore you could chain these commands the same way.

Main difference between file system commands and IIS provider commands is how remove-item is handled. When user applies remove-item to the virtual object, like site, application or virtual directory, only those virtual objects will be deleted, no matter which switches are added to the command. Home folders and their contents will not be touched. We decided that silent removal of file system content will be too destructive. When user calls remove-item with a path pointing to file system folder or file, those objects will be deleted.

Why did we extend IIS provider to file system? We decided that it will make user experience better. All this functionality is possible to do through file system, but first user would need to obtain file system path from the virtual path. We have cmdlet that simplifies this operation, but it still will require extra steps and mental efforts.

Additional cmdlets

Get-WebFilePath

This cmdlet will return file system object from virtual path, if it is available. When path points to virtual object, its home folder will be returned. For example, for the same site as above we could get file system path for the configuration file, passing virtual path as parameter. You could do this for any file, folder or virtual object.

PS IIS:\Sites\test #> Get-WebFilePath .\web.config


    Directory: C:\inetpub\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          5/3/2009   9:33 PM        692 web.config


PS IIS:\Sites\test #> Get-WebFilePath .


    Directory: C:\inetpub


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
d----          5/8/2009  11:01 AM            test

If you want to get path as s string, take FullName property of returned object.

PS IIS:\Sites\test #> (Get-WebFilePath .\web.config).FullName
C:\inetpub\test\web.config

Get-WebConfigFile

This cmdlet will return file system object for the nearest configuration file, defined on or above virtual path passed as parameter. Path could point to virtual object, or file system folder or file. If there is no web.config file anywhere on the path, it will return global configuration file.

PS IIS:\Sites\test #> get-webconfigfile


    Directory: C:\inetpub\test


Mode                LastWriteTime     Length Name
----                -------------     ------ ----
-a---          5/3/2009   9:33 PM        692 web.config


PS IIS:\Sites\test #> get-webconfigfile | cat
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.webServer>
      <rewrite>
        <rules>
     ...
        </rules>
      </rewrite>
    </system.webServer>
</configuration>

When I need to access global configuration file, I type

notepad (get-webconfigfile iis:\).FullName

This command is quite useful if you want to see the 'nearest' configuration file that defines something for a path.

Enjoy,

Sergei

No Comments