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

发布于 2024-12-11 09:52:56 字数 972 浏览 2 评论 0原文

我需要使用 PowerShell v2.0 针对 IIS 6.0 删除虚拟目录。我成功做到这一点的唯一方法是使用 cscript 命令和 iisvdir.vbs /delete vbs 脚本文件。问题是我需要使用系统内部 psexec 工具调用 PowerShell 脚本,但它在执行 cscript 时卡住了。

我已尝试以下操作,但没有成功或错误:

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

并且此 WMI 调用也没有成功: 如何使用 PowerShell 更新现有 IIS 6 网站

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

然后我改变了:

IISWeb服务器设置> IIsWebVirtualDirSetting

服务器评论>应用程序友好名称

任何帮助将不胜感激!

I need to delete a virtual directory using PowerShell v2.0 against IIS 6.0. The only way I have managed to do this successfully is to use the cscript command using the iisvdir.vbs /delete vbs script file. The problem is I need to call the PowerShell script using System Internals psexec tool and it gets stuck on the execution of the cscript.

I have tried the following without success or an error:

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

And this WMI call also without success:
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()}

I then changed:

IISWebServerSetting > IIsWebVirtualDirSetting

And

ServerComment > AppFriendlyName

Any help would be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我要还你自由 2024-12-18 09:52:56

发布答案以将其从“未答复”箱中删除。

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

Posting an answer to get this out of the "unanswered" bin.

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")
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文