【问题标题】:How to delete a virtual directory using PowerShell 2.0 against IIS 6.0如何针对 IIS 6.0 使用 PowerShell 2.0 删除虚拟目录
【发布时间】:2024-05-02 04:45:01
【问题描述】:

我需要针对 IIS 6.0 使用 PowerShell v2.0 删除一个虚拟目录。我成功地做到这一点的唯一方法是使用cscript 命令和iisvdir.vbs /delete vbs 脚本文件。问题是我需要使用 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

任何帮助将不胜感激!

【问题讨论】:

  • 全部 - 现在已解决,我没有足够的积分来发布答案,但明天会这样做

标签: iis powershell virtual-directory


【解决方案1】:

发布答案以将其从“未回答”垃圾箱中取出。

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

【讨论】: