Link to home
Start Free TrialLog in
Avatar of schnibitz
schnibitz

asked on

Script is duplicating user data

All,
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
            
        Next

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
	else
	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

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

    End If
           Username = ""
           
Dim objComputer

    'Wscript.Echo stuff
    adoRecordset.MoveNext
Loop

' Clean up.
adoRecordset.Close
adoConnection.Close
    WriteStuff.Close
SET WriteStuff = NOTHING
SET myFSO = 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
	Else
		Ping = False
	End If
End Function


Wscript.Echo "Done"

Open in new window


-S
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of schnibitz
schnibitz

ASKER

100% Fixed my problem.  THANK YOU!
No problem. Thanks for the grade.

Rob.