Link to home
Create AccountLog in
Avatar of tw525
tw525Flag for United States of America

asked on

Script for last reboot

I have a great script I found to reboot all the machines within a specific OU.

JeremyW posted this wonderful vbs script on Petri forums to force reboots:

On Error Resume Next

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


'--- change the DN to where you want to start your search ---
objCommand.CommandText = _
"<LDAP://ou=workstations,ou=corp,dc=domain,dc=com>;" & _
"(objectCategory=computer);distinguishedName,name; subtree"

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF

RestartComputer objRecordSet.Fields("Name")
objRecordSet.MoveNext

Wend

objConnection.Close


Sub RestartComputer(strComputer)

set objShell = CreateObject("WScript.Shell") 
objShell.Run "shutdown -s -t 0 -f -m \\" & strComputer

End Sub  

Open in new window


So now I'm looking to alter this script to again look inside a specific OU and report back the last reboot time for every computer within that OU.  Just a pop up with the info would be fine.  Can anyone help me alter this script to get the output I'm looking for?  (Or create a new script if that's easier)

Thanks,
Mike
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, run this with
cscript GetLastBootTime.vbs

and see what you get.  I haven't tested it yet.

Regards,

Rob.
On Error Resume Next

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


'--- change the DN to where you want to start your search ---
objCommand.CommandText = _
"<LDAP://ou=workstations,ou=corp,dc=domain,dc=com>;" & _
"(objectCategory=computer);distinguishedName,name; subtree"

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
	WScript.Echo objRecordSet.Fields("Name") & ": " & GetBootUpTime objRecordSet.Fields("Name")
	objRecordSet.MoveNext
Wend

objConnection.Close

WScript.Echo "Done"

' GetBootUpTime function

Function GetBootUpTime( strComputer )

    Dim objItem
    Dim objWMIService, colItems
    Dim strBootUpTime

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem",,48)
	For Each objItem in colItems
	   strBootUpTime =  Left(objItem.LastBootUpTime, InStr(objItem.LastBootUpTime, ".") -1)
	Next

	GetBootUpTime = strBootUpTime

End Function

Open in new window

Avatar of tw525

ASKER

I am getting an error when running the script that seems to point to the "objRecordSet.Fields" in [& GetBootUpTime objRecordSet.Fields("Name")].

Any thoughts?
Avatar of tw525

ASKER

A coworker sent me this:

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.CommandText = _
    "Select Name, Location from 'LDAP://ou=Computers,ou=HWM,dc=client,dc=local' " _
        & "Where objectClass='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 
SET objRecordSet = objCommand.EXECUTE
objRecordSet.MoveFirst

DO Until objRecordSet.EOF
    Wscript.Echo "Computer Name: " & objRecordSet.Fields("Name").Value
    Wscript.Echo "Location: " & objRecordSet.Fields("Location").Value
    objRecordSet.MoveNext
LOOP

Open in new window


This is getting closer.  It does query the proper OU and lists each machine.  However, if there are 10 computers in the OU I'll get 20 little popups.  A popup for the computer name and then a Popup that just says "Location:" with nothing after it(probably because the location field is blank). Then I ComputerB/Location:<blank>, ComputerC/Location:<blank>, and so on.

So first how can I get it to display last reboot time instead of Location.  I have been seaching for a listing of objRecordSet.Fields but cant seem to find what I'm looking for.

Then is there a way to get it all(all 10 machines) to display in one single popup?

Thanks.
Hi, give this a shot.  Make sure you set the distinguished name to your target ou correctly.

Regards,

Rob.
'On Error Resume Next

Set objConnection = CreateObject("ADODB.Connection")
objConnection.Open "Provider=ADsDSOObject;"

Set objCommand = CreateObject("ADODB.Command")
objCommand.ActiveConnection = objConnection


'--- change the DN to where you want to start your search ---
objCommand.CommandText = _
"<LDAP://ou=IT Computers,ou=it,dc=domain,dc=com>;" & _
"(objectCategory=computer);distinguishedName,name;subtree"

Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
	If Ping(objRecordSet.Fields("Name")) = True Then
		WScript.Echo "Reading last boot up time from " & objRecordSet.Fields("Name")
		WScript.Echo objRecordSet.Fields("Name") & ": " & GetBootUpTime(objRecordSet.Fields("Name"))
	Else
		WScript.Echo objRecordset.Fields("Name") & " is offline"
	End If
	objRecordSet.MoveNext
Wend

objConnection.Close

WScript.Echo "Done"

' GetBootUpTime function

Function GetBootUpTime( strComputer )

    Dim objItem
    Dim objWMIService, colItems
    Dim strBootUpTime

	Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
	Set colItems = objWMIService.ExecQuery("Select LastBootUpTime from Win32_OperatingSystem",,48)
	For Each objItem in colItems
	   strBootUpTime =  Left(objItem.LastBootUpTime, InStr(objItem.LastBootUpTime, ".") -1)
	Next

	GetBootUpTime = strBootUpTime

End Function

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

Avatar of tw525

ASKER

Rob, thanks for posting a reply.  I will be back at this client on Thursday and will give your script a try.
Avatar of tw525

ASKER

Getting close.

Rob, when I run your script I get a popup "Reading last boot up time from ASHLEY" [OK]
Then I get ASHLEY: 20110707172636  (I see the date but not sure what to do with "172636"?)

However on the second machine run I get the popup "Reading last boot up time from ELLEN" [OK]
But then instead of ELLEN: {Value} I get a script error on line 39 character 2.

Thanks,
Mike
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer