Solved

remote logoff/reboot VBS Script

Posted on 2009-07-16
7
2,795 Views
Last Modified: 2013-11-26
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

0
Comment
Question by:alexianit
  • 4
  • 3
7 Comments
 
LVL 6

Expert Comment

by:ou_dober
ID: 24869818
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
 

Author Comment

by:alexianit
ID: 24870478
It would be good to combine somehow in the case of checking if the computer is available.
0
 

Author Comment

by:alexianit
ID: 24870484
Thank you for the comparison, and thanks in advance for the additional code.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 6

Expert Comment

by:ou_dober
ID: 24872560
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
 
LVL 6

Accepted Solution

by:
ou_dober earned 500 total points
ID: 24873069
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
 
LVL 6

Expert Comment

by:ou_dober
ID: 24936069
alexianit,

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

ou_dober
0
 

Author Closing Comment

by:alexianit
ID: 31604228
Sorry I should have closed this a while ago.  Thank You!
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

How can you create a game plan that lets you focus on special projects instead of running from cubicle to cubicle every day and feeling like you’ve accomplished nothing? Try these strategies for prioritizing your tasks, offloading what you can, and …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now