IIS change applications to virtual directories in powershell

iispowershell

Trying to write a script here to do change all applications to virtual directories. I'm using Powershell, but my skills there are a bit weak. Am I using the right tool?

Here's what I have so far:

cd $env:SystemRoot\system32\inetsrv\

#Find all applications below RootAppName, convert them to virtual directories

$RootAppName = 'Default Web Site/RootApp'

./appcmd list app  | Where { [Regex]::IsMatch($_, $RootAppName + '/') } | Foreach{
            $FirstIndex = $_.IndexOf('"', 0)
            $SecondIndex = $_.IndexOf('"', $FirstIndex + 1)
            $Appname = $_.Substring($FirstIndex + 1, $SecondIndex - $FirstIndex - 1)

            $PhysicalPath = '' #Can't figure out how to get this
            $VDirPath = $Appname.Replace($RootAppName, '')

            # Need to invoke here appcmd delete app $Appname
            # Need to invoke here appcmd add vdir /app.name:$RootAppName /path:$VDirPath /physicalPath:$PhysicalPath
        }

Any ideas? Am I going about this the right way?

Best Answer

Urgh! PowerShell 2? Don't shell out to AppCMD.exe.

At the PowerShell prompt do a Import-Module WebAdministration, or just right-click on the PowerShell icon and select "Import System Modules"

Then try Get-WebApplication and Remove-WebApplication.