We help IT Professionals succeed at work.
Get Started

Script is duplicating user data

schnibitz asked
Last Modified: 2012-07-12
Can you help me figure this out?  The code below (well the big code) enumerates all machines in AD.  It then pings them to see if they're alive.  If they are, it then checkes to see if anyone is logged in.  If they are, it writes the machine name, and the user name to a text file.  Problem is, some usernames are getting repeated, and I know they're not logged in multiple times.  I did some troubleshooting, and one of the machines that has a

"the remote server machine does not exist or is unavailable

Error when I run the following portion of the code:

        strComputer = "comptuername"
        Set objWMIService = GetObject("winmgmts:" _
            & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

        Set colComputer = objWMIService.ExecQuery _
            ("Select * from Win32_ComputerSystem")
        For Each objComputer in colComputer
            Username = objComputer.UserName
            'WriteStuff.WriteLine(Username & " " & stuff)
            Wscript.Echo "Logged-on user: " & objComputer.UserName

Open in new window

On line 2

Here's my code, can you help me?

on error resume next

'Open up the path to save the information into a text file
Dim myFSO, WriteStuff, strPingUp
    dim stuff

'Write information to Text File

function age(ComputerName)
	Set Computer = GetObject("WinNT://" & domainname & "/" & ComputerName & "$,user")
	If Computer.Get("PasswordAge") < 5184000 Then '60 Days
        age = true
	age = false
	End If
End Function

Set objShell = CreateObject("WScript.Shell")
Set myFSO = CreateObject("Scripting.FileSystemObject")
Set WriteStuff = myFSO.OpenTextFile("Workstations.txt", 8, True)

'Option Explicit

Dim objRootDSE, strDNSDomain, adoConnection, adoCommand, strQuery
Dim adoRecordset, strComputerDN, strBase, strFilter, strAttributes

' Determine DNS domain name from RootDSE object.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

' Use ADO to search Active Directory for all computers.
Set adoCommand = CreateObject("ADODB.Command")
Set adoConnection = CreateObject("ADODB.Connection")
adoConnection.Provider = "ADsDSOObject"
adoConnection.Open "Active Directory Provider"
adoCommand.ActiveConnection = adoConnection

' Search entire domain.
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on computer objects with server operating system.
strFilter = "(&(objectCategory=computer)(!operatingSystem=*server*))"

' Comma delimited list of attribute values to retrieve.
strAttributes = "sAMAccountName"

' Construct the LDAP syntax query.
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"

adoCommand.CommandText = strQuery
adoCommand.Properties("Page Size") = 100
adoCommand.Properties("Timeout") = 30
adoCommand.Properties("Cache Results") = False

Set adoRecordset = adoCommand.Execute

' Enumerate computer objects with server operating systems.
Do Until adoRecordset.EOF

    stuff = adoRecordset.Fields("sAMAccountName").Value
    stuff = Replace(stuff,"$","")
    If Ping(stuff) = True Then
        strComputer = stuff
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
	Username = objComputer.UserName
            WriteStuff.WriteLine(Username & " " & stuff)
            'Wscript.Echo "Logged-on user: " & objComputer.UserName

            Username = ""
            WriteStuff.WriteLine(stuff & " inactive")

    End If
           Username = ""
Dim objComputer

    'Wscript.Echo stuff

' Clean up.
SET WriteStuff = NOTHING

Function Ping(strComputer)
	Dim objShell, boolCode
	Set objShell = CreateObject("WScript.Shell")
	boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
	If boolCode = 0 Then
		Ping = True
		Ping = False
	End If
End Function

Wscript.Echo "Done"

Open in new window

Watch Question
Most Valuable Expert 2012
Top Expert 2014
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE