From 9fbc65fce120a9fca84d2912e87e4fa010ec25e0 Mon Sep 17 00:00:00 2001 From: Guillaume Bougard Date: Mon, 2 Oct 2023 17:41:41 +0200 Subject: [PATCH] feat: Update VBS with expected features Permit MSI package installation from current folder Improve VBS deployment with a check if installed correctly or retry Fix uninstallFusionInventoryAgent() function indentation Closes #429, #505 --- Changes | 4 + contrib/windows/glpi-agent-deployment.vbs | 139 +++++++++++++++------- 2 files changed, 102 insertions(+), 41 deletions(-) diff --git a/Changes b/Changes index c2f24b0b2..fb7c55066 100644 --- a/Changes +++ b/Changes @@ -50,6 +50,10 @@ packaging: * fix #446: Always add Sys::Hostname dependency to rpm packaging * fix #493: Add support for CONFIG=reset option for MSI Installer +contrib: +* fix #429: Permit MSI package installation from current folder +* fix #505: [Idea] Improve VBS deployment with a check if installed correctly or retry + 1.5 Wed, 21 Jun 2023 core: diff --git a/contrib/windows/glpi-agent-deployment.vbs b/contrib/windows/glpi-agent-deployment.vbs index 77512ea3c..a00c4c58a 100644 --- a/contrib/windows/glpi-agent-deployment.vbs +++ b/contrib/windows/glpi-agent-deployment.vbs @@ -214,41 +214,41 @@ Function hasOption(opt) End Function Function uninstallFusionInventoryAgent() - Dim Uninstall, getValue - - ' Try to get SERVER and LOCAL from FIA configuration in registry if needed - If not hasOption("SERVER") then - On error resume next - getValue = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\FusionInventory-Agent\server") - If err.number = 0 And getValue <> "" then - SetupOptions = SetupOptions & " SERVER='" & getValue & "'" - End If - End If - If not hasOption("LOCAL") then - On error resume next - getValue = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\FusionInventory-Agent\local") - If err.number = 0 And getValue <> "" then - SetupOptions = SetupOptions & " LOCAL='" & getValue & "'" - End If - End If - - ' Verify normal case - On error resume next - Uninstall = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\UninstallString") - If err.number = 0 then - WshShell.Run "CMD.EXE /C net stop FusionInventory-Agent",0,True - WshShell.Run "CMD.EXE /C """ & Uninstall & """ /S /NOSPLASH",0,True - WshShell.Run "CMD.EXE /C rmdir ""%ProgramFiles%\FusionInventory-Agent"" /S /Q",0,True - End If - - ' Verify FIA x86 is installed on x64 OS - On error resume next - Uninstall = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\UninstallString") - If err.number = 0 then - WshShell.Run "CMD.EXE /C net stop FusionInventory-Agent",0,True - WshShell.Run "CMD.EXE /C """ & Uninstall & """ /S /NOSPLASH",0,True - WshShell.Run "CMD.EXE /C rmdir ""%ProgramFiles(x86)%\FusionInventory-Agent"" /S /Q",0,True - End If + Dim Uninstall, getValue + + ' Try to get SERVER and LOCAL from FIA configuration in registry if needed + If not hasOption("SERVER") then + On error resume next + getValue = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\FusionInventory-Agent\server") + If err.number = 0 And getValue <> "" then + SetupOptions = SetupOptions & " SERVER='" & getValue & "'" + End If + End If + If not hasOption("LOCAL") then + On error resume next + getValue = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\FusionInventory-Agent\local") + If err.number = 0 And getValue <> "" then + SetupOptions = SetupOptions & " LOCAL='" & getValue & "'" + End If + End If + + ' Verify normal case + On error resume next + Uninstall = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\UninstallString") + If err.number = 0 then + WshShell.Run "CMD.EXE /C net stop FusionInventory-Agent",0,True + WshShell.Run "CMD.EXE /C """ & Uninstall & """ /S /NOSPLASH",0,True + WshShell.Run "CMD.EXE /C rmdir ""%ProgramFiles%\FusionInventory-Agent"" /S /Q",0,True + End If + + ' Verify FIA x86 is installed on x64 OS + On error resume next + Uninstall = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\UninstallString") + If err.number = 0 then + WshShell.Run "CMD.EXE /C net stop FusionInventory-Agent",0,True + WshShell.Run "CMD.EXE /C """ & Uninstall & """ /S /NOSPLASH",0,True + WshShell.Run "CMD.EXE /C rmdir ""%ProgramFiles(x86)%\FusionInventory-Agent"" /S /Q",0,True + End If End Function Function AdvanceTime(nMinutes) @@ -459,6 +459,63 @@ Function ShowMessage(strMessage) End If End Function +Function MsiServerAvailable() + Dim loopCount, objWMIService, oMsiServer, oServicePath, errExecMethod + MsiServerAvailable = false + Const maxLoops = 120 + loopCount = 0 + 'Wait maximum 2 minutes to get MsiServer service available + Set objWMIService = GetObject("winmgmts:\\.\root\CIMV2") + Do While loopCount < maxLoops + If loopCount > 0 Then + WScript.Sleep 1000 + End If + Set oMsiServer = GetObject("winmgmts:Win32_Service='MsiServer'") + If oMsiServer.State = "Stopped" Then + MsiServerAvailable = true + Exit Function + End If + Set oServicePath = oMsiServer.Path_ + Set errExecMethod = objWMIService.ExecMethod(oServicePath, "StopService") + 'StopService method should fail with ReturnValue set to 5 on MsiServer business + If errExecMethod.ReturnValue = 0 Then + MsiServerAvailable = true + Exit Function + End If + loopCount = loopCount + 1 + Loop +End Function + +Function MsiExec(strOptions) + Dim loopCount + Const maxLoops = 3 + loopCount = 0 + Do While loopCount < maxLoops + If loopCount > 0 Then + ShowMessage("Next attempt in 30 seconds...") + WScript.Sleep 30000 + End If + If MsiServerAvailable() Then + ShowMessage("Running: MsiExec.exe " & strOptions) + MsiExec = WshShell.Run("MsiExec.exe " & strOptions, 0, True) + ' Error 1618 occurs on MsiServer business, we should only retry on that error + If MsiExec <> 1618 Then + Exit Do + End If + Else + MsiExec = 1618 + End If + loopCount = loopCount + 1 + Loop + If MsiExec = 0 Then + ShowMessage("Deployment done!") + ElseIf MsiExec = 1618 Then + ShowMessage("Deployment failed: MSI Installer is busy!") + Else + ShowMessage("Deployment failed! (Err=" & MsiExec & ")") + End If +End Function + ' ' ' MAIN @@ -543,18 +600,18 @@ If bInstall Then If SaveWebBinary(SetupLocation, Setup) Then strCmd = WshShell.ExpandEnvironmentStrings("%ComSpec%") strTempDir = WshShell.ExpandEnvironmentStrings("%TEMP%") - ShowMessage("Running: MsiExec.exe " & strInstallOrRepair & " """ & strTempDir & "\" & Setup & """ " & SetupOptions) - WshShell.Run "MsiExec.exe " & strInstallOrRepair & " """ & strTempDir & "\" & Setup & """ " & SetupOptions, 0, True + MsiExec(strInstallOrRepair & " """ & strTempDir & "\" & Setup & """ " & SetupOptions) ShowMessage("Scheduling: DEL /Q /F """ & strTempDir & "\" & Setup & """") WshShell.Run "AT.EXE " & AdvanceTime(nMinutesToAdvance) & " " & strCmd & " /C ""DEL /Q /F """"" & strTempDir & "\" & Setup & """""", 0, True - ShowMessage("Deployment done!") Else ShowMessage("Error downloading '" & SetupLocation & "\" & Setup & "'!") End If Else - ShowMessage("Running: MsiExec.exe " & strInstallOrRepair & " """ & SetupLocation & "\" & Setup & """ " & SetupOptions) - WshShell.Run "MsiExec.exe " & strInstallOrRepair & " """ & SetupLocation & "\" & Setup & """ " & SetupOptions, 0, True - ShowMessage("Deployment done!") + 'Don't include path if empty or set to current folder + If SetupLocation <> "" And SetupLocation <> "." Then + Setup = SetupLocation & "\" & Setup + End If + MsiExec(strInstallOrRepair & " """ & Setup & """ " & SetupOptions) End If Else ShowMessage("It isn't needed the installation of '" & Setup & "'.")