remote logoff/reboot VBS Script

I've quite a basic understanding of VBS and I have 2 scripts that 'should' perform the same basic function.  I note that there are differences in the scripting and some of these simply involve the name of the dialogue boxes.

I'd like to know which of these is the better script to use, or what the difference in functionality is between the two, like a comparison.

I'd also like to add the functionality for the admin running it, to see an input box of some sort that lists the various functions that the script COULD do in its message body.  Such as ...

"Press 1 to log off the user, press 2 to Force user logoff, press 3 to reboot ..." etc.
 
1st script
'On Error Resume Next
strComputer=InputBox ("Name of Computer to Unlock"& vbCrlf & vbCrlf & _
	"Such as ABC-123456","The Kick'Em OFF Doodad!")
	If strComputer="" Then
	wscript.quit
	end if
doit = MsgBox ("Press OK to Forcibly log off the current user on "& strComputer, 65, "Really Kick'Em?")
if doit = 1 then
Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
ObjOperatingSystem.Win32Shutdown(0+4)
Next
end if
 
'Note:  You may substitute the line that reads objOperatingSystem.Win32Shutdown(0) with any of the Win32Shutdown paramaters isted below:
 
'Log Off 
'objOperatingSystem.Win32Shutdown(0)
 
'Forced Log Off 
'objOperatingSystem.Win32Shutdown(0 + 4) 
 
'Shutdown 
'objOperatingSystem.Win32Shutdown(1) 
 
'Forced Shutdown 
'objOperatingSystem.Win32Shutdown(1 + 4) 
 
'Reboot 
'objOperatingSystem.Win32Shutdown(2) 
 
'Forced Reboot 
'objOperatingSystem.Win32Shutdown(2 + 4) 
 
'Power Off 
'objOperatingSystem.Win32Shutdown(8) 
 
'Forced Power Off
'objOperatingSystem.Win32Shutdown(8 + 4) 
 
************************************************************************
2nd script
************************************************************************
'On Error Resume Next 
strComputer = InputBox("Enter Machine Name") 
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}")_
    .ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'") 
For Each objItem in colItems 
If objItem.StatusCode = 0 Then 
Wscript.Echo "The Command On " & UCase(strComputer) & " Has Been Accepted"
Else 
Wscript.Echo "The Machine " & UCase(strComputer) & " Was Not Found"
Wscript.quit
End if 
Next 
 
Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem in colOperatingSystems
objOperatingSystem.Win32Shutdown(0 + 4)
Next
 
 
'Note:  You may substitute the line that reads objOperatingSystem.Win32Shutdown(2 + 4) with any of the Win32Shutdown paramaters isted below:
 
'Log Off 
'objOperatingSystem.Win32Shutdown(0)
 
'Forced Log Off 
'objOperatingSystem.Win32Shutdown(0 + 4) 
 
'Shutdown 
'objOperatingSystem.Win32Shutdown(1) 
 
'Forced Shutdown 
'objOperatingSystem.Win32Shutdown(1 + 4) 
 
'Reboot 
'objOperatingSystem.Win32Shutdown(2) 
 
'Forced Reboot 
'objOperatingSystem.Win32Shutdown(2 + 4) 
 
'Power Off 
'objOperatingSystem.Win32Shutdown(8) 
 
'Forced Power Off
'objOperatingSystem.Win32Shutdown(8 + 4)

Open in new window

alexianitAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ou_doberCommented:
The key differences between these two scripts are as follows:

  • Script 1 is asking for the computer name and than asking again for the user/admin to confirm logging the user off of the computer.
  • Script 1 also has it to where if the user/admin leaves to computer field blank, the script ends automatically.
  • Script 2 is also asking for the computer name and specifically checks if the computer is available via ping before proceeding with the log off off the user from the remote computer.
I will try to boiler plate with a if/else nest and input box when I get back from this meeting. That should not be difficult to do.

ou_dober
0
alexianitAuthor Commented:
It would be good to combine somehow in the case of checking if the computer is available.
0
alexianitAuthor Commented:
Thank you for the comparison, and thanks in advance for the additional code.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

ou_doberCommented:
alexianit,

Here is the first one for you to test.  I am trying to find a force loop to prevent other choices i.ie 5 and enter as a option.

' ****************** Remote Reboot Shutdown Script *******************************
' Note: You may substitute the line that reads objOperatingSystem.Win32Shutdown(0)
' with any of the Win32Shutdown paramaters isted below:
' Log Off         objOperatingSystem.Win32Shutdown(0)
' Forced Log Off  objOperatingSystem.Win32Shutdown(0+4) -- (Currently Choice 1)
' Shutdown        objOperatingSystem.Win32Shutdown(1)
' Forced Shutdown objOperatingSystem.Win32Shutdown(1+4) -- (Currently Choice 2)
' Reboot          objOperatingSystem.Win32Shutdown(2)
' Forced Reboot   objOperatingSystem.Win32Shutdown(2+4) -- (Currently Choice 3)
' Power Off       objOperatingSystem.Win32Shutdown(8)
' Forced Power OffobjOperatingSystem.Win32Shutdown(8+4) -- (Currently Choice 4)
' ********************************************************************************
'
' Take rem out of the line below once testing is complete to turn off error messaging.
' On Error Resume Next
 
strComputer = InputBox("Please Enter Machine Name","Remote Shutdown / Reboot")
 
If strComputer="" Then
   wscript.quit
end if
 
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}")_
    .ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'")
 
For Each objItem in colItems
If objItem.StatusCode = 0 Then
    Wscript.Echo "The machine " & UCase(strComputer) & " is online and is ready for instructions."
Else
    Wscript.Echo "The machine " & UCase(strComputer) & " is offline or was not reachable."
    Wscript.quit
End if
Next
 
strShutdown = InputBox("What do you wish to do?"& vbcr &"1 - Force User Log Off"& vbcr &"2 - Shutdown the machine"& vbcr &"3 - Reboot the machine"& vbcr &"2 - Power down the machine")
if strShutdown = "" then
    Wscript.Echo "The machine request for" & UCase(strComputer) & " has been cancelled due to no selection."
    Wscript.quit
End if
 
if strShutdown = 1 then
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(0+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is forcing the user to log off."
end if
 
if strShutdown = 2 then
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(1+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is shutting down."
end if
 
if strShutdown = 3 then
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(2+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is rebooting."
end if
 
if strShutdown = 4 then
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(8+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is forcing a power down."
end if

Open in new window

0
ou_doberCommented:
Here is one even better.  I got the locked in loop built with the option to cancel.  I also fixed a menu type.  Hope you like it.

ou_dober

' ****************** Remote Reboot Shutdown Script *******************************
' Note: You may substitute the line that reads objOperatingSystem.Win32Shutdown(0)
' with any of the Win32Shutdown paramaters isted below:
' Log Off         objOperatingSystem.Win32Shutdown(0)
' Forced Log Off  objOperatingSystem.Win32Shutdown(0+4) -- (Currently Choice 1)
' Shutdown        objOperatingSystem.Win32Shutdown(1)
' Forced Shutdown objOperatingSystem.Win32Shutdown(1+4) -- (Currently Choice 2)
' Reboot          objOperatingSystem.Win32Shutdown(2)
' Forced Reboot   objOperatingSystem.Win32Shutdown(2+4) -- (Currently Choice 3)
' Power Off       objOperatingSystem.Win32Shutdown(8)
' Forced Power OffobjOperatingSystem.Win32Shutdown(8+4) -- (Currently Choice 4)
' ********************************************************************************
'
' Take rem out of the line below once testing is complete to turn off error messaging.
' On Error Resume Next
Dim Lock
 
strComputer = InputBox("Please Enter Machine Name","Remote Shutdown / Reboot")
 
If strComputer="" Then
   Wscript.Echo "Script cancelled."
   wscript.quit
end if
 
Set colItems = GetObject("winmgmts:{impersonationLevel=impersonate}")_
    .ExecQuery("SELECT * FROM Win32_PingStatus where address = '" & strComputer & "'")
 
For Each objItem in colItems
If objItem.StatusCode = 0 Then
    Wscript.Echo "The machine " & UCase(strComputer) & " is online and is ready for instructions."
Else
    Wscript.Echo "The machine " & UCase(strComputer) & " is offline or was not reachable."
    Wscript.quit
End if
Next
 
Lock = 0
 
Do
strShutdown = InputBox("What do you wish to do?"& vbcr &"1 - Force User Log Off"& vbcr &"2 - Shutdown the machine"& vbcr &"3 - Reboot the machine"& vbcr &"4 - Power down the machine")
if strShutdown = "" then
    Wscript.Echo "The machine request for" & UCase(strComputer) & " has been cancelled due to no selection."
    Wscript.quit
    Lock = 1
End if
Select Case strShutdown
Case "1"
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(0+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is forcing the user to log off."
   Lock = 1
Case "2"
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(1+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is shutting down."
   Lock = 1
Case "3"
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(2+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is rebooting."
   Lock = 1
Case "4"
   Set objWMI = GetObject("winmgmts:{(Shutdown)}\\" + strComputer + "\root\cimv2")
   Set colOperatingSystems = objWMI.ExecQuery("Select * from Win32_OperatingSystem")
   For Each objOperatingSystem in colOperatingSystems
       ObjOperatingSystem.Win32Shutdown(8+4)
   Next
   Wscript.Echo "The machine " & UCase(strComputer) & " is forcing a power down."
   Lock = 1
Case Else
   Wscript.Echo "You have made an incorrect selection."
   Lock = 0
end Select
Loop Until Lock=1

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ou_doberCommented:
alexianit,

Did this script work for what you were trying to do?

ou_dober
0
alexianitAuthor Commented:
Sorry I should have closed this a while ago.  Thank You!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.