Solved

remote logoff/reboot VBS Script

Posted on 2009-07-16
7
2,826 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

You may have a outside contractor who comes in once a week or seasonal to do some work in your office but you only want to give him access to the programs and files he needs and keep privet all other documents and programs, can you do this on a loca…
Why pager replacement is still an issue OnPage has what some might call a “hate/hate” relationship with pagers. Not much room for love. As we see it, pagers are an antiquated bit of technology. Pagers are dinosaurs which, like most dinosaurs, sho…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

830 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