Advertisement
Guest User

Untitled

a guest
Sep 11th, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VBScript 12.54 KB | None | 0 0
  1. Option Explicit
  2. Dim Force, Verbose
  3. Dim Setup, SetupArchitecture, SetupLocation, SetupOptions, SetupVersion
  4.  
  5.  
  6. SetupVersion = "2.5.1"
  7.  
  8. SetupLocation = "http://stock.server.com"
  9.  
  10.  
  11. SetupArchitecture = "Auto"
  12.  
  13. SetupOptions = "/acceptlicense /runnow /tag='COMPANY' /server='http://stock.server.com/plugins/fusioninventory/' /S"
  14.  
  15. Setup = "fusioninventory-agent_windows-" & SetupArchitecture & "_" & SetupVersion & ".exe"
  16.  
  17. Force = "Yes"
  18.  
  19. Verbose = "No"
  20.  
  21. '
  22. '
  23. ' DO NOT EDIT BELOW
  24. '
  25. '
  26.  
  27. Function AdvanceTime(nMinutes)
  28.    Dim nMinimalMinutes, dtmTimeFuture
  29.    ' As protection
  30.   nMinimalMinutes = 5
  31.    If nMinutes < nMinimalMinutes Then
  32.       nMinutes = nMinimalMinutes
  33.    End If
  34.    ' Add nMinutes to the current time
  35.   dtmTimeFuture = DateAdd ("n", nMinutes, Time)
  36.    ' Format the result value
  37.   '    The command AT accepts 'HH:MM' values only
  38.   AdvanceTime = Hour(dtmTimeFuture) & ":" & Minute(dtmTimeFuture)
  39. End Function
  40.  
  41. Function baseName (strng)
  42.    Dim regEx, ret
  43.    Set regEx = New RegExp
  44.    regEx.Global = true
  45.    regEx.IgnoreCase = True
  46.    regEx.Pattern = ".*[/\\]([^/\\]+)$"
  47.    baseName = regEx.Replace(strng,"$1")
  48. End Function
  49.  
  50. Function GetSystemArchitecture()
  51.    Dim strSystemArchitecture
  52.    Err.Clear
  53.    ' Get operative system architecture
  54.   On Error Resume Next
  55.    strSystemArchitecture = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%PROCESSOR_ARCHITECTURE%")
  56.    If Err.Number = 0 Then
  57.       ' Check the operative system architecture
  58.      Select Case strSystemArchitecture
  59.          Case "x86"
  60.             ' The system architecture is 32-bit
  61.            GetSystemArchitecture = "x86"
  62.          Case "AMD64"
  63.             ' The system architecture is 64-bit
  64.            GetSystemArchitecture = "x64"
  65.          Case Else
  66.             ' The system architecture is not supported
  67.            GetSystemArchitecture = "NotSupported"
  68.       End Select
  69.    Else
  70.       ' It has been not possible to get the system architecture
  71.      GetSystemArchitecture = "Unknown"
  72.    End If
  73. End Function
  74.  
  75. Function isHttp(strng)
  76.    Dim regEx, matches
  77.    Set regEx = New RegExp
  78.    regEx.Global = true
  79.    regEx.IgnoreCase = True
  80.    regEx.Pattern = "^(http(s?)).*"
  81.    If regEx.Execute(strng).count > 0 Then
  82.       isHttp = True
  83.    Else
  84.       isHttp = False
  85.    End If
  86.    Exit Function
  87. End Function
  88.  
  89. Function IsInstallationNeeded(strSetupVersion, strSetupArchitecture, strSystemArchitecture)
  90.    Dim strCurrentSetupVersion
  91.    ' Compare the current version, whether it exists, with strSetupVersion
  92.   If strSystemArchitecture = "x86" Then
  93.       ' The system architecture is 32-bit
  94.      ' Check if the subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' exists
  95.      '    This subkey is now deprecated
  96.      On error resume next
  97.       strCurrentSetupVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent\DisplayVersion")
  98.       If Err.Number = 0 Then
  99.       ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' exists
  100.         If strCurrentSetupVersion <> strSetupVersion Then
  101.             ShowMessage("Installation needed: " & strCurrentSetupVersion & " -> " & strSetupVersion)
  102.             IsInstallationNeeded = True
  103.          End If
  104.          Exit Function
  105.       Else
  106.          ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' doesn't exist
  107.         Err.Clear
  108.          ' Check if the subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  109.         On error resume next
  110.          strCurrentSetupVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\DisplayVersion")
  111.          If Err.Number = 0 Then
  112.          ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  113.            If strCurrentSetupVersion <> strSetupVersion Then
  114.                ShowMessage("Installation needed: " & strCurrentSetupVersion & " -> " & strSetupVersion)
  115.                IsInstallationNeeded = True
  116.             End If
  117.             Exit Function
  118.          Else
  119.             ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' doesn't exist
  120.            Err.Clear
  121.             ShowMessage("Installation needed: " & strSetupVersion)
  122.             IsInstallationNeeded = True
  123.          End If
  124.       End If
  125.    Else
  126.       ' The system architecture is 64-bit
  127.      ' Check if the subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' exists
  128.      '    This subkey is now deprecated
  129.      On error resume next
  130.       strCurrentSetupVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent\DisplayVersion")
  131.       If Err.Number = 0 Then
  132.       ' The subkey 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' exists
  133.         If strCurrentSetupVersion <> strSetupVersion Then
  134.             ShowMessage("Installation needed: " & strCurrentSetupVersion & " -> " & strSetupVersion)
  135.             IsInstallationNeeded = True
  136.          End If
  137.          Exit Function
  138.       Else
  139.          ' The subkey 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory Agent' doesn't exist
  140.         Err.Clear
  141.          ' Check if the subkey 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  142.         On error resume next
  143.          strCurrentSetupVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\DisplayVersion")
  144.          If Err.Number = 0 Then
  145.          ' The subkey 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  146.            If strCurrentSetupVersion <> strSetupVersion Then
  147.                ShowMessage("Installation needed: " & strCurrentSetupVersion & " -> " & strSetupVersion)
  148.                IsInstallationNeeded = True
  149.             End If
  150.             Exit Function
  151.          Else
  152.             ' The subkey 'SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' doesn't exist
  153.            Err.Clear
  154.             ' Check if the subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  155.            On error resume next
  156.             strCurrentSetupVersion = WshShell.RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent\DisplayVersion")
  157.             If Err.Number = 0 Then
  158.             ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' exists
  159.               If strCurrentSetupVersion <> strSetupVersion Then
  160.                   ShowMessage("Installation needed: " & strCurrentSetupVersion & " -> " & strSetupVersion)
  161.                   IsInstallationNeeded = True
  162.                End If
  163.                Exit Function
  164.             Else
  165.                ' The subkey 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\FusionInventory-Agent' doesn't exist
  166.               Err.Clear
  167.                ShowMessage("Installation needed: " & strSetupVersion)
  168.                IsInstallationNeeded = True
  169.             End If
  170.          End If
  171.       End If
  172.    End If
  173. End Function
  174.  
  175. Function IsSelectedForce()
  176.    If LCase(Force) <> "no" Then
  177.       ShowMessage("Installation forced: " & SetupVersion)
  178.       IsSelectedForce = True
  179.    Else
  180.       IsSelectedForce = False
  181.    End If
  182. End Function
  183.  
  184. ' http://www.ericphelps.com/scripting/samples/wget/index.html
  185. Function SaveWebBinary(strSetupLocation, strSetup)
  186.    Const adTypeBinary = 1
  187.    Const adSaveCreateOverWrite = 2
  188.    Const ForWriting = 2
  189.    Dim web, varByteArray, strData, strBuffer, lngCounter, ado, strUrl
  190.    strUrl = strSetupLocation & "/" & strSetup
  191.    'On Error Resume Next
  192.   'Download the file with any available object
  193.   Err.Clear
  194.    Set web = Nothing
  195.    Set web = CreateObject("WinHttp.WinHttpRequest.5.1")
  196.    If web Is Nothing Then Set web = CreateObject("WinHttp.WinHttpRequest")
  197.    If web Is Nothing Then Set web = CreateObject("MSXML2.ServerXMLHTTP")
  198.    If web Is Nothing Then Set web = CreateObject("Microsoft.XMLHTTP")
  199.    web.Open "GET", strURL, False
  200.    web.Send
  201.    If Err.Number <> 0 Then
  202.       SaveWebBinary = False
  203.       Set web = Nothing
  204.       Exit Function
  205.    End If
  206.    If web.Status <> "200" Then
  207.       SaveWebBinary = False
  208.       Set web = Nothing
  209.       Exit Function
  210.    End If
  211.    varByteArray = web.ResponseBody
  212.    Set web = Nothing
  213.    'Now save the file with any available method
  214.   On Error Resume Next
  215.    Set ado = Nothing
  216.    Set ado = CreateObject("ADODB.Stream")
  217.    If ado Is Nothing Then
  218.       Set fs = CreateObject("Scripting.FileSystemObject")
  219.       Set ts = fs.OpenTextFile(baseName(strUrl), ForWriting, True)
  220.       strData = ""
  221.       strBuffer = ""
  222.       For lngCounter = 0 to UBound(varByteArray)
  223.          ts.Write Chr(255 And Ascb(Midb(varByteArray,lngCounter + 1, 1)))
  224.       Next
  225.       ts.Close
  226.    Else
  227.       ado.Type = adTypeBinary
  228.       ado.Open
  229.       ado.Write varByteArray
  230.       ado.SaveToFile CreateObject("WScript.Shell").ExpandEnvironmentStrings("%TEMP%") & "/" & strSetup, adSaveCreateOverWrite
  231.       ado.Close
  232.    End If
  233.    SaveWebBinary = True
  234. End Function
  235.  
  236. Function ShowMessage(strMessage)
  237.    If LCase(Verbose) <> "no" Then
  238.       WScript.Echo strMessage
  239.    End If
  240. End Function
  241.  
  242. '
  243. '
  244. ' MAIN
  245. '
  246. '
  247.  
  248. Dim nMinutesToAdvance, strCmd, strSystemArchitecture, strTempDir, WshShell
  249. Set WshShell = WScript.CreateObject("WScript.shell")
  250.  
  251. nMinutesToAdvance = 5
  252.  
  253. ' Get system architecture
  254. strSystemArchitecture = GetSystemArchitecture()
  255. If (strSystemArchitecture <> "x86") And (strSystemArchitecture <> "x64") Then
  256.    ShowMessage("The system architecture is unknown or not supported.")
  257.    ShowMessage("Deployment aborted!")
  258.    WScript.Quit 1
  259. Else
  260.    ShowMessage("System architecture detected: " & strSystemArchitecture)
  261. End If
  262.  
  263. ' Check and auto detect SetupArchitecture
  264. Select Case LCase(SetupArchitecture)
  265.    Case "x86"
  266.       ' The setup architecture is 32-bit
  267.      SetupArchitecture = "x86"
  268.       Setup = Replace(Setup, "x86", SetupArchitecture, 1, 1, vbTextCompare)
  269.       ShowMessage("Setup architecture: " & SetupArchitecture)
  270.    Case "x64"
  271.       ' The setup architecture is 64-bit
  272.      SetupArchitecture = "x64"
  273.       Setup = Replace(Setup, "x64", SetupArchitecture, 1, 1, vbTextCompare)
  274.       ShowMessage("Setup architecture: " & SetupArchitecture)
  275.    Case "auto"
  276.       ' Auto detection of SetupArchitecture
  277.      SetupArchitecture = strSystemArchitecture
  278.       Setup = Replace(Setup, "Auto", SetupArchitecture, 1, 1, vbTextCompare)
  279.       ShowMessage("Setup architecture detected: " & SetupArchitecture)
  280.    Case Else
  281.       ' The setup architecture is not supported
  282.      ShowMessage("The setup architecture '" & SetupArchitecture & "' is not supported.")
  283.       WScript.Quit 2
  284. End Select
  285.  
  286. ' Check the relation between strSystemArchitecture and SetupArchitecture
  287. If (strSystemArchitecture = "x86") And (SetupArchitecture = "x64") Then
  288.    ' It isn't possible to execute a 64-bit setup on a 32-bit operative system
  289.   ShowMessage("It isn't possible to execute a 64-bit setup on a 32-bit operative system.")
  290.    ShowMessage("Deployment aborted!")
  291.    WScript.Quit 3
  292. End If
  293.  
  294. If IsSelectedForce() Or IsInstallationNeeded(SetupVersion, SetupArchitecture, strSystemArchitecture) Then
  295.    If isHttp(SetupLocation) Then
  296.       ShowMessage("Downloading: " & SetupLocation & "/" & Setup)
  297.       If SaveWebBinary(SetupLocation, Setup) Then
  298.          strCmd = WshShell.ExpandEnvironmentStrings("%ComSpec%")
  299.          strTempDir = WshShell.ExpandEnvironmentStrings("%TEMP%")
  300.          ShowMessage("Running: """ & strTempDir & "\" & Setup & """ " & SetupOptions)
  301.          WshShell.Run """" & strTempDir & "\" & Setup & """ " & SetupOptions, 0, True
  302.          ShowMessage("Scheduling: DEL /Q /F """ & strTempDir & "\" & Setup & """")
  303.          WshShell.Run "AT.EXE " & AdvanceTime(nMinutesToAdvance) & " " & strCmd & " /C ""DEL /Q /F """"" & strTempDir & "\" & Setup & """""", 0, True
  304.          ShowMessage("Deployment done!")
  305.       Else
  306.          ShowMessage("Error downloading '" & SetupLocation & "\" & Setup & "'!")
  307.       End If
  308.    Else
  309.       ShowMessage("Running: """ & SetupLocation & "\" & Setup & """ " & SetupOptions)
  310.       WshShell.Run "CMD.EXE /C """ & SetupLocation & "\" & Setup & """ " & SetupOptions, 0, True
  311.       ShowMessage("Deployment done!")
  312.    End If
  313. Else
  314.    ShowMessage("It isn't needed the installation of '" & Setup & "'.")
  315. End If
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement