Discussion:
How to query for "service does not exist"
(too old to reply)
Kevin K
2007-02-03 16:48:00 UTC
Permalink
I'm not a developer so please bear with me :)

I created a WMI query (to be used by Group Policy) to check for the
non-existence of a service. For example:

SELECT * FROM Win32_Service where Name <> "myservice"

The goal is to target machines that do NOT have a specific service present.

The above query I suspect is saying "sure -- I have quite a few services
that are not equal to "myservice".

I don't think WMI has a NOT logic operator as it doesn't seem to work?

In any case, how would one write a query that can be used as a WMI Filter
for Group Policy, that will only select computers that do NOT have
"myservice"?

Thanks!
Chris Richards
2007-02-03 17:28:37 UTC
Permalink
WQL does include the <> comparison operator for the not equals
operation, but your suspicion is correct; the WMI query is returning the
list of all services that are NOT "myservice". To do what you want,
simply use the following query:

Select * from Win32_Service where Name = "myservice"

When you run the query, if you don't get back an object, the service
doesn't exit.

HTH

Later,
Chris
Post by Kevin K
I'm not a developer so please bear with me :)
I created a WMI query (to be used by Group Policy) to check for the
SELECT * FROM Win32_Service where Name <> "myservice"
The goal is to target machines that do NOT have a specific service present.
The above query I suspect is saying "sure -- I have quite a few services
that are not equal to "myservice".
I don't think WMI has a NOT logic operator as it doesn't seem to work?
In any case, how would one write a query that can be used as a WMI Filter
for Group Policy, that will only select computers that do NOT have
"myservice"?
Thanks!
Kevin K
2007-02-03 19:08:00 UTC
Permalink
Thanks but unfortunately WMI filters (group policy) only react where the
result is TRUE.

Is is possible to write a WMI filter/query that can be TRUE when a service
does NOT exist?
Post by Chris Richards
WQL does include the <> comparison operator for the not equals
operation, but your suspicion is correct; the WMI query is returning the
list of all services that are NOT "myservice". To do what you want,
Select * from Win32_Service where Name = "myservice"
When you run the query, if you don't get back an object, the service
doesn't exit.
HTH
Later,
Chris
Post by Kevin K
I'm not a developer so please bear with me :)
I created a WMI query (to be used by Group Policy) to check for the
SELECT * FROM Win32_Service where Name <> "myservice"
The goal is to target machines that do NOT have a specific service present.
The above query I suspect is saying "sure -- I have quite a few services
that are not equal to "myservice".
I don't think WMI has a NOT logic operator as it doesn't seem to work?
In any case, how would one write a query that can be used as a WMI Filter
for Group Policy, that will only select computers that do NOT have
"myservice"?
Thanks!
Chris Richards
2007-02-05 21:16:01 UTC
Permalink
I think you may need to query the GPO newsgroups for this, as it seems
like this is more of an issue with a GPO limitation than a WMI one. I
don't see any way to create a query that will do what you want. You can
create a query that will let you know if a service exists or not; we
have already shown that. The GPO logic simply won't work with the
information that WMI returns in this instance.

I'm terribly sorry, but I don't see any way around this with WMI.

Of course, I'm also not a WMI guru, so there is probably something I am
missing. :-/

Later,
Chris
Post by Kevin K
Thanks but unfortunately WMI filters (group policy) only react where the
result is TRUE.
Is is possible to write a WMI filter/query that can be TRUE when a service
does NOT exist?
Post by Chris Richards
WQL does include the <> comparison operator for the not equals
operation, but your suspicion is correct; the WMI query is returning the
list of all services that are NOT "myservice". To do what you want,
Select * from Win32_Service where Name = "myservice"
When you run the query, if you don't get back an object, the service
doesn't exit.
HTH
Later,
Chris
Post by Kevin K
I'm not a developer so please bear with me :)
I created a WMI query (to be used by Group Policy) to check for the
SELECT * FROM Win32_Service where Name <> "myservice"
The goal is to target machines that do NOT have a specific service present.
The above query I suspect is saying "sure -- I have quite a few services
that are not equal to "myservice".
I don't think WMI has a NOT logic operator as it doesn't seem to work?
In any case, how would one write a query that can be used as a WMI Filter
for Group Policy, that will only select computers that do NOT have
"myservice"?
Thanks!
Kevin K
2007-02-06 14:43:03 UTC
Permalink
Post by Chris Richards
You can
create a query that will let you know if a service exists or not; we
have already shown that.
We might have to go with Plan B. Would you or someone be able to direct me
to a smaple query that could be used in a WMI script to only select machines
that do NOT have a specific service?

Thanks!
Chris Richards
2007-02-07 18:47:52 UTC
Permalink
Here's a portion of some VBScript I found that does what you want, I think:

Set colServiceList = objServices.ExecQuery("Select * From
Win32_Service Where Name='" & _
strServiceName & "'")
'Error check
If (Err.Number = 0) And IsObject(colServiceList) Then
If colServiceList.Count > 0 Then
WriteOut " The " & strServiceName & " service exists."

'Set the function return value
WMI_Service_Exists = True
Else
'Set the function return value
WMI_Service_Exists = False

WriteOut " The " & strServiceName & " service does not exist."
End If
End If

This is part of a much larger script that I found at
http://myitforum.com/cs2/blogs/dthomson/attachment/79325.ashx

Later,
Chris
Post by Kevin K
Post by Chris Richards
You can
create a query that will let you know if a service exists or not; we
have already shown that.
We might have to go with Plan B. Would you or someone be able to direct me
to a smaple query that could be used in a WMI script to only select machines
that do NOT have a specific service?
Thanks!
Continue reading on narkive:
Loading...