我需要一个powershell脚本来在IIS6中的物理文件夹上启用“目录浏览”。关键是我想要修改的文件夹是另一个物理文件夹的子文件夹。这两个文件夹都不是“虚拟目录”。
我尝试了以下操作,但DirectoryEntry为空。我认为这是因为该文件夹不是“虚拟目录”。
$oDir = New-Object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root/Test/Upgrade")
# Loop thru all even though there should only be one...
foreach ($oDirEntry in $oDir)
{
Write-Host "Enabling Directory Browsing on IIS folder [" $oDirEntry.Name "]."
$oDirEntry.put("EnableDirBrowsing",$true)
$oDirEntry.psbase.CommitChanges()
}
答案 0 :(得分:1)
这不是有史以来最干净的代码,但我尝试了很多不同的迭代,这是有效的...
$sFolderName = "Test"
$sSubFolderName = "SubTest"
$oSubFolder = $null
# Get reference to root website
$oService = new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/W3SVC/1/Root")
foreach ($oChild in $oService.children)
{
if ($oChild.Name -eq $sFolderName)
{
# Check if we already have an IIsWebDirectory named $sSubFolderName
foreach ($oChild2 in $oChild.children)
{
if ($oChild2.Name -eq $sSubFolderName)
{
$oSubFolder = $oChild2
}
}
# Create one if it doesn't exist
if ($oSubFolder -eq $null)
{
$oSubFolder = $oChild.Children.Add($sSubFolderName, "IIsWebDirectory")
$oSubFolder.psbase.CommitChanges()
}
$oSubFolder.Put("AccessRead",$true)
$oSubFolder.Put("EnableDirBrowsing",$true)
$oSubFolder.psbase.CommitChanges()
}
}