Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WMI Syntax trouble 1

Status
Not open for further replies.

Jerz

MIS
Sep 10, 2004
102
US
I need to get services "where startname = 'domain\user'"

Here's what I'm working with:

Code:
  OldUser = "domain\user"
  Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & Computer & "\root\cimv2")


  If Err.Number <> 0 Then
    Wscript.Echo Computer & " is not reachable"


  Else

' 1   Set colServices = objWMIService.ExecQuery ("Select * From Win32_Service Where StartName = '" & OldUser & "'")

' 2   Set colServices = objWMIService.ExecQuery ("Select * From Win32_Service Where StartName = 'domain\user'")

' 3   Set colServices = objWMIService.ExecQuery ("Select * From Win32_Service")

    If Err.Number <> 0 Then
      Wscript.Echo err.number & vbcrlf & err.description
    End if
    For Each objService in colServices
      wscript.echo objService.name & vbcrlf & objService.Startname 
'      errReturn = objService.Change( , , , , , , newuser , pw)
      objFileRpt.WriteLine "On Server " & Computer &", Changing User for service " & objService.name & " from " & OldUser & " to " & Newuser
      serv = serv + 1
    Next
  End If

I need to do number 1, query based on a variable.

I thought I would troubleshoot by doing 2, but it doesn't work either.

Only #3 gives me some results, namely the names of the services & the accounts they start under. The 'startname' of the service I'm looking to change is a perfect, case sensitive match with the string 'domain\user', but the where won't work for me.....

Also, the error trap shows nothing on 1, 2, or 3.

But I do get Error: 0x80041017 Source: (null) on the 'For Each objService' for both 1 & 2 (I think because the 'where' clause finds no matches?)

If anyone sees any obvious (or not so obvious) errors, please point me in the right direction.

Thanks, And have a great year!

David J.
 
When looking for your user try using two "\". i.e.
OldUser = "domain\\user"

dm4ever
--------------------------------------------------------------------------------
My approach to things: K.I.S.S - Keep It Simple Stupid
 
Sure enough, that's the ticket. Now that I can find the right service, I can stop it, change the credentials, and kick it right back off.

Thanks so much DM, here's a star!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top