Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2972
  • Last Modified:

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

0
alexianit
Asked:
alexianit
  • 4
  • 3
1 Solution
 
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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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
 
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

Featured Post

Lessons on Wi-Fi & Recommendations on KRACK

Simplicity and security can be a difficult  balance for any business to tackle. Join us on December 6th for a look at your company's biggest security gap. We will also address the most recent attack, "KRACK" and provide recommendations on how to secure your Wi-Fi network today!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now