Comment convertir un script avec GUI en script avec arguments sans GUI

Aide sur les Interfaces Graphique Utilisateurs (GUI).
Règles du forum
.
Répondre
MarcS
Niveau 1
Niveau 1
Messages : 1
Enregistré le : sam. 30 janv. 2021 21:35
Status : Hors ligne

Comment convertir un script avec GUI en script avec arguments sans GUI

#1

Message par MarcS »

Bonjour,

j’ai trouvé cet excellent outil sur le forum de AutoIT, il a été partagé par psyCodez en 2014, cet usager n’a eu aucune autre activité sur le forum officiel depuis, voici un lien vers son post de 2014.

Post original du script

Voici le code de l’outil :

Code : Tout sélectionner

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <Constants.au3>
#include <Date.au3>
#include <String.au3>

GUI()

Exit(0)

Func GUI()
    Global $nameCol     = 0
    Global $verCol      = 1
    Global $pubCol      = 2
    Global $archCol     = 3
    Global $msiCol      = 4
    Global $guidCol     = 5
    Global $uninstCol   = 6
    Global $dateCol     = 7
    Global $locCol      = 8
    Global $srcCol      = 9
    Global $keyCol      = 10

    Global $computer = ""
    Global $osArch = ""

    Global $GUI = GUICreate("Collect Uninstall Keys", 1000, 500, -1, -1, BitOR($WS_MAXIMIZEBOX, $WS_MINIMIZEBOX, $WS_SYSMENU, $WS_SIZEBOX, $WS_CAPTION), BitOR($WS_EX_STATICEDGE,$WS_EX_WINDOWEDGE))

    Global $iComp    = GUICtrlCreateInput("", 10, 9, 100, 21)
    Global $bConnect = GUICtrlCreateButton("Connect", 115, 5, 50, 28)
    Global $iSrch    = GUICtrlCreateInput("", 177, 9, 100, 21)
    Global $bFilter  = GUICtrlCreateButton("Filter", 283, 5, 50, 28)
    Global $cHideMSUpdates = GUICtrlCreateCheckbox("Hide Microsoft Updates", 352, 10, 137, 20)
    Global $rAll     = GUICtrlCreateRadio("All", 505, 10, 35, 20)
    Global $r32      = GUICtrlCreateRadio("x86", 540, 10, 35, 20)
    Global $r64      = GUICtrlCreateRadio("x64", 580, 10, 35, 20)
    Global $bExport  = GUICtrlCreateButton("Export to CSV", 883, 5, 90, 28)
    GUICtrlSetState($cHideMSUpdates, $GUI_CHECKED)
	;GUICtrlSetState($bConnect, $GUI_CHECKED)
	;GUICtrlSetState($bExport, $GUI_CHECKED)
    GUICtrlSetState($rAll, $GUI_CHECKED)

    If $CmdLine[0] > 0 And $CmdLine[0] < 3 Then
        If $CmdLine[0] == 2 Then
            GUICtrlSetData($iSrch, $CmdLine[1])
            GUICtrlSetData($iComp, $CmdLine[2])
        Else
            GUICtrlSetData($iSrch, $CmdLine[1])
        EndIf
    EndIf

    Global $lResults = GUICtrlCreateLabel("", 8, 485, 500, 20)

    Global $ListView = GUICtrlCreateListView("", 0, 38, 1000, 445, -1, BitOR($LVS_EX_GRIDLINES,$LVS_EX_FULLROWSELECT,$WS_EX_CLIENTEDGE))

    _GUICtrlListView_AddColumn($ListView, "Name", 170)
    _GUICtrlListView_AddColumn($ListView, "Version", 65)
    _GUICtrlListView_AddColumn($ListView, "Publisher", 105)
    _GUICtrlListView_AddColumn($ListView, "Arch", 40, 2)
    _GUICtrlListView_AddColumn($ListView, "MSI", 35, 2)
    _GUICtrlListView_AddColumn($ListView, "GUID", 85)
    _GUICtrlListView_AddColumn($ListView, "Uninstall String", 95)
    _GUICtrlListView_AddColumn($ListView, "Install Date", 70)
    _GUICtrlListView_AddColumn($ListView, "Install Location", 100)
    _GUICtrlListView_AddColumn($ListView, "Install Source", 85)
    _GUICtrlListView_AddColumn($ListView, "Uninstall Key", 125)

    Global $rcMenu          = GUICtrlCreateContextMenu($ListView)
    Global $rcCopy          = GUICtrlCreateMenu("Copy", $rcMenu)
    Global $rcCopyLine      = GUICtrlCreateMenuItem("Line", $rcCopy)
    Global $rcCopyName      = GUICtrlCreateMenuItem("Name", $rcCopy)
    Global $rcCopyVer       = GUICtrlCreateMenuItem("Version", $rcCopy)
    Global $rcCopyPub       = GUICtrlCreateMenuItem("Publisher", $rcCopy)
    Global $rcCopyGUID      = GUICtrlCreateMenuItem("GUID", $rcCopy)
    Global $rcCopyUninst    = GUICtrlCreateMenuItem("Uninstall String", $rcCopy)
    Global $rcCopyLoc       = GUICtrlCreateMenuItem("Install Location", $rcCopy)
    Global $rcCopySrc       = GUICtrlCreateMenuItem("Install Source", $rcCopy)
    Global $rcCopyKey       = GUICtrlCreateMenuItem("Uninstall Key", $rcCopy)

    Global $rcJump      = GUICtrlCreateMenu("Jump to", $rcMenu)
    Global $rcJumpLoc   = GUICtrlCreateMenuItem("Install Location", $rcJump)
    Global $rcJumpSrc   = GUICtrlCreateMenuItem("Install Source", $rcJump)
    Global $rcJumpKey   = GUICtrlCreateMenuItem("Uninstall Key", $rcJump)

    Global $rcUninstall = GUICtrlCreateMenuItem("Uninstall", $rcMenu)

    Global $rcCancel = 0

    _GUICtrlListView_RegisterSortCallBack($ListView)

    GUISetState(@SW_SHOW)

    FillListView()

    While 1
        ReadGUI(GUIGetMsg())
    WEnd
EndFunc

Func FillListView()
    Local $i = 0, $c = 0
    Local $key = "", $value = "", $name = ""

    Local $regRoot = "HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    Local $regRoot32 = "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    Local $regArch = "HKLM64\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

    $computer = GUICtrlRead($iComp)
    Local $srchTrm  = GUICtrlRead($iSrch)

    ToggleControlAvailability(0)

    _GUICtrlListView_DeleteAllItems($ListView)
    _GUICtrlListView_BeginUpdate($ListView)

    Local $pingErr = 0
    If $computer <> "" And StringCompare($computer, @ComputerName) <> 0 And StringCompare($computer, "localhost") <> 0 And $computer <> "127.0.0.1" Then
        $regRoot    = "\\" & $computer & "\" & $regRoot
        $regRoot32  = "\\" & $computer & "\" & $regRoot32
        $regArch    = "\\" & $computer & "\" & $regArch
        GUICtrlSetData($lResults, "Connecting to " & $computer & "...")
        Ping($computer, 1000)
        $pingErr = @error
    Else
        GUICtrlSetData($lResults, "")
    EndIf

    Local $dataBlob = ""

    ToggleRcMenus(1)

    If $pingErr <> 0 Then
        GUICtrlSetData($lResults, "Unable to connect to " & $computer & ".")
    Else
        $osArch = StringUpper(RegRead($regArch, "PROCESSOR_ARCHITECTURE"))

        If $osArch == "AMD64" And GUICtrlRead($r32) == $GUI_CHECKED Then
            $regRoot = $regRoot32
        ElseIf $osArch <> "AMD64" And GUICtrlRead($r64) == $GUI_CHECKED Then
            GUICtrlSetState($rAll, $GUI_CHECKED)
        EndIf

        GUICtrlSetData($lResults, "Searching...")
        Local $hideUpdates = 0
        If GUICtrlRead($cHideMSUpdates) == $GUI_CHECKED Then
            $hideUpdates = 1
        EndIf

        While 1
            If ReadGUI(GUIGetMsg($GUI)) == False Then
                GUICtrlSetData($lResults, $c & " results found (Search cancelled)")
                ExitLoop
            EndIf
            $i += 1
            $key = $regRoot & "\" & RegEnumKey($regRoot, $i)
            Switch @error
                Case 0
                   $name = RegRead($key, "DisplayName")
				  If @error == 0 And $name <> "" Then
					 $pub = RegRead($key, "Publisher")
					 $uninst = RegRead($key, "UninstallString")
				  If ($srchTrm == "" Or StringInStr($name, $srchTrm) Or StringInStr($pub, $srchTrm) Or StringInStr($uninst, $srchTrm)) Then
					 If $hideUpdates Then
						If Not (StringInStr($pub, "Microsoft", 0, 1) And (StringInStr($name, "Update", 0, 1) Or StringInStr($name, "Hotfix", 0, 1))) Then
						   $dataBlob &= BuildLine($ListView, $key, $name, RegRead($key, "DisplayVersion"), $pub, $uninst, RegRead($key, "InstallDate"), RegRead($key, "InstallLocation"), RegRead($key, "InstallSource"))
						If $computer <> "" Then AddListEntry($computer, $dataBlob)
				  $c += 1
            EndIf
        Else
            $dataBlob &= BuildLine($ListView, $key, $name, RegRead($key, "DisplayVersion"), $pub, $uninst, RegRead($key, "InstallDate"), RegRead($key, "InstallLocation"), RegRead($key, "InstallSource"))
            If $computer <> "" Then AddListEntry($computer, $dataBlob)
            $c += 1
        EndIf
    EndIf
EndIf
                Case -1
                    If $osArch == "AMD64" And $regRoot <> $regRoot32 And GUICtrlRead($r64) <> $GUI_CHECKED Then
                        $regRoot = $regRoot32
                        $i = 0
                    Else
                        GUICtrlSetData($lResults, $c & " result(s) found")
                        ExitLoop
                    EndIf
                Case 3
                    GUICtrlSetData($lResults, "Unable to connect to remote registry.")
                    ExitLoop
            EndSwitch
        WEnd
    EndIf

    If $computer == "" Then
        $dataBlob = StringStripWS($dataBlob, $STR_STRIPTRAILING)
        Local $lines = StringSplit($dataBlob, @CRLF, $STR_ENTIRESPLIT + $STR_NOCOUNT)
        If $dataBlob <> "" Then
            _ArraySort($lines)
            Local $listArray[UBound($lines)][_GUICtrlListView_GetColumnCount($ListView)]
            For $i = 0 To UBound($lines)-1
                Local $temp = StringSplit($lines[$i], "|", $STR_NOCOUNT)
                For $j = 0 To UBound($temp)-1
                    $listArray[$i][$j] = $temp[$j]
                Next
            Next
            _GUICtrlListView_DeleteAllItems($ListView)
            _GUICtrlListView_AddArray($ListView, $listArray)
            _GUICtrlListView_SetColumnWidth($ListView, 0, 170)
        EndIf
    Else
        Local $sortSense = False
        _GUICtrlListView_SimpleSort($ListView, $sortSense, 0)
    EndIf

    _GUICtrlListView_EndUpdate($ListView)

    ToggleRcMenus(0)

    ToggleControlAvailability(1)
EndFunc

Func ToggleRcMenus($inProgress)
    If $inProgress Then
        GUICtrlDelete($rcMenu)
        $rcMenu     = GUICtrlCreateContextMenu($ListView)
        $rcCancel    = GUICtrlCreateMenuItem("Cancel Search", $rcMenu)
    Else
        GUICtrlDelete($rcMenu)
        $rcMenu         = GUICtrlCreateContextMenu($ListView)
        $rcCopy         = GUICtrlCreateMenu("Copy", $rcMenu)
        $rcCopyLine     = GUICtrlCreateMenuItem("Line", $rcCopy)
        $rcCopyName     = GUICtrlCreateMenuItem("Name", $rcCopy)
        $rcCopyVer      = GUICtrlCreateMenuItem("Version", $rcCopy)
        $rcCopyPub      = GUICtrlCreateMenuItem("Publisher", $rcCopy)
        $rcCopyGUID     = GUICtrlCreateMenuItem("GUID", $rcCopy)
        $rcCopyUninst   = GUICtrlCreateMenuItem("Uninstall String", $rcCopy)
        $rcCopyLoc      = GUICtrlCreateMenuItem("Install Location", $rcCopy)
        $rcCopySrc      = GUICtrlCreateMenuItem("Install Source", $rcCopy)
        $rcCopyKey      = GUICtrlCreateMenuItem("Uninstall Key", $rcCopy)
        $rcJump         = GUICtrlCreateMenu("Jump to", $rcMenu)
        $rcJumpLoc      = GUICtrlCreateMenuItem("Install Location", $rcJump)
        $rcJumpSrc      = GUICtrlCreateMenuItem("Install Source", $rcJump)
        $rcJumpKey      = GUICtrlCreateMenuItem("Uninstall Key", $rcJump)
        $rcUninstall    = GUICtrlCreateMenuItem("Uninstall", $rcMenu)
    EndIf
EndFunc

Func ToggleControlAvailability($enable)
    If $enable Then
        GUICtrlSetState($iComp, $GUI_ENABLE)
        GUICtrlSetState($iSrch, $GUI_ENABLE)
        GUICtrlSetState($bConnect, $GUI_ENABLE)
        GUICtrlSetState($bFilter, $GUI_ENABLE)
        GUICtrlSetState($cHideMSUpdates, $GUI_ENABLE)
        GUICtrlSetState($rAll, $GUI_ENABLE)
        GUICtrlSetState($r32, $GUI_ENABLE)
        GUICtrlSetState($r64, $GUI_ENABLE)
        GUICtrlSetState($bExport, $GUI_ENABLE)
    Else
        GUICtrlSetState($iComp, $GUI_DISABLE)
        GUICtrlSetState($iSrch, $GUI_DISABLE)
        GUICtrlSetState($bConnect, $GUI_DISABLE)
        GUICtrlSetState($bFilter, $GUI_DISABLE)
        GUICtrlSetState($cHideMSUpdates, $GUI_DISABLE)
        GUICtrlSetState($rAll, $GUI_DISABLE)
        GUICtrlSetState($r32, $GUI_DISABLE)
        GUICtrlSetState($r64, $GUI_DISABLE)
        GUICtrlSetState($bExport, $GUI_DISABLE)
    EndIf
EndFunc

Func ReadGUI($guiMsg)
    Switch $guiMsg
        Case $bConnect
            FillListView()
        Case $bFilter
            FillListView()
        Case $rAll To $r64
            FillListView()
        Case $cHideMSUpdates
            FillListView()
        Case $bExport
            ExportCSV()
        Case $ListView
            _GUICtrlListView_SortItems($ListView, GUICtrlGetState($ListView))
        Case $rcCopyLine
            ClipPut(_GUICtrlListView_GetItemTextString($ListView, -1))
        Case $rcCopyName
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $nameCol))
        Case $rcCopyVer
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $verCol))
        Case $rcCopyPub
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $pubCol))
        Case $rcCopyGUID
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $guidCol))
        Case $rcCopyUninst
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $uninstCol))
        Case $rcCopyLoc
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $locCol))
        Case $rcCopySrc
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $srcCol))
        Case $rcCopyKey
            ClipPut(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $keyCol))
        Case $rcJumpLoc
            JumpToDir(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $locCol))
        Case $rcJumpSrc
            JumpToDir(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $srcCol))
        Case $rcJumpKey
            If $computer <> "" Then
                MsgBox($MB_ICONWARNING, "", "Jumping to remote registry not supported.", 10)
            Else
                Local $file = ""
                If $osArch == "AMD64" Then
                    FileInstall("C:\DRIVE\APPS\REGSCANNER\regscanner_x64.exe", @TempDir & "\", 0)
                    $file = "regscanner_x64.exe"
                Else
                    FileInstall("C:\DRIVE\APPS\REGSCANNER\regscanner.exe", @TempDir & "\", 0)
                    $file = "regscanner.exe"
                EndIf
                Local $key = ExpandKey(_GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $keyCol))
                Run(@TempDir & "\" & $file & " /regedit """ & $key & """")
            EndIf
        Case $rcUninstall
            If $computer <> "" Then
                MsgBox($MB_ICONWARNING, "", "Remote uninstall not supported.", 10)
            Else
                Local $ans = MsgBox($MB_ICONQUESTION + $MB_YESNO + $MB_DEFBUTTON2, "", "Uninstall """ & _GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $nameCol) & """", 10)
                If $ans == $IDYES Then
                    Local $uninstallStr = _GUICtrlListView_GetItemText($ListView, Int(_GUICtrlListView_GetSelectedIndices($ListView),1), $uninstCol)
                    RunWait($uninstallStr)
                    FillListView()
                EndIf
            EndIf
        Case $rcCancel
            Return False ; Stop search operation.
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
    Return True ; Continue search operation.
EndFunc

Func JumpToDir($dir)
    If $computer <> "" And StringInStr($dir, ":\", 0, 1) Then
        $dir = StringReplace($dir, ":", "$", 1)
        $dir = "\\" & $computer & "\" & $dir
    EndIf
    If Not FileExists($dir) Then
        MsgBox($MB_ICONWARNING, "", "Unable to locate:" & @CRLF & @CRLF & $dir, 10)
    Else
        Run("explorer.exe """ & $dir & """", @SystemDir)
    EndIf
EndFunc

Func ExpandKey($key)
    If StringLeft($key, 4) == "HKLM" Then
        $key = StringReplace($key, "HKLM", "HKEY_LOCAL_MACHINE", 1)
    ElseIf StringLeft($key, 4) == "HKCU" Then
        $key = StringReplace($key, "HKCU", "HKEY_CURRENT_USER", 1)
    ElseIf StringLeft($key, 3) == "HKU" Then
        $key = StringReplace($key, "HKU",  "HKEY_USERS", 1)
    EndIf
    Return $key
EndFunc

Func AddListEntry($computer, ByRef $dataBlob)
    If $dataBlob <> "" Then
        $dataBlob = StringStripWS($dataBlob, $STR_STRIPTRAILING)
        Local $temp = StringSplit($dataBlob, "|", $STR_NOCOUNT)
        Local $lineArray[1][UBound($temp)]
        For $i = 0 To UBound($temp)-1
            $lineArray[0][$i] = $temp[$i]
        Next
        _GUICtrlListView_AddArray($ListView, $lineArray)
        $dataBlob = ""
    EndIf
EndFunc

Func BuildLine($ListView, $key, $name, $ver, $pub, $uninst, $date, $loc, $src)
    Local $guid, $msi, $arch, $temp

    If $date <> "" Then
        $date = _StringInsert($date, "/", 4)
        $date = _StringInsert($date, "/", 7)
        $date = _DateTimeFormat($date, 2)
    EndIf

    If StringInStr($uninst, "msiexec", 0) > 0 Then
        $temp = _StringBetween($uninst, "{", "}")
        $guid = "{" & $temp[0] & "}"
        $msi = "Y"
    Else
        $guid = ""
        $msi = "N"
    EndIf

    If $osArch == "AMD64" Then
        If StringInStr($key, "HKLM\SOFTWARE\WOW6432NODE") > 0 Then
            $arch = "x86"
        Else
            $key = StringReplace($key, "HKLM64", "HKLM", 1)
            $arch = "x64"
        EndIf
    Else
        $key = StringReplace($key, "HKLM64", "HKLM", 1)
        $arch = "x86"
    EndIf

    If $computer <> "" Then
        $key = StringReplace($key, "\\" & $computer & "\", "", 1, 0)
    EndIf

    Return $name & "|" & $ver & "|" & $pub & "|" & $arch & "|" & $msi & "|" & $guid & "|" & $uninst & "|" & $date & "|" & $loc & "|" & $src & "|" & $key & @CRLF
EndFunc

Func ExportCSV()
    Local $filePath = FileSaveDialog("Specify Output File", @DesktopDir, "Comma-Separated Value(*.csv)", $FD_PROMPTOVERWRITE)

    If @error == 0 Then
        $compare = StringCompare(StringRight($filePath, 4), ".csv", 0)
        If $compare <> 0 Then
            $filePath &= ".csv"
        EndIf

        Local $lvColCount = _GUICtrlListView_GetColumnCount($ListView) - 1
        Local $lvRowCount = _GUICtrlListView_GetItemCount($ListView) - 1
        Local $csvString = ""
        For $i = 0 To $lvRowCount
            For $j = 0 To $lvColCount
                $csvString &= '"' & StringReplace(_GUICtrlListView_GetItemText($ListView, $i, $j), '"', '"' & '"') & '"'
                If $j < $lvColCount Then $csvString &= ','
            Next
            If $i < $lvRowCount Then $csvString &= @CRLF
        Next
        Local $fileHandle = FileOpen($filePath, 10)
        If $fileHandle <> -1 Then
            Local $header = "Name,Version,Publisher,Arch,MSI,GUID,Uninstall String,Install Date,Install Source,Install Location,Uninstall Key"
            FileWrite($fileHandle, $header & @CRLF & StringStripWS($csvString, $STR_STRIPTRAILING))
            FileClose($fileHandle)
            MsgBox($MB_ICONINFORMATION, "", "Export successful.", 10)
        Else
            MsgBox(16, "", "Export unsuccessful.", 10)
        EndIf
    EndIf
EndFunc
Le lien pour le téléchargement de l’outil compilé, joint dans le poste original est mort.

Alors j’ai utilisé le code original et je lui ai appliqué la correction mentionné, ainsi que télécharger les exécutables installés afin d’être en mesure de le compiler à nouveau.

L’outil est fonctionnel, mais j’aurais besoin de l’utiliser sans le GUI et les menus.

Il faudrait que je puisse seulement fournir en paramètre de ligne de commande, le nom du poste à inventorié et le chemin ou sauvegarder le résultat (fichier « csv ») et obtenir un code de retour selon le résultat de l’exécution, ok, time out, accès refusé.

Puisque mon expertise avec AutoIT est très limitée et que la structure de menus est elle assez complexe, j’aurais besoin d’un sérieux coup de main pour y parvenir.

Merci.

Collect_Uninstall_Keys.zip
(790.02 Kio) Téléchargé 22 fois
Collect_Uninstall_Keys.zip
(790.02 Kio) Téléchargé 22 fois
Avatar du membre
mikell
Modérateur
Modérateur
Messages : 6112
Enregistré le : dim. 29 mai 2011 17:32
Localisation : Deep Cévennes
Status : Hors ligne

Re: Comment convertir un script avec GUI en script avec arguments sans GUI

#2

Message par mikell »

Dommage, se priver de la gui c'est se priver des options proposées
Pour ne pas perdre cette possibilité, un bon plan serait de vérifier $CmdLine[0] au démarrage. Si pas de paramètres, on affiche la gui, et s'il y en a, on ne l'affiche pas et on écrit direct le .csv. Dans ce dernier cas la gui pourrait être simplement cachée (non visible) ou carrément supprimée
Mais de toute façon il va te falloir mettre le mains dans le cambouis :mrgreen: pour définir les paramètres et avoir le résultat exact souhaité

Un exemple du code sans sa gui :

#include <Constants.au3>
#include <Date.au3>
#include <String.au3>
#Include <Array.au3>

Global $done = 0
Global $osArch = ""
Global $computer = ""
Global $srchTrm = ""

If $CmdLine[0] > 0 And $CmdLine[0] < 3 Then
   $computer = $CmdLine[2]
   $srchTrm  = $CmdLine[1]
EndIf

_GetData()

While 1
   Sleep(10)
   If $done = 1 Then Exit
WEnd

;=====================================

Func _GetData()
    Local $i = 0
    Local $key = "", $value = "", $name = "", $dataBlob = ""

    Local $regRoot = "HKLM64\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall"
    Local $regRoot32 = "HKLM\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall"
    Local $regArch = "HKLM64\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"

    Local $pingErr = 0
    If $computer <> "" And StringCompare($computer, @ComputerName) <> 0 And StringCompare($computer, "localhost") <> 0 And $computer <> "127.0.0.1" Then
        $regRoot    = "\\" & $computer & "\" & $regRoot
        $regRoot32  = "\\" & $computer & "\" & $regRoot32
        $regArch    = "\\" & $computer & "\" & $regArch
        Ping($computer, 1000)
        $pingErr = @error
    EndIf

    If $pingErr <> 0 Then
     ;   GUICtrlSetData($lResults, "Unable to connect to " & $computer & ".")
    Else
        $osArch = StringUpper(RegRead($regArch, "PROCESSOR_ARCHITECTURE"))
        If $osArch == "AMD64"  Then $regRoot = $regRoot32
        Local $hideUpdates = 1

        While 1
            $i += 1
            $key = $regRoot & "\" & RegEnumKey($regRoot, $i)
            Switch @error
                Case 0
                   $name = RegRead($key, "DisplayName")
              If @error == 0 And $name <> "" Then
                $pub = RegRead($key, "Publisher")
                $uninst = RegRead($key, "UninstallString")
                If ($srchTrm == "" Or StringInStr($name, $srchTrm) Or StringInStr($pub, $srchTrm) Or StringInStr($uninst, $srchTrm)) Then
                  If $hideUpdates Then
                     If Not (StringInStr($pub, "Microsoft", 0, 1) And (StringInStr($name, "Update", 0, 1) Or StringInStr($name, "Hotfix", 0, 1))) Then
                       $dataBlob &= BuildLine($key, $name, RegRead($key, "DisplayVersion"), $pub, $uninst, RegRead($key, "InstallDate"), RegRead($key, "InstallLocation"), RegRead($key, "InstallSource"))
                     EndIf
                  Else
                     $dataBlob &= BuildLine($key, $name, RegRead($key, "DisplayVersion"), $pub, $uninst, RegRead($key, "InstallDate"), RegRead($key, "InstallLocation"), RegRead($key, "InstallSource"))
                  EndIf
                EndIf
              EndIf
                Case -1
                    If $osArch == "AMD64" Then
                        $regRoot = $regRoot32
                        $i = 0
                    Else
                        ExitLoop
                    EndIf
                Case 3
               ;     GUICtrlSetData($lResults, "Unable to connect to remote registry.")
                    ExitLoop
            EndSwitch
        WEnd
    EndIf

    If $computer == "" Then
        $dataBlob = StringStripWS($dataBlob, $STR_STRIPTRAILING)
        Local $lines = StringSplit($dataBlob, @CRLF, $STR_ENTIRESPLIT + $STR_NOCOUNT)
        If $dataBlob <> "" Then _ArraySort($lines)
    EndIf

   ExportCSV($lines)
EndFunc


Func BuildLine($key, $name, $ver, $pub, $uninst, $date, $loc, $src)
    Local $guid, $msi, $arch, $temp

    If $date <> "" Then
        $date = _StringInsert($date, "/", 4)
        $date = _StringInsert($date, "/", 7)
        $date = _DateTimeFormat($date, 2)
    EndIf

    If StringInStr($uninst, "msiexec", 0) > 0 Then
        $temp = _StringBetween($uninst, "{", "}")
        $guid = "{" & $temp[0] & "}"
        $msi = "Y"
    Else
        $guid = ""
        $msi = "N"
    EndIf

    If $osArch == "AMD64" Then
        If StringInStr($key, "HKLM\SOFTWARE\WOW6432NODE") > 0 Then
            $arch = "x86"
        Else
            $key = StringReplace($key, "HKLM64", "HKLM", 1)
            $arch = "x64"
        EndIf
    Else
        $key = StringReplace($key, "HKLM64", "HKLM", 1)
        $arch = "x86"
    EndIf

    If $computer <> "" Then
        $key = StringReplace($key, "\\" & $computer & "\", "", 1, 0)
    EndIf

    Return $name & "|" & $ver & "|" & $pub & "|" & $arch & "|" & $msi & "|" & $guid & "|" & $uninst & "|" & $date & "|" & $loc & "|" & $src & "|" & $key & @CRLF
EndFunc



Func ExportCSV($lines)
    Local $filePath = FileSaveDialog("Specify Output File", @DesktopDir, "Comma-Separated Value(*.csv)", $FD_PROMPTOVERWRITE)

    If @error == 0 Then
        $compare = StringCompare(StringRight($filePath, 4), ".csv", 0)
        If $compare <> 0 Then
            $filePath &= ".csv"
        EndIf

        Local $lvRowCount = UBound($lines) - 1
        Local $csvString = ""
        Local $separator = ","

        For $i = 0 To $lvRowCount
      $tmp = StringSplit($lines[$i], "|", $STR_NOCOUNT)
      Local $lvColCount = UBound($tmp) - 1
            For $j = 0 To $lvColCount
                $csvString &= '"' & StringReplace($tmp[$j], '"', '"' & '"') & '"'
                If $j < $lvColCount Then $csvString &= $separator
            Next
            If $i < $lvRowCount Then $csvString &= @CRLF
        Next

        Local $fileHandle = FileOpen($filePath, 10+32)
        If $fileHandle <> -1 Then
            Local $header = "Name" & $separator & "Version" & $separator & "Publisher" & $separator & _
                  "Arch" & $separator & "MSI" & $separator & "GUID" & $separator & _
                  "Uninstall String" & $separator & "Install Date" & $separator & _
                  "Install Source" & $separator & "Install Location" & $separator & "Uninstall Key"
            FileWrite($fileHandle, $header & @CRLF & StringStripWS($csvString, $STR_STRIPTRAILING))
            FileClose($fileHandle)
            MsgBox($MB_ICONINFORMATION, "", "Export successful.", 10)
        Else
            MsgBox(16, "", "Export unsuccessful.", 10)
        EndIf
    EndIf
   $done = 1
EndFunc
 
" L'échec est le fondement de la réussite. " (Lao-Tseu )
" Plus ça rate, plus on a de chances que ça marche " (les Shadoks )
Répondre