Your basic ITPro blog... What's going on at work, what I'm interested in.

Wednesday, June 22, 2011

IIS7 Virtual Directory Reporting

WARNING: This post is bred out of ignorance. I don’t know much at all about IIS7 administration (it’s new to us) or the IIS7 Powershell cmdlets. Everything below is a result of web searches and trial-and-error. I am completely confident that there is a better and easier way to accomplish this stuff. I would love to hear about it! Thanks!

***

I got a request for a list of our virtual directories and their ‘HTTP Redirect’ settings on our web server. We are running IIS7. I knew that there is a Powershell module for IIS7 management – named ‘WebAdministration’ – so I figured that this would be an easy one-liner… something like:

- Get-WebVirtualDirectory | Select Name, HTTPRedirectDestination

Or something similar…

But, this did not work. After looking around for a while, it looks like the WebVirtualDirectory cmdlets don’t have any capacity to report on, or modify, these settings. Further, some of my vDirs had this information stored in a web.config file in their physical path location and some did not (was it in the IIS metabase?!). I would look at the web.config files of two vDirs that looked the same in the GUI and the web.config files would have totally different settings. I was getting very confused and frustrated.

But, I need this report so I needed a plan of attack. I settled on the following:

  • Get all vDir configurations to be consistent, using a web.config file.
    • Make sure each vDir has a unique physical path.
    • Make sure each vDir has a web.config file with the proper configuration items.
  • Write a Powershell script to list vDirs and pull the redirect destination information out of the web.config file.

To that end, I ended up with the following few lines of Powershell -

$hashTable = @{}

foreach ($virtualDirectory in Get-WebVirtualDirectory)
{
$vdName = $virtualDirectory.Path.Trim('/')
$vdPath = $virtualDirectory.physicalPath
$webConfig = $vdPath + "\web.config"
$webConfigXML = [xml](Get-Content $webConfig)
$redirectDestination = $webConfigXML.configuration.FirstChild.httpRedirect.destination

$hashTable[$vdName] = $redirectDestination
}

$hashTable.GetEnumerator() | Sort Name
From here, I just Out-File the script results to a text file and send the report off. Functional, I guess.
 
*******************
 
UPDATE: I updated this script a bit to add a little more information to the output. I also changed the output object from a hashtable.
 
$vDirs = @()

foreach ($virtualDirectory in Get-WebVirtualDirectory)
{
$vdName = $virtualDirectory.Path.Trim('/')
$vdPath = $virtualDirectory.physicalPath
$webConfig = $vdPath + "\web.config"
$webConfigXML = [xml](Get-Content $webConfig)
$redirectDestination = $webConfigXML.configuration.FirstChild.httpRedirect.destination
$vdCreationDate = (Get-ChildItem $vdPath).CreationTime #.ToShortDateString()

$obj = New-Object object
Add-Member -InputObject $obj -MemberType NoteProperty -Name "Name" -Value $vdName
Add-Member -InputObject $obj -MemberType NoteProperty -Name "RedirectDestination" -Value $redirectDestination
Add-Member -InputObject $obj -MemberType NoteProperty -Name "CreationDate" -Value $vdCreationDate

$vDirs += $obj
}

$vDirs

Additional Info

My photo
email: support (AT) mangrumtech (DOT) com
mobile: 480-270-4332