尝试在此处编写一个脚本来将所有应用程序更改为虚拟目录。我正在使用 Powershell,但我的技能有点弱。我使用的工具正确吗?
以下是我目前所掌握的信息:
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
}
有什么想法吗?我这样做对吗?
答案1
呃!PowerShell 2?别花钱了AppCMD.exe
。
在 PowerShell 提示符下执行Import-Module WebAdministration
或者直接右键单击 PowerShell 图标并选择“导入系统模块”
答案2
以下是使用 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
}