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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Computername only works when hardcoded not as "." variable?

Status
Not open for further replies.
Feb 11, 2005
153
0
0
US
Okay thew below code works just fine but ONLY if I got to strComputer and I type in a machine name in the field E.G. my computer is named COMPUTERNAME. I type in strComputer = "COMPUTERNAME" and it runs. If I type in strComputer = "computername" or "compUternAme" or "." I get no processing once the script hits the line - For Each objAccount in colAccounts. I don't even get an error. (The reson I know thats where it stops is I put in a wscript.echo line in the script until it no longer processed the line.) So it gets as far as setting the objWMIService and it just stops processing.

This is no good though to me because the strComputer field has to be a variable for the application to work. Why can't I get strComputer = "." to process in this script?

Code:
Sub DestroyObjects()
    If IsObject(objAdminAccount) Then Set objAdminAccount = Nothing
    If IsObject(objDevice) Then Set objDevice = Nothing
End Sub

    strComputer = "."
    strPassword = "mypass"
	strUser = "user1"
	strUser1 = "user2"
	strUser2 = "user3"
	strUser3 = "user4"
	strUser4 = "user5"
	strUser5 = "user6"
	strUser6 = "user7"
	strUser7 = "user8"
	strUser8 = "user9"
	strUser9 = "user10"
	strUser10 = "Administrator"
Set objDevice = GetObject("WinNT://" & strComputer )
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colAccounts = objWMIService.ExecQuery _
    ("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")
For Each objAccount in colAccounts
    If Left (objAccount.SID, 6) = "S-1-5-" and Right(objAccount.SID, 4) = "-500" Then
      Set objAdminAccount = GetObject("WinNT://" & strComputer & "/" & objAccount.Name)
        On error resume next
        objDevice.Delete "user", strUser
		objDevice.Delete "user", strUser1
		objDevice.Delete "user", strUser2
		objDevice.Delete "user", strUser3
		objDevice.Delete "user", strUser4
		objDevice.Delete "user", strUser5
		objDevice.Delete "user", strUser6
		objDevice.Delete "user", strUser7
		objDevice.Delete "user", strUser8
		objDevice.Delete "user", strUser9
		objDevice.Delete "user", strUser10
                On Error GoTo 0
		        objAdminAccount.SetPassword(strPassword)
                objAdminAccount.FullName = ""
		        objAdminAccount.Description = "Built-in account for administering the computer/domain"
		        objAdminAccount.SetInfo
                objDevice.MoveHere objAdminAccount.ADsPath, "" & strUser & ""
End if
Next

DestroyObjects()
 
what that could be a problem:
("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
No nothing is wrong with that field that I can see -

I have it narrowed down though if I do a -

Set objNet = CreateObject("WScript.NetWork")
strComputer = objNet.ComputerName

This returns the good information and allows for processing. IMHO this is a kludge to get it to work however.

If I leave - strComputer = "."

and do a wscript.echo I get . as a result. Why isn't it pulling down the computer name as the local variable?
 
I would have to agree with PHV that Domain in that WMI query will not take "." and may be the source of the problem.

Look at the class using the WMI Explorer or wbemtest to see that property.

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Set objDevice = GetObject("WinNT://" & strComputer )
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

i would say you can use "." for the objWMIServive but you cant use "." for the WinNT provider.

so either you ahve to use another variable other than strCOmputer for one or the other or you need to change strComputer at some point
 
I take it you are tring to run through user accounts that are on the local machine. Where you have:

Code:
Set colAccounts = objWMIService.ExecQuery _
    ("Select * From Win32_UserAccount Where Domain = '" & strComputer & "'")

This is interpreting the ' " & strComputer &" ' literaly as the . instead of querying the WMI of the local machine. Try it like this:

Code:
Set colAccounts = objWMIService.ExecQuery _
    ("Select * From Win32_UserAccount Where Domain = strComputer")

And I actually would have expected the WScript.Echo to be blank rather than .
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top