Option Explicit
On Error Resume Next


Const USAGE_MESSAGE = "deleteVR.vbs sysname VirtualDirectory VirtualName SystemPath"
Const DEFAULT_VIR_DIRECTORY = "ArrayManager"
Const DEFAULT_VIR_NAME = "ArrayManager"
Const DEFAULT_VIR_PATH = "C:\Program Files\Dell\OpenManage\ArrayManager"
Const VIR_DIR_EXISTS_MSG = "Virtual directory already exists"
Const CREATING_VIR_DIR_MSG = "Creating virtual directory..."
Const ADSI_GETOBJECT_IIS_ERROR = "Error:ADSI:GetObject(IIS)"
Const ADSI_CREATE_IISWEBVIRTUALDIR_IIS_ERROR = "Error:ADSI:Create:IISWebVirtualDir(IIS)"
Const VIR_DIR_CREATE_SUCCESS = "The virtual directory was successfully created"


if Not (IsIIS) then
'	wscript.echo "IIS is not installed on this system"
	wscript.quit
end if

dim oArgs, arg
Set oArgs = Wscript.Arguments

if oArgs.Count <> 4 then 
	ShowUsage()
	wscript.quit(1)
end if


dim strSysName, strVirtualDir, strVirtualName, strSystemPath
strVirtualDir = DEFAULT_VIR_DIRECTORY
strVirtualName = DEFAULT_VIR_NAME
strSystemPath = DEFAULT_VIR_PATH

dim strAccessMode, strAuthMode, strAccessType, strDefaultDocs
strAuthMode = ""


strSysName = oArgs(0)
strVirtualDir = oArgs(1)
strVirtualName = oArgs(2)
strSystemPath = oArgs(3)

if Trim(strSysName) = "" then
	strSysName = GetSysName()
end if


Dim Root
Dim Dir

'Err = 0
Set Root = GetObject("IIS://" & strSysName & "/W3SVC/1/ROOT")
if (Err.Number <> 0) then
    wscript.Quit(2)
end if

dim o, so
for each o in Root
	if UCase(o.KeyType) = UCase("IIsWebVirtualDir") then
		if UCase(o.name) = Ucase(strVirtualDir) then
			Root.Delete "IIsWebVirtualDir", strVirtualDir 	
		end if
	end if
next

wscript.Quit(0)

Set Dir = Nothing

Set Root = Nothing

private function GetSysName()
	dim oNet
	Set oNet = CreateObject("WScript.Network")
	GetSysName = oNet.ComputerName
	Set oNet = Nothing
end function

private function IsIIS()
	IsIIS = false
	'on error resume next
	dim oIIS, service
	Err = 0
	Set oIIS = GetObject("IIS://LocalHost")
	if Err <> 0 then
		wscript.quit
	end if
	for each service in oIIS
		wscript.echo service.KeyType
		if UCase(service.KeyType) = "IISWEBSERVICE" then
			IsIIS = true
		end if
	next
end function


private sub ShowUsage()
	wscript.echo (USAGE_MESSAGE)
end sub
