Solved

remote logoff/reboot VBS Script

Posted on 2009-07-16
7
2,878 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Ready to trade in that old firewall?

Whether you need to trade-up to a shiny new Firebox or just ready to upgrade from whatever appliance you're using now, WatchGuard has the right appliance for you! Find your perfect Firebox today with appliance sizing tool!

Question has a verified solution.

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

When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
Popular third-party chat platforms like Slack, Discord, and Telegram are just a few of the many new productivity applications that are being hijacked by cybercriminals to create command-and-control (C&C) communications infrastructures for their malw…
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses
Course of the Month10 days, 16 hours left to enroll

631 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