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
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"
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”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.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Most Valuable Expert award recognizes technology experts who passionately share their knowledge with the community, demonstrate the core values of this platform, and go the extra mile in all aspects of their contributions. This award is based off of nominations by EE users and experts. Multiple MVEs may be awarded each year.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.