2011-01-26 46 views
1

我需要一個PowerShell腳本來啓用IIS6中物理文件夾上的「目錄瀏覽」。關鍵是我想修改的文件夾是另一個物理文件夾的子文件夾。這兩個文件夾都不是「虛擬目錄」。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()   
} 

回答

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()  
    } 
}