Link to home
Start Free TrialLog in
Avatar of mikemost
mikemostFlag for United States of America

asked on

Script to query time (net time) on all servers via accessing the AD schema

Greetings All,

I've searched around EE & Google but am yet to find a good sample script to get me started. What I'm looking for is either a Winbatch sample script, or windows command file that can query the active directory schema, or a collection of OU's, and go out to the servers within the OU's and retrieve the current time (run a net time command) and output the results into a file, preferably a .csv but an .xls or even .txt file would suffice just fine. I believe this should be possible through WMI, if that proves to be to big of a hurdle, I could attempt to use a script which could simply rely on pre-determined list. The benefit of going into AD would be avoiding calculating every single server in our environment as my company is a small enterprise with god knows how many virtual servers! It would also be great if such a script could specify in the output servers that do not respond into a separate category in the output file due to certian VM's being powered off at certain times. Any and all assistance is appreciated and this is certainly a 500 point question.

Best Regards,
Mike Mostwill
Avatar of ahoffmann
ahoffmann
Flag of Germany image

# what's wrong with
net time /QUERYSNTP
Avatar of mikemost

ASKER

I apologize as I was a bit vague with how I worded the question. I'm not looking for the time server, but rather the script to output the current time (ie 16:43:29) into the log file. Thank you for your input.

 - Mike
echo ""|time > logfile
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
Rob, clear, concise, perfect... thanks so much!

My only question is, does this script only query Server O/S's? We have a few XP machines that are utilized as automation servers. Practically, the script makes perfect sense, we just have a bit of an odd setup in that area. Do you think there would be any way for the script to, in addition to querying the LDAP, to query a text file which would contain the three or four XP machines and append it to the same file? I'm sure we could query workstation O/S's as well, but we obviously wouldn't want to pull the time down for every workstation in the enterprise.

Thanks again,
Mike
Hi there.  You can now fill in arrOtherServers to query any extra computers that are not server OS's.

Regards,

Rob.
If LCase(Right(Wscript.FullName, 11)) = "wscript.exe" Then
    strPath = Wscript.ScriptFullName
    strCommand = "%comspec% /c cscript  """ & strPath & """"
    Set objShell = CreateObject("Wscript.Shell")
    objShell.Run(strCommand), 1, True
    Wscript.Quit
End If
 
strLogFile = "Time_On_Servers.csv"
arrOtherServers = Array("XPPC1","XPPC2")
For Each strComputer In arrOtherServers
	strQuery = strQuery & " OR Name='" & strComputer & "'"
Next
 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
If objFSO.FileExists(strLogFile) = True Then objFSO.DeleteFile strLogFile, True
Const intForReading = 1
Set objShell = CreateObject("WScript.Shell")
 
Const ADS_SCOPE_SUBTREE = 2
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection
 
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
 
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")
 
objCommand.CommandText = "SELECT name FROM 'LDAP://" & strDNSDomain & "' WHERE operatingSystem='*server*'" & strQuery
Set objRecordSet = objCommand.Execute
 
Set objDictionary = CreateObject("Scripting.Dictionary")
 
While Not objRecordSet.EOF
	strComputer = objRecordSet.Fields("name").Value
	If Ping(strComputer) = True Then
		WScript.Echo "Querying " & strComputer
		strCommand = "cmd /c net time \\" & strComputer & " >> """ & strLogFile & """"
		objShell.Run strCommand, 0, True
	Else
		WScript.Echo strComputer & " offline"
		strCommand = "cmd /c echo " & strComputer & ",Offline >> """ & strLogFile & """"
		objShell.Run strCommand, 0, True
	End If
	objRecordSet.MoveNext
Wend
objRecordSet.Close
Set objRecordSet = Nothing
 
' Clean up output file
Set objLogfile = objFSO.OpenTextFile(strLogFile, intForReading, False)
strContents = Replace(objLogfile.ReadAll, VbCrLf & "The command completed successfully." & VbCrLf & VbCrLf, "")
strContents = Replace(Replace(strContents, "Current time at \\", ""), " is ", ",")
objLogfile.Close
Set objLogfile = objFSO.CreateTextFile(strLogFile, True)
objLogfile.Write strContents
objLogFile.Close
Set objLogFile = Nothing
MsgBox "Finished. Please see " & strLogFile
 
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

Open in new window

Talk about service, very impressive... thanks again Rob!!
No problem. Thanks for the grade.

Regards,

Rob.