Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsVB SyntaxEnterprise DevelopmentDatabase AccessControlsCOMWin APICrystal ReportDeploymentGeneralGeneral 2
Related Topics
VB.NET / ASP.NETMS SQL ServerMS AccessOther Database ProductsMore Topics ...

VB Forum / COM / April 2008



Tip: Looking for answers? Try searching our database.

Help - vbs WMI script to vb.net 2005 conversion

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
JohnBates - 16 Apr 2008 04:56 GMT
Problem:
I need to backup and clear the security event log.  I have this working via
a vbsscript which I will post below.  However while I can use this script
manually it is not user friendly and my end users who have to perform the
backup and clear chore weekly are the "where is the button" types.

I have written a vb.net 2005 gui as a front end that can launch my script
and run it ok but the problem is since it is a script running in a shell
object I have no way to return status to my vb.net program saying it succeded
or failed or even to know when the shell exits.

So I decided to look into writing performing the steps via vb.net code.  I
can successfully create a WMI connection and (on the local machine) I can
even list out all log files by code shown below.  What I cannot do is execute
the BackupEventLog method via WMI.  I get access denied, which I have
researched and I feel the reason is that the WMI connection does not have the
privileges enabled for backup and security.  If you look at the vbs script
below you will see where it addes (Backup, security) into the moniker for the
object and I believe allows the execution of the method.

I did find out there that you are supposed to use the ".EnablePrivileges =
True" option but I also found that .NET 1.1 messed that option up.  Someone
please help!

CREATE CONNECTION CODE:
===================BEGIN
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

       With myConnectionOptions
           .Impersonation = Management.ImpersonationLevel.Impersonate

           '* Use next line for XP
           .Authentication = System.Management.AuthenticationLevel.Packet
           .EnablePrivileges = True

           'Cannot specify username/password for local connections
           '.Username = Me.txtUsername.Text
           '.Password = Me.txtPassword.Text
        End With

       '* "." is the string for a local connection
       Dim myServerName As String = Me.txtServer.Text

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

       '* connect to WMI namespace
       myManagementScope.Connect()
       If myManagementScope.IsConnected = False Then
           rtbStatus.AppendText("Could not connect to WMI namespace on " &
myServerName & ControlChars.Cr)
       Else
           rtbStatus.AppendText("Connected to WMI namespace on " &
myServerName & ControlChars.Cr)
       End If
   End Sub
===================END

LIST ALL LOG FILES CODE:
===================BEGIN
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
       Dim logfileSearcher As System.Management.ManagementObjectSearcher
       Dim logfiles As System.Management.ManagementObjectCollection
       Dim logfile As System.Management.ManagementObject

       logfileSearcher = New
System.Management.ManagementObjectSearcher(myManagementScope.Path.ToString,
"Select * from win32_NTEventLogFile")

       '* execute query
       logfiles = logfileSearcher.Get()

       Try

           For Each logfile In logfiles

               rtbStatus.AppendText("Found logfile " &
logfile.GetPropertyValue("FileName").ToString & " which is the " &
logfile.GetPropertyValue("LogfileName").ToString & " event log" &
ControlChars.Cr)

          'INSERT BACKUP CODE HERE (SHOWN BELOW)

           Next

       Catch ex As Exception
           rtbStatus.AppendText("Error Encountered: " & ex.ToString &
ControlChars.Cr)
       End Try
   End Sub
===================END

FAILING BACKUP METHOD INVOCATION
===================BEGIN
Dim inParams As Management.ManagementBaseObject =
logfile.GetMethodParameters("BackupEventLog")

               inParams("ArchiveFileName") = "c:\testing.evt"

               Dim outParams As Management.ManagementBaseObject =
logfile.InvokeMethod("BackupEventLog", inParams, Nothing)
===================END

WORKING VBS SCRIPT
===================BEGIN
'Arguments
fileName = WScript.Arguments.Item(0)
logType = WScript.Arguments.Item(1)
fullPathName = filename & ".evt"

'NOTE: for this to work on a normal user account they must have following
rights
'Manage Auditing and Secuirty
'Generate Security Audits

strComputer = "."
Set objWMIService = GetObject("winmgmts:" &
"{impersonationLevel=impersonate,(Backup,security)}!\\" & strComputer &
"\root\cimv2")
Set colLogFiles = objWMIService.ExecQuery ("SELECT * FROM
Win32_NTEventLogFile WHERE LogFileName='" & logType & "'")

For Each objLogfile in colLogFiles
errBackupLog = objLogFile.BackupEventLog(fullPathName)

If errBackupLog = 0 Then
   Wscript.Echo "The Security event log was backed up."
   objLogFile.ClearEventLog()
End If
If errBackupLog = 8 Then
   Wscript.Echo "Privilege missing!"
End If
If errBackupLog = 21 Then
   Wscript.Echo "Invalid Parameter in call"
End If

If errBackupLog = 183 Then
   Wscript.Echo "The archive file already exists."
End If
Next
===================END
Kevin Provance - 16 Apr 2008 05:08 GMT
[Canned response]
This is a VB "classic" newsgroup. Questions about VB.NET (including VB
2005/2008 and VB Express, which have dropped .NET from their names) are
off-topic here.

Please ask .NET questions in newsgroups with "dotnet" in their names. The
*.vb.* groups are for VB6 and earlier. If you don't see the *.dotnet.*
groups on your news server, connect directly to the Microsoft server:
msnews.microsoft.com.

For questions specific to the VB.NET language, use this group:
microsoft.public.dotnet.languages.vb

Please note that things like controls and data access, which have their own
subgroups in the Classic VB hierarchy, are not language-specific in .NET, so
you should look for groups like these:
microsoft.public.dotnet.framework.windowsforms.controls
microsoft.public.dotnet.framework.adonet
(Note that "vb" is not present in the group name.)

| Problem:
| I need to backup and clear the security event log.  I have this working via
[quoted text clipped - 64 lines]
|
|        logfileSearcher = New

System.Management.ManagementObjectSearcher(myManagementScope.Path.ToString,
| "Select * from win32_NTEventLogFile")
|
[quoted text clipped - 70 lines]
| Next
| ===================END
JohnBates - 16 Apr 2008 05:21 GMT
I'm sorry this was my first post to MSDN.  I'll post in those forums but I
don't think I can remove this one.

> [Canned response]
> This is a VB "classic" newsgroup. Questions about VB.NET (including VB
[quoted text clipped - 166 lines]
> | Next
> | ===================END
Kevin Provance - 16 Apr 2008 05:31 GMT
You cannot, but don't worry about it.  I was simply pointing you in the
right direction.  Go forward my son, and my the Schwartz be with you.

- Kev

| I'm sorry this was my first post to MSDN.  I'll post in those forums but I
| don't think I can remove this one.
[quoted text clipped - 93 lines]
| > |
| > |        logfileSearcher = New

System.Management.ManagementObjectSearcher(myManagementScope.Path.ToString,
| > | "Select * from win32_NTEventLogFile")
| > |
[quoted text clipped - 70 lines]
| > | Next
| > | ===================END
mayayana - 16 Apr 2008 13:51 GMT
If you don't have luck in the .Net groups
you can also try:

microsoft.public.scripting.vbscript
microsoft.public.win32.programmer.wmi

(The former group is probably a better bet. A lot
of people there are network admins who use WMI,
whereas the WMI group has very little activity.)

 You're in an awkward position, in that you're
involving 3 languages that have no inherent
relationship with each other, and there's no
group for VBS/WMI -> .Net.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.