Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

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.

share|improve this answer
    
Got it. Thanks. –  Shlomo Oct 28 '11 at 4:26
    
Please see below if you're looking for the finished script. –  Shlomo Nov 1 '11 at 16:54
add comment

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
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.