By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,219 Members | 2,140 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,219 IT Pros & Developers. It's quick & easy.

WMI query for events

P: n/a
This code snippet fails with 'Specified cast is not valid' when running
the query to get application warning events in the last 10 days...

What am I doing wrong?
HankC
Dim objWMIService As Object

objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")

Dim colEvents As Collection

strquery = "Select * from Win32_NTLogEvent Where Logfile =
'Application' and Type = 'Warning' and TimeWritten >=
'20050916143529.000000-300'"
'''the next line fails: Specified cast is not valid.
colEvents = objWMIService.ExecQuery(strquery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim objEvent As Object
For Each objEvent In colEvents
TextBox1.Text = TextBox1.Text & objEvent.Type & vbVrLf
Next
End If

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
Hi,

Here is how I would do that.

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_NTLogEvent Where " & _

"Logfile ='Application' and Type = 'Warning' and TimeWritten
='20050916143529.000000-300'")
moReturn = moSearch.Get

For Each mo In moReturn

Dim strOut As String

strOut = String.Format("Date {0} Message {1}", mo("TimeWritten"),
mo("Message"))

Trace.WriteLine(strOut)

Next

Ken

-------------------

"HankC" <cl****@missouri.edu> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com... This code snippet fails with 'Specified cast is not valid' when running
the query to get application warning events in the last 10 days...

What am I doing wrong?
HankC
Dim objWMIService As Object

objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")

Dim colEvents As Collection

strquery = "Select * from Win32_NTLogEvent Where Logfile =
'Application' and Type = 'Warning' and TimeWritten >=
'20050916143529.000000-300'"
'''the next line fails: Specified cast is not valid.
colEvents = objWMIService.ExecQuery(strquery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim objEvent As Object
For Each objEvent In colEvents
TextBox1.Text = TextBox1.Text & objEvent.Type & vbVrLf
Next
End If

Nov 21 '05 #2

P: n/a

Ken Tucker [MVP] wrote:
Hi,

Here is how I would do that.

Dim moReturn As Management.ManagementObjectCollection

Dim moSearch As Management.ManagementObjectSearcher

Dim mo As Management.ManagementObject

moSearch = New Management.ManagementObjectSearcher("Select * from
Win32_NTLogEvent Where " & _

"Logfile ='Application' and Type = 'Warning' and TimeWritten
>='20050916143529.000000-300'")


moReturn = moSearch.Get

For Each mo In moReturn

Dim strOut As String

strOut = String.Format("Date {0} Message {1}", mo("TimeWritten"),
mo("Message"))

Trace.WriteLine(strOut)

Next

Ken

-------------------

"HankC" <cl****@missouri.edu> wrote in message
news:11**********************@g49g2000cwa.googlegr oups.com...
This code snippet fails with 'Specified cast is not valid' when running
the query to get application warning events in the last 10 days...

What am I doing wrong?
HankC
Dim objWMIService As Object

objWMIService =
GetObject("winmgmts:{impersonationLevel=impersonat e}!\\.\root\cimv2")

Dim colEvents As Collection

strquery = "Select * from Win32_NTLogEvent Where Logfile =
'Application' and Type = 'Warning' and TimeWritten >=
'20050916143529.000000-300'"
'''the next line fails: Specified cast is not valid.
colEvents = objWMIService.ExecQuery(strquery, "WQL",
wbemFlagReturnImmediately + wbemFlagForwardOnly)

Dim objEvent As Object
For Each objEvent In colEvents
TextBox1.Text = TextBox1.Text & objEvent.Type & vbVrLf
Next
End If


Thanks, Ken. That did the trick...

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.