David
David

Reputation: 135

How to delete a virtual directory using PowerShell 2.0 against IIS 6.0

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!

Upvotes: 2

Views: 2841

Answers (1)

JonnyG
JonnyG

Reputation: 759

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

Upvotes: 3

Related Questions