troubleshooting Question

Script is duplicating user data

Avatar of schnibitz
schnibitz asked on
VB Script
3 Comments1 Solution261 ViewsLast Modified:
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

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"

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros