1

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?

2 Answers 2

1

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.

1
  • Please see below if you're looking for the finished script.
    – Shlomo
    Nov 1, 2011 at 16:54
1

Here's the script for posterity using WebAdministration:

Import-Module WebAdministration

#Find all applications below AppName in site SiteName, convert them to virtual directories

$RootAppName = 'AppName' 
$SiteName = 'SiteName'

$SitePath = 'IIS:\Sites\' + $SiteName 
cd $SitePath

dir | Where { [Regex]::IsMatch($_.Name, $RootAppName + '\\') } | Foreach {
    $AppName = $_.Name
    $PhysicalPath = $_.PhysicalPath
    Remove-WebApplication -Name $AppName -Site $SiteName 
    Write-Host 'Removing application' $AppName 'from site' $SiteName
    New-WebVirtualDirectory -Site $SiteName -Name $AppName -PhysicalPath $PhysicalPath
    Write-Host 'Adding virtual directory' $AppName 'to site' $SiteName 'at path' $PhysicalPath
}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .