On Error Resume Next
 
'+-------------------------------------+
'|                                     |
'|             PRE-INSTALL             |
'|                                     |
'+-------------------------------------+
Dim bErrorFound
bErrorFound = False	
Dim iPackageExitCode
iPackageExitCode = 0
Dim sErrorMsg
sErrorMsg = ""

bCriteriaCheck=True

'Prechek if SSLVP is on by detecting all network adapters and then searching for the Cisco adapter to see if it's enabled and active

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & sComputerName & "\root\cimv2")
Set colAdapterCfgs = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True")

For Each objAdapterCfg In colAdapterCfgs
	Set objAdapter = objWMIService.Get ("Win32_NetworkAdapter.DeviceID=" & objAdapterCfg.Index)
	If Instr(1,cstr(objAdapter.Caption),"Cisco") > 0 then 
		EmersonLog "Cisco network Adapter detected: " & objAdapter.Caption
		EmersonLog " SSLVPN is in use" 
		bCriteriaCheck=False
	End if
Next

If bCriteriaCheck=True Then
	EmersonLog "SSLVPN is not in use for the moment. Attempting to stop vpn processes if they would be running"
	' AnyConnect version 4.9.00086 has the services protected from user & local administrators, so this may not work anymore
	x=KillProcess("vpnui.exe")
	x=KillProcess("vpnagent.exe")
Else
	' bErrorFound = True
	' iPackageExitCode = 1603
	' sErrorMsg = "There was a problem, SSLVPN connection is in use, installation will cancel to avoid disconnecting the user."
	' EmersonLog "Error: " & sErrorMsg
	' Err.Raise iPackageExitCode, sPackageName & " " & sPackageVersion, sErrorMsg
	' RuntimeErrLog	
	EmersonLog "SSLVPN is in use for the moment. Attempting to stop vpn processes."
	' AnyConnect version 4.9.00086 has the services protected from user & local administrators, so this may not work anymore
	x=KillProcess("vpnui.exe")
	x=KillProcess("vpnagent.exe")
	x=KillProcess("acumbrellaagent.exe")
End If

'+-------------------------------------+
'|                                     |
'|               INSTALL               |
'|                                     |
'+-------------------------------------+

If bErrorFound = False Then
	
	'Typically the only things to change for MSI is defining the correct values for the following 4 variables:
	'sMsiFile32Bit, sMsiFile64Bit, sMsiExtraCommandLineOptionsInstall, sMsiExtraCommandLineOptionsRepair
	'Note: if a transforms file is created and put into the Emerson\Config folder the sMsiExtraCommandLineOptionsInstall variable should be defined to something like this:
	'      sMsiExtraCommandLineOptionsInstall = "TRANSFORMS=""" & sCurrentFolder &  "\Emerson\Config\setup.mst"""
	'Note: code expects to have the 32Bit installation files located in folder Program\32Bit
	'Note: code expects to have the 64Bit installation files located in folder Program\64Bit	
	
	sUmbrellaMSI="{2926F2F0-E903-4A6D-910D-F2C8384D0106}"
	' sGinaMSI="{621CEF4F-D26A-4D3B-A3F1-727B4BD9F811}"
	sDartMSI="{C46A2E06-2082-464C-9D8E-EF22CB8571C9}"
	
	sMsiFile32Bit="setup.msi"
	sMsiFile64Bit=sMsiFile32Bit
	sMsiExtraCommandLineOptionsInstall = "LOCKDOWN=1 ARPSYSTEMCOMPONENT=1"						
	sMsiExtraCommandLineOptionsRepair = sMsiExtraCommandLineOptionsInstall
	
	If Instr(sPackageName,"32Bit")>0 Then
		EmersonLog "The package name (defined in Package.dat) indicates this is a 32Bit only install package"
		bInstall32Bit=True
		bInstall64Bit=False
	ElseIf Instr(sPackageName,"64Bit")>0 Then
		EmersonLog "The package name (defined in Package.dat) indicates this is a 64Bit only install package"
		bInstall32Bit=False
		bInstall64Bit=True
	Else
		EmersonLog "The package name (defined in Package.Dat) indicates this package can install both 64Bit and 32Bit"
		'Follow the bitness of the OS to decide which bitness to install
		If OSArchitecture=OSx86 Then
			EmersonLog "The OS is 32Bit, so the package is going to install the 32Bit version"
			bInstall32Bit=True
			bInstall64Bit=False
		Else
			EmersonLog "The OS is 64Bit, so the package is going to install the 64Bit version"
			bInstall32Bit=False
			bInstall64Bit=True
		End If	
	End If
	
	If bInstall32Bit=True Then
		'Install Core VPN Client Package
		sVPNPack="Core VPN package"
		sMsiFile32Bit="anyconnect-win-" & sApplicationVersion & "-core-vpn-predeploy-k9.msi"
		sSetupLogFile = sLogPath & "\" & Replace(sPackageName & "_" & sPackageVersion & "-" & sVPNPack & ".log"," ","_")		
		Installed = MSIInstalled(sMSIProductCode)
		EmersonLog "Set the current path to " & sSourcePath & "\32Bit"
		WshShell.CurrentDirectory = sSourcePath & "\32Bit"
		If Installed Then
			'Repair the installation
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started repair over existing version."
			sCommand="msiexec /i " & Chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsRepair & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 REINSTALL=All REINSTALLMODE=vemu /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & ")"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		Else
			'Install the application
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started."			
			sCommand="msiexec /i " & chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsInstall & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		End If		
		
		'Control loop - Checking again if software is installed
		Installed = MSIInstalled(sMSIProductCode)
		'Control loop processing
		If Installed=False Then
			EmersonLog "Control loop detected an issue with the installation of " & sVPNPack
			iPackageExitCode=1603
		Else
			EmersonLog "Control loop found the software is installed"
		End If
		
		Select Case iPackageExitCode
			Case 0
				EmersonLog "Install Finished succesfully iPackageExitCode=" & iPackageExitCode
			Case 1638
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Already installed, iPackageExitCode=" & iPackageExitCode
			Case 3010
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Software installed but needs a reboot, iPackageExitCode=" & iPackageExitCode
			Case Else
				bErrorFound = True
				EmersonLog "Install Returned Error. iPackageExitCode="  & iPackageExitCode & ". Please investigate." 
				Call GetMsiErrorDescription(iPackageExitCode)		'defines variables sErrorName, sErrorDescription
				EmersonLog "Error name: " & sErrorName & ". Error Description: " & sErrorDescription
				iPackageExitCode = 1603								'translate it to something Ivanti understands
				Err.Raise iPackageExitCode, sPackageName & " " & sPackageVersion, "Error running setup, iPackageExitCode = " & iPackageExitCode 
				RuntimeErrLog	
		End Select
		
		'Install Umbrella Package
		sVPNPack="Umbrella package"
		sMsiFile32Bit="anyconnect-win-" & sApplicationVersion & "-umbrella-predeploy-k9.msi"
		sSetupLogFile = sLogPath & "\" & Replace(sPackageName & "_" & sPackageVersion & "-" & sVPNPack & ".log"," ","_")		
		Installed = MSIInstalled(sUmbrellaMSI)
		
		If Installed Then
			'Repair the installation
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started repair over existing version."
			sCommand="msiexec /i " & Chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsRepair & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 REINSTALL=All REINSTALLMODE=vemu /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & ")"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		Else
			'Install the application
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started."			
			sCommand="msiexec /i " & chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsInstall & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		End If		
		
		'Control loop - Checking again if software is installed
		Installed = MSIInstalled(sUmbrellaMSI)
		'Control loop processing
		If Installed=False Then
			EmersonLog "Control loop detected an issue with the installation of " & sVPNPack
			iPackageExitCode=1603
		Else
			EmersonLog "Control loop found the software is installed"
		End If
		
		Select Case iPackageExitCode
			Case 0
				EmersonLog "Install Finished succesfully iPackageExitCode=" & iPackageExitCode
			Case 1638
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Already installed, iPackageExitCode=" & iPackageExitCode
			Case 3010
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Software installed but needs a reboot, iPackageExitCode=" & iPackageExitCode
			Case Else
				bErrorFound = True
				EmersonLog "Install Returned Error. iPackageExitCode="  & iPackageExitCode & ". Please investigate." 
				Call GetMsiErrorDescription(iPackageExitCode)		'defines variables sErrorName, sErrorDescription
				EmersonLog "Error name: " & sErrorName & ". Error Description: " & sErrorDescription
				iPackageExitCode = 1603								'translate it to something Ivanti understands
				Err.Raise iPackageExitCode, sPackageName & " " & sPackageVersion, "Error running setup, iPackageExitCode = " & iPackageExitCode 
				RuntimeErrLog	
		End Select
		
		' 'Install Gina Package
		' sVPNPack="Gina package"
		' sMsiFile32Bit="anyconnect-win-" & sApplicationVersion & "-gina-predeploy-k9.msi"
		' sSetupLogFile = sLogPath & "\" & Replace(sPackageName & "_" & sPackageVersion & "-" & sVPNPack & ".log"," ","_")		
		' Installed = MSIInstalled(sGinaMSI)
		
		' If Installed Then
			' 'Repair the installation
			' EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started repair over existing version."
			' sCommand="msiexec /i " & Chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsRepair & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 REINSTALL=All REINSTALLMODE=vemu /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			' sCmdLength=Len(sCommand)
			' EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & ")"
			' iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		' Else
			' 'Install the application
			' EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started."			
			' sCommand="msiexec /i " & chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsInstall & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			' sCmdLength=Len(sCommand)
			' EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
			' iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		' End If		
		
		' 'Control loop - Checking again if software is installed
		' Installed = MSIInstalled(sGinaMSI)
		' 'Control loop processing
		' If Installed=False Then
			' EmersonLog "Control loop detected an issue with the installation of " & sVPNPack
			' iPackageExitCode=1603
		' Else
			' EmersonLog "Control loop found the software is installed"
		' End If
		
		' Select Case iPackageExitCode
			' Case 0
				' EmersonLog "Install Finished succesfully iPackageExitCode=" & iPackageExitCode
			' Case 1638
				' EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Already installed, iPackageExitCode=" & iPackageExitCode
			' Case 3010
				' EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Software installed but needs a reboot, iPackageExitCode=" & iPackageExitCode
			' Case Else
				' bErrorFound = True
				' EmersonLog "Install Returned Error. iPackageExitCode="  & iPackageExitCode & ". Please investigate." 
				' Call GetMsiErrorDescription(iPackageExitCode)		'defines variables sErrorName, sErrorDescription
				' EmersonLog "Error name: " & sErrorName & ". Error Description: " & sErrorDescription
				' iPackageExitCode = 1603								'translate it to something Ivanti understands
				' Err.Raise iPackageExitCode, sPackageName & " " & sPackageVersion, "Error running setup, iPackageExitCode = " & iPackageExitCode 
				' RuntimeErrLog	
		' End Select
		
		'Install DART Package
		sVPNPack="DART package"
		sMsiFile32Bit="anyconnect-win-" & sApplicationVersion & "-dart-predeploy-k9.msi"
		sSetupLogFile = sLogPath & "\" & Replace(sPackageName & "_" & sPackageVersion & "-" & sVPNPack & ".log"," ","_")		
		Installed = MSIInstalled(sDartMSI)
		
		If Installed Then
			'Repair the installation
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started repair over existing version."
			sCommand="msiexec /i " & Chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsRepair & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 REINSTALL=All REINSTALLMODE=vemu /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & ")"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		Else
			'Install the application
			EmersonLog sApplicationName & " Version " & sApplicationVersion & " - " & sVPNPack & " Setup started."			
			sCommand="msiexec /i " & chr(34) & sMsiFile32Bit & chr(34) & " " & sMsiExtraCommandLineOptionsInstall & " ROOTDRIVE=C:\ REBOOT=ReallySuppress ALLUSERS=2 /qn /LVX " & Chr(34) & sSetupLogFile & Chr(34)
			sCmdLength=Len(sCommand)
			EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
			iPackageExitCode = WshShell.Run(sCommand,1,TRUE)
		End If		
		
		'Control loop - Checking again if software is installed
		Installed = MSIInstalled(sDartMSI)
		'Control loop processing
		If Installed=False Then
			EmersonLog "Control loop detected an issue with the installation of " & sVPNPack
			iPackageExitCode=1603
		Else
			EmersonLog "Control loop found the software is installed"
		End If
		
		Select Case iPackageExitCode
			Case 0
				EmersonLog "Install Finished succesfully iPackageExitCode=" & iPackageExitCode
			Case 1638
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Already installed, iPackageExitCode=" & iPackageExitCode
			Case 3010
				EmersonLog sPackageName & " Version " & sPackageVersion & " - " & sVPNPack & " Software installed but needs a reboot, iPackageExitCode=" & iPackageExitCode
			Case Else
				bErrorFound = True
				EmersonLog "Install Returned Error. iPackageExitCode="  & iPackageExitCode & ". Please investigate." 
				Call GetMsiErrorDescription(iPackageExitCode)		'defines variables sErrorName, sErrorDescription
				EmersonLog "Error name: " & sErrorName & ". Error Description: " & sErrorDescription
				iPackageExitCode = 1603								'translate it to something Ivanti understands
				Err.Raise iPackageExitCode, sPackageName & " " & sPackageVersion, "Error running setup, iPackageExitCode = " & iPackageExitCode 
				RuntimeErrLog	
		End Select
		
	End If	
	
	
	
End If

'---------------------------------------------------------------------------------------------
' Reboot if nessecary.
' The parameter of EmersonLandeskReboot below, is the name of the job (in this script)
' which has to be started after the reboot.
' It is possible to rename rebootX to a more appropriate name.
' If you do so, make sure you rename the parameter below and the corresponding job name.
'---------------------------------------------------------------------------------------------
'EmersonLandeskReboot("InstallReboot1")

EmersonLog sApplicationName & " " & sApplicationVersion & " setup ended (resultcode=" & iPackageExitCode & ")."

'+-------------------------------------+
'|                                     |
'|            POST-INSTALL             |
'|                                     |
'+-------------------------------------+

Emersonlog "Check if profile file is not missing"

if not fso.FileExists (sSystemDrive & "\ProgramData\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\Employee.xml") then
	fso.CopyFile sSourcePath & "\Profiles\vpn\Employee.xml", sSystemDrive & "\ProgramData\Cisco\Cisco AnyConnect Secure Mobility Client\Profile\", True
end if

EmersonLog "Check if umbrella json file is not missing"

if not fso.FileExists (sSystemDrive & "\ProgramData\Cisco\Cisco AnyConnect Secure Mobility Client\Umbrella\OrgInfo.json") then
	fso.CopyFile sSourcePath & "\Profiles\Umbrella\OrgInfo.json", sSystemDrive & "\ProgramData\Cisco\Cisco AnyConnect Secure Mobility Client\Umbrella\", True
end if

EmersonLog "Shortcut processing"
MakeFolder sAllusersPrograms & "\Utility Applications"
If fso.FolderExists(sAllusersPrograms & "\Cisco") Then 
	fso.deletefolder sAllusersPrograms & "\Cisco", True
End If 
If fso.FileExists(sAllusersDesktop & "\Cisco AnyConnect Secure Mobility Client.lnk") Then
	fso.deletefile sAllusersDesktop & "\Cisco AnyConnect Secure Mobility Client.lnk", True
End If 
select case OSArchitecture
	case OSx86
		set oShellLink = WshShell.CreateShortcut(sAllUsersPrograms & "\Utility Applications\Cisco AnyConnect Secure Mobility Client.lnk")
		oShellLink.TargetPath = sProgramFiles & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnui.exe"
		oShellLink.Save 
		set oShellLink = WshShell.CreateShortcut(sAllUsersPrograms & "\Utility Applications\Cisco AnyConnect Diagnostics and Reporting Tool.lnk")
		oShellLink.TargetPath = sProgramFiles & "\Cisco\Cisco AnyConnect Secure Mobility Client\DART\DartOffline.exe"
		oShellLink.Save 
	case else
		set oShellLink = WshShell.CreateShortcut(sAllUsersPrograms & "\Utility Applications\Cisco AnyConnect Secure Mobility Client.lnk")
		oShellLink.TargetPath = sProgramFilesx86 & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnui.exe"
		oShellLink.Save 
		set oShellLink = WshShell.CreateShortcut(sAllUsersPrograms & "\Utility Applications\Cisco AnyConnect Diagnostics and Reporting Tool.lnk")
		oShellLink.TargetPath = sProgramFilesx86 & "\Cisco\Cisco AnyConnect Secure Mobility Client\DART\DartOffline.exe"
		oShellLink.Save 
End Select
RuntimeErrLog
If bErrorFound = False Then
	Call CleanAdjustCU ("Once", "Cisco_AnyConnect_Secure_Mobility_Client")
End If

EmersonLog "Restarting " & sApplicationName & " Version " & sApplicationVersion & "..."			
select case OSArchitecture
	case OSx86		
		sVpnPath = sProgramFiles & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnagent.exe"		
	case else		
		sVpnPath = sProgramFilesx86 & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnagent.exe"		
End Select
sCommand=sVpnPath
EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
RelaunchRC = WshShell.Run(CHR(34) & sCommand & CHR(34),1,TRUE)
EmersonLog "Return code:" & RelaunchRC
select case OSArchitecture
	case OSx86		
		sVpnPath = sProgramFiles & "\Cisco\Cisco AnyConnect Secure Mobility Client\acumbrellaagent.exe"		
	case else		
		sVpnPath = sProgramFilesx86 & "\Cisco\Cisco AnyConnect Secure Mobility Client\acumbrellaagent.exe"		
End Select
sCommand=sVpnPath
EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
RelaunchRC = WshShell.Run(CHR(34) & sCommand & CHR(34),1,TRUE)
EmersonLog "Return code:" & RelaunchRC
'select case OSArchitecture
'	case OSx86		
'		sVpnPath = sProgramFiles & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnui.exe"		
'	case else		
'		sVpnPath = sProgramFilesx86 & "\Cisco\Cisco AnyConnect Secure Mobility Client\Vpnui.exe"		
'End Select
'sCommand=sVpnPath
'EmersonLog "Running [" & sCommand & "] (command length is:" & sCmdLength & " characters)"
'iPackageExitCode = WshShell.Run(CHR(34) & sCommand & CHR(34),1,FALSE)