''''''''''''''''''''''''''''''''''''
'
' computil.VBS
'
' Date:   05/09/11
' Bishop

Option Explicit
On Error Resume Next

''''''''''''''''''
' Main Script Code
''''''''''''''''''
Dim ArgObj ' Object which contains the command line argument
Dim Result ' Result of the command function call
Dim Args(999) ' Array that contains all of the non-global arguments
Dim ArgCount ' Tracks the size of the Args array

' Used for string formatting
Dim Spacer
Dim SpacerSize

Const GENERAL_FAILURE = 2
Const GENERAL_WARNING = 1
Const AppCreate_InProc = 0
Const AppCreate_OutOfProc = 1
Const AppCreate_PooledOutOfProc = 2

Const APPSTATUS_NOTDEFINED = 2
Const APPSTATUS_RUNNING = 1
Const APPSTATUS_STOPPED = 0



Spacer = "                                " ' Used to format the strings
SpacerSize = Len(Spacer)

' Note: The default execution mode may be under WScript.exe.
' That would be very annoying since WScript has popups for Echo.
' So, I want to detect that, and warn the user that it may cause
' problems.
DetectExeType

' Get the Arguments object
Set ArgObj = WScript.Arguments

' Test to make sure there is at least one command line arg - the command
If ArgObj.Count < 1 Then
        WScript.Quit (GENERAL_FAILURE)
End If

Dim I
For I = 0 To ArgObj.Count - 1
        Args(ArgCount) = ArgObj.Item(I)
        ArgCount = ArgCount + 1
Next


' Call the function associated with the given command
Select Case UCase(Args(0))

    Case "DELETE"
        Result = DeleteCommand()
    Case "ENUM"
    	Result = EnumCommand()
    Case Else
        WScript.Echo "Command not recognized: " & Args(0)
        Result = GENERAL_FAILURE

End Select

WScript.Quit (Result)

''''''''''
' End Main
''''''''''

Function DeleteCommand()

    On Error Resume Next


    Dim ObjectName
	Dim ComAdminCat
	Dim CatCollection
	Dim iComCount
	Dim iComIndex
	Dim comObj
	
    ' Set the return code - assume success
    DeleteCommand = 0

    ' Setup the parameters
    If (ArgCount <> 2) Then
        WScript.Echo "Error: Wrong number of Args for the DELETE command"
        WScript.Quit (GENERAL_FAILURE)
    End If

    ObjectName = Args(1)
	
	Set ComAdminCat = CreateObject("COMAdmin.COMAdminCatalog")
    If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error Create ComAdmin"
        WScript.Quit (Err.Number)
    End If

	Set CatCollection = ComAdminCat.GetCollection("Applications")
	If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error ComAdminCat.GetCollection"
        WScript.Quit (Err.Number)
    End If
    
    CatCollection.Populate
	iComCount = CatCollection.Count
	
	
	For iComIndex = 0 To iComCount-1
		'Wscript.Echo iComIndex
        Set comObj = CatCollection.Item(iComIndex)
        
        
        If comObj.Name = ObjectName Then
        	CatCollection.remove iComIndex
        	Exit For
        End if
    Next
    
    CatCollection.SaveChanges
     
    If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error Delete Com+:" & ObjectName
        WScript.Quit (Err.Number)
    End If

    If iComIndex = iComCount Then 
    	WScript.Echo
        WScript.Echo "Can't Find Com+:" & ObjectName
        DeleteCommand = 1
    Else
    	WScript.Echo "deleted Com+:" & ObjectName
    End If
	
	
    Exit Function

End Function

Function EnumCommand()

    On Error Resume Next


    Dim ObjectName
	Dim ComAdminCat
	Dim CatCollection
	Dim iComCount
	Dim iComIndex
	Dim comObj
	
    ' Set the return code - assume success
    EnumCommand = 0

    ' Setup the parameters
    If (ArgCount <> 1) Then
        WScript.Echo "Error: Wrong number of Args for the DELETE command"
        WScript.Quit (GENERAL_FAILURE)
    End If
	
	Set ComAdminCat = CreateObject("COMAdmin.COMAdminCatalog")
    If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error Create ComAdmin"
        WScript.Quit (Err.Number)
    End If

	Set CatCollection = ComAdminCat.GetCollection("Applications")
	If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error ComAdminCat.GetCollection"
        WScript.Quit (Err.Number)
    End If
    
	CatCollection.Populate
	iComCount = CatCollection.Count
	
	For iComIndex = 0 To iComCount-1
		
        Set comObj = CatCollection.Item(iComIndex)
        
        Wscript.Echo iComIndex & ":" & comObj.Name
    Next
    

     
    If Err.Number <> 0 Then
        WScript.Echo
        ReportError ()
        WScript.Echo "Error Delete Com+:" & ObjectName
        WScript.Quit (Err.Number)
    End If


	
    Exit Function

End Function

Sub ReportError()
'   On Error Resume Next

    WScript.Echo "ErrNumber: " & Err.Number & " (0x" & Hex(Err.Number) & ")"
End Sub


'''''''''''''''''''''''''''
'
' DetectExeType
'
' This can detect the type of exe the
' script is running under and warns the
' user of the popups.
'
'''''''''''''''''''''''''''
Sub DetectExeType()
    Dim ScriptHost
    Dim ShellObject

    Dim CurrentPathExt
    Dim EnvObject

    Dim RegCScript
    Dim RegPopupType ' This is used to set the pop-up box flags.
                                            ' I couldn't find the pre-defined names
    RegPopupType = 32 + 4

    On Error Resume Next

    ScriptHost = WScript.FullName
    ScriptHost = Right(ScriptHost, Len(ScriptHost) - InStrRev(ScriptHost, "\"))

    If (UCase(ScriptHost) = "WSCRIPT.EXE") Then
        WScript.Echo ("This script does not work with WScript.")

        ' Create a pop-up box and ask if they want to register cscript as the default host.
        Set ShellObject = WScript.CreateObject("WScript.Shell")
        ' -1 is the time to wait.  0 means wait forever.
        RegCScript = ShellObject.PopUp("Would you like to register CScript as your default host for VBscript?", 0, "Register CScript", RegPopupType)

        If (Err.Number <> 0) Then
            ReportError ()
            WScript.Echo "To run this script using CScript, type: ""CScript.exe " & WScript.ScriptName & """"
            WScript.Quit (GENERAL_FAILURE)
            WScript.Quit (Err.Number)
        End If

        ' Check to see if the user pressed yes or no.  Yes is 6, no is 7
        If (RegCScript = 6) Then
            ShellObject.RegWrite "HKEY_CLASSES_ROOT\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
            ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SOFTWARE\Classes\VBSFile\Shell\Open\Command\", "%WINDIR%\System32\CScript.exe //nologo ""%1"" %*", "REG_EXPAND_SZ"
            ' Check if PathExt already existed
            CurrentPathExt = ShellObject.RegRead("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT")
            If Err.Number = &H80070002 Then
                Err.Clear
                Set EnvObject = ShellObject.Environment("PROCESS")
                CurrentPathExt = EnvObject.Item("PATHEXT")
            End If

            ShellObject.RegWrite "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment\PATHEXT", CurrentPathExt & ";.VBS", "REG_SZ"

            If (Err.Number <> 0) Then
                ReportError ()
                WScript.Echo "Error Trying to write the registry settings!"
                WScript.Quit (Err.Number)
            Else
                WScript.Echo "Successfully registered CScript"
            End If
        Else
            WScript.Echo "To run this script type: ""CScript.Exe adsutil.vbs <cmd> <params>"""
        End If

        Dim ProcString
        Dim ArgIndex
        Dim ArgObj
        Dim Result

        ProcString = "Cscript //nologo " & WScript.ScriptFullName

        Set ArgObj = WScript.Arguments

        For ArgIndex = 0 To ArgCount - 1
            ProcString = ProcString & " " & Args(ArgIndex)
        Next

        'Now, run the original executable under CScript.exe
        Result = ShellObject.Run(ProcString, 0, True)

        WScript.Quit (Result)
    End If

End Sub