如何使用PowerShell 2.0对IIS 6.0删除虚拟目录

时间:2011-10-21 07:37:38

标签: iis powershell virtual-directory

我需要使用PowerShell v2.0针对IIS 6.0删除虚拟目录。我成功完成此操作的唯一方法是使用cscript vbs脚本文件使用iisvdir.vbs /delete命令。问题是我需要使用System Internals psexec工具调用PowerShell脚本,它会卡在cscript的执行上。

我尝试了以下但没有成功或错误:

$path = [ADSI]"IIS://myserver/W3SVC/1/ROOT/MyDirectory" 
$result = $path.Delete
$result = $path.Commit

这个WMI调用也没有成功: How to update existing IIS 6 Web Site using PowerShell

$tempWebsite  = gwmi -namespace "root\MicrosoftIISv2" 
                     -class "IISWebServerSetting" 
                     -filter "ServerComment like '%$name%'"
if (!($tempWebsite -eq $NULL)) {$tempWebsite.delete()}
然后我改变了:

  

IISWebServerSetting> IIsWebVirtualDirSetting

  

ServerComment> AppFriendlyName

非常感谢任何帮助!

1 个答案:

答案 0 :(得分:3)

发布一个答案,将其从“无人接听”的垃圾箱中取出。

function GetIISRoot( [string]$siteName ) {
  $iisWebSite = GetWebsite($siteName)
  new-object System.DirectoryServices.DirectoryEntry("IIS://localhost/" + $iisWebSite.Name + "/Root")
}
function GetWebsite( [string]$siteName ) {
    $iisWebSite = Get-WmiObject -Namespace 'root\MicrosoftIISv2' -Class IISWebServerSetting -Filter "ServerComment = '$siteName'"
    if(!$iisWebSite) {
        throw ("No website with the name `"$siteName`" exists on this machine")
    }
    if ($iisWebSite.Count -gt 1) {
        throw ("More than one site with the name `"$siteName`" exists on this machine")
    }
    $iisWebSite
}

function GetVirtualDirectory( [string]$siteName, [string]$vDirName ) {
  $iisWebSite = GetWebsite($siteName)
  $iisVD = "IIS://localhost/$($iisWebSite.Name)/ROOT/$vDirName"
  [adsi]$iisVD
}

function DeleteVirtualDirectory( [string]$siteName, [string]$vDirName ) {
  $iisWebSite = GetWebsite($siteName)
  $ws = $iisWebSite.Name
  $objIIS = GetIISRoot $siteName
  write-host "Checking existance of IIS://LocalHost/$ws/ROOT/$vDirName"
  if ([System.DirectoryServices.DirectoryEntry]::Exists("IIS://LocalHost/$ws/ROOT/$vDirName")) {
    write-host "Deleting Virtual Directory $vDirName at $path ..."
    $objIIS.Delete("IIsWebVirtualDir", "$vDirName")
  }
}