Feedback

VB - Installierte Programme und Hotfixes per WMI abfragen

Veröffentlicht von am 10.09.2008
(1 Bewertungen)
Dieses Beispiel liefert die installierte SW eines Rechners und dessen Hotfixes. Mit entsprechenden Rechten lässt sich das auch remote abfragen. Die Ergebnisse werden in einer lokalen Tabelle im Dataset gespeichert.

fUpdateLog speichert den übergebenene String in ein Textfile (nicht enthalten)

Tabelle:
WMIDataID - Autowert
Hostname - String
Property - String
Value - String
Type - String
Private Sub fQueryHost(ByVal sComputer As String)
        On Error Resume Next
        Dim myConnectionOptions As New System.Management.ConnectionOptions
        Dim myManagementScope As System.Management.ManagementScope
        Dim myObjectSearcher As System.Management.ManagementObjectSearcher
        Dim myCollection As System.Management.ManagementObjectCollection
        Dim myObject As System.Management.ManagementObject
        Dim iCounter As Long

        With myConnectionOptions
            .Impersonation = System.Management.ImpersonationLevel.Impersonate
            .Authentication = System.Management.AuthenticationLevel.Packet
        End With

        myManagementScope = New System.Management.ManagementScope("\\" & _
          sComputer & "\root\cimv2", myConnectionOptions)

        ' Connect WMI
        myManagementScope.Connect()
        If myManagementScope.IsConnected = False Then
            fUpdateLog("Kann keine Verbindung zum WMI Namespace auf " & sComputer & "aufnehmen.")
            Exit Sub
        Else
            fUpdateLog("[" & sComputer & "] Mit WMI verbunden...")
        End If

        myObjectSearcher = New System.Management.ManagementObjectSearcher( _
          myManagementScope.Path.ToString, "SELECT * FROM WIN32_PRODUCT")

        ' Execute
        myCollection = myObjectSearcher.Get()
        fUpdateLog("[" & sComputer & "] WMI Abfrage für Software...")

        ' Read
        iCounter = 1
        For Each myObject In myCollection
            fUpdateLog("[" & sComputer & "] Lese Software " & iCounter & "/" & myCollection.Count, False)

            myTable.AddWMIDataRow(sComputer, "Name", myObject.GetPropertyValue("Name"), "SOFTWARE")
            If myObject.GetPropertyValue("AssignmentType") = 0 Then
                myTable.AddWMIDataRow(sComputer, "AssignmentType", "AssignmentType: perUser", "SOFTWARE")
            Else
                myTable.AddWMIDataRow(sComputer, "AssignmentType", "AssignmentType: perMachine", "SOFTWARE")
            End If

            myTable.AddWMIDataRow(sComputer, "Caption", myObject.GetPropertyValue("Caption"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "Description", myObject.GetPropertyValue("Description"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "IdentifyingNumber", myObject.GetPropertyValue("IdentifyingNumber"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "InstallDate", myObject.GetPropertyValue("InstallDate"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "InstallLocation", myObject.GetPropertyValue("InstallLocation"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "InstallSource", myObject.GetPropertyValue("InstallSource"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "Language", myObject.GetPropertyValue("Language"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "LocalPackage", myObject.GetPropertyValue("LocalPackage"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "PackageCode", myObject.GetPropertyValue("PackageCode"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "PackageName", myObject.GetPropertyValue("PackageName"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "ProductID", myObject.GetPropertyValue("ProductID"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "Vendor", myObject.GetPropertyValue("Vendor"), "SOFTWARE")
            myTable.AddWMIDataRow(sComputer, "Version", myObject.GetPropertyValue("Version"), "SOFTWARE")
            iCounter = iCounter + 1
        Next

        myObjectSearcher = New System.Management.ManagementObjectSearcher( _
          myManagementScope.Path.ToString, "SELECT * FROM WIN32_QUICKFIXENGINEERING")

        ' Execute
        myCollection = myObjectSearcher.Get()
        fUpdateLog("[" & sComputer & "] WMI Abfrage für Hotfixes...")

        ' Read
        iCounter = 1
        For Each myObject In myCollection
            fUpdateLog("[" & sComputer & "] Lese Hotfix " & iCounter & "/" & myCollection.Count, False)

            myTable.AddWMIDataRow(sComputer, "HotFixID", myObject.GetPropertyValue("HotFixID"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "Caption", myObject.GetPropertyValue("Caption"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "FixComments", myObject.GetPropertyValue("FixComments"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "InstallDate", myObject.GetPropertyValue("InstallDate"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "InstalledBy", myObject.GetPropertyValue("InstalledBy"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "InstalledOn", myObject.GetPropertyValue("InstalledOn"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "Name", myObject.GetPropertyValue("Name"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "ServicePackInEffect", myObject.GetPropertyValue("ServicePackInEffect"), "HOTFIX")
            myTable.AddWMIDataRow(sComputer, "Status", myObject.GetPropertyValue("Status"), "HOTFIX")
            iCounter = iCounter + 1
        Next
    End Sub
Abgelegt unter Installierte Programme, Hotfixes, WMI.

1 Kommentare zum Snippet

Volker Steitz schrieb am 11.09.2008:

With myConnectionOptions
.Impersonation = System.Management.ImpersonationLevel.Impersonate
.Authentication = System.Management.AuthenticationLevel.Packet
End With


Kannst Du auf dem lokalen System Weglassen. Willst Du ein Remote Remote System abfragen. musst Du in den Connection Options noch Username, Userdomain und Password übergebem - sonst scheitert es.
 

Logge dich ein, um hier zu kommentieren!