View Single Post
Old 04-20-2008, 09:50 PM   #6 (permalink)
Jaytee Bushwacker
FoH Member with a rod in his pants
 
Jaytee Bushwacker's Avatar
 
Join Date: Jan 2002
Location: Forest, MS
Posts: 243
+0 Internets
Here is the code I use in my app to create a thread and do the backup/clear operations. It took a couple days but I finally have it working pretty well for the local connection, I can make it work for remotes but I'm trying to decide how I want to design my connection function to accept a username/password.



Code:
Private Sub cmdWMIArchive_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdWMIArchive.Click Dim saveFileName As String = Me.archiveDrive & "" & Me.archiveRootDirectory & "" & Me.txtSaveFile.Text Dim computer As String = Me.txtComputerName.Text Dim args(1) As Object args(0) = saveFileName args(1) = computer Me.rtbStatus.Clear() 'BackupAndClearLog(args) Dim threadA As New Thread(New ParameterizedThreadStart(AddressOf backupClearLog)) threadA.Start(args) End Sub Sub backupClearLog(ByVal args As Object) Dim scope As New System.Management.ManagementScope Dim logSearcher As New System.Management.ManagementObjectSearcher Dim logfiles As System.Management.ManagementObjectCollection Dim logfile As System.Management.ManagementObject Dim inParams As System.Management.ManagementBaseObject Dim outParams As System.Management.ManagementBaseObject Dim result As UInt32 Dim backupFile As String = args(0) Dim computer As String = args(1) Dim updateArgs(1) As Object Try 'My attempt at creating a dll with WMI functions 'it just executes the following manual way code myWMI.openConnection(scope, computer) 'The Manual way 'scope.Options.EnablePrivileges = True 'scope.Path.Path = "\\.\root\cimv2" 'scope.Connect() 'I'm calling a sub that updates a richtextbox control on my UI with connection status Me.Invoke(New connectedToDelegate(AddressOf connectedTo), scope) logSearcher.Query.QueryString = "Select * from win32_NTEventLogFile WHERE LogFileName='security'" logfiles = logSearcher.Get() For Each logfile In logfiles 'Can't update UI from a thread directly next few lines do it via invoke 'rtbStatus.AppendText(logfile.Item("Name") & ControlChars.Cr) updateArgs(0) = False updateArgs(1) = logfile.Item("Name") & ControlChars.Cr Me.Invoke(New updateStatusDelegate(AddressOf updateStatus), updateArgs) 'This block actually does the backup inParams = logfile.GetMethodParameters("BackupEventLog") inParams.Item("ArchiveFileName") = backupFile outParams = logfile.InvokeMethod("BackupEventLog", inParams, Nothing) result = outParams.Item("returnValue") 'Can't update UI from a thread directly next few lines do it via invoke 'Me.rtbStatus.AppendText("BackupEventLog " & myWMI.exitCodeMessage(outParams.Item("returnValue"), "win32_NTEventLogFile") & ControlChars.Cr) updateArgs(0) = False updateArgs(1) = "BackupEventLog " & myWMI.exitCodeMessage(outParams.Item("returnValue"), "win32_NTEventLogFile") & ControlChars.Cr Me.Invoke(New updateStatusDelegate(AddressOf updateStatus), updateArgs) 'If backup was successful result will be 0 - only then do we clear If result = 0 Then 'Have to do the same thing as backup to clear the log just call the ClearEventLog method 'There are no parameters for the clear method but if we don't reinit inParams it will hose up inParams = logfile.GetMethodParameters("ClearEventLog") outParams = logfile.InvokeMethod("ClearEventLog", inParams, Nothing) 'Can't update UI from a thread directly next few lines do it via invoke 'Me.rtbStatus.AppendText("ClearEventLog " & myWMI.exitCodeMessage(outParams.Item("returnValue"), "win32_NTEventLogFile") & ControlChars.Cr) updateArgs(0) = False updateArgs(1) = "BackupEventLog " & myWMI.exitCodeMessage(outParams.Item("returnValue"), "win32_NTEventLogFile") & ControlChars.Cr Me.Invoke(New updateStatusDelegate(AddressOf updateStatus), updateArgs) End If Next Catch ex As Exception 'Can't update UI from a thread directly next few lines do it via invoke 'Me.rtbStatus.AppendText(ex.Message & ControlChars.Cr) updateArgs(0) = False updateArgs(1) = ex.Message & ControlChars.Cr Me.Invoke(New updateStatusDelegate(AddressOf updateStatus), updateArgs) End Try End Sub
__________________
Jaytee Bushwacker is offline   Reply With Quote

 
Uberguilds Network