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

x
?
Solved

How to create a script to turn off a remote computer automatically?

Posted on 2012-04-07
13
Medium Priority
?
425 Views
Last Modified: 2012-04-22
Dear Expert,

I need a script in order to turn off a specific computer over the network.
I need to schedule the script to run on a specific day to turn off the remote computer and recurring on that day. If the PC get turned on, the script must turn it off.

Thank you and regards,
0
Comment
Question by:ISC-IT-Admin
  • 7
  • 5
13 Comments
 
LVL 2

Expert Comment

by:ven16
ID: 37819483
0
 
LVL 17

Accepted Solution

by:
Anuroopsundd earned 2000 total points
ID: 37819506
On Error Resume Next
Const ForAppending = 8
Const ForReading = 1
Dim strShutdown
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set oTS = oFS.OpenTextFile("C:\computer.txt", ForReading)
Set objTextFile = objFSO.OpenTextFile ("c:\Result.txt", ForAppending, True)
Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
strPingStatus = PingStatus(strCompname)
If strPingStatus = "Success" Then
strShutdown = "shutdown -s -t 120 -f -m \\" & strCompname

set objShell = CreateObject("WScript.Shell")

objShell.run strShutdown


Else

    objTextFile.writeline "Failure pinging Machine name -" & strCompname & ": " & strPingStatus
End If
Loop
Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "


oTS.Close
objTextFile.close

Function PingStatus(strCompname)

    On Error Resume Next
   
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strCompname & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strCompname & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819528
1. Please save above code in .vbs file
2. Above script first pings the machine and if machine is pingable then will shutdown the machine. machine will be shutdown in 2 minutes and user will get a prompt.

3.  create a file c:\computer.txt and put your machine names or ip address
     1 machine name per line.
4.  create c:\result.txt file. -- in this file it will put the output incase the machine is not pingable.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 1

Author Comment

by:ISC-IT-Admin
ID: 37819556
Thank you Anuroopsundd for the reply,

Please note that I don't want the user to be prompted, I need to force shutting down the machine.

Regards,
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819570
change line below so that immediately machine is shutdown.

strShutdown = "shutdown -s -t 120 -f -m \\" & strCompname

 to
strShutdown = "shutdown -s  -f -m \\" & strCompname
0
 
LVL 1

Author Comment

by:ISC-IT-Admin
ID: 37819575
How can I set above script to run only on Friday for example?
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819583
use task schedular to run the script every week on friday at specified time.

http://www.windowsnetworking.com/articles_tutorials/Working-Windows-Server-2008-Task-Scheduler-Part1.html
0
 
LVL 1

Author Comment

by:ISC-IT-Admin
ID: 37819654
Another question, if the user turn on again his PC, what will happen?

I need the script to run all day long on Friday, if the user turn on his PC, then the script should run again.
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819661
you can have multiple schedule task that run on Friday at different time..
may be first run at 7:00 PM again it runs at 10:00 PM and then on Saturday morning 9:00 AM.
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819709
Please close this if you think this has been done. .....
0
 
LVL 1

Author Comment

by:ISC-IT-Admin
ID: 37819722
Thank you, I have another question:

I noticed that when the script runs, it shows below text box message:

"The script runs successfully", then I need to click ok, I need it to be run transparent without any notification.

Regards,
0
 
LVL 17

Expert Comment

by:Anuroopsundd
ID: 37819725
Delete the below line from the script
Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "
0
 
LVL 1

Author Comment

by:ISC-IT-Admin
ID: 37877560
Dear Anuroopsundd,

I would like to thank you again for the script:

Please I need to add a quick message box like an error saying: An Error has occured and your system will shutdown... Then the system will force shutdown.

How can I do this using your script below?
Thank you.

On Error Resume Next
Const ForAppending = 8
Const ForReading = 1
Dim strShutdown
Set WSHShell = WScript.CreateObject("WScript.Shell")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set oTS = oFS.OpenTextFile("C:\computer.txt", ForReading)
Set objTextFile = objFSO.OpenTextFile ("c:\Result.txt", ForAppending, True)
Do Until oTS.AtEndOfStream
strCompname = oTS.ReadLine
strPingStatus = PingStatus(strCompname)
If strPingStatus = "Success" Then
strShutdown = "shutdown -s -t 0 -f -m \\" & strCompname

set objShell = CreateObject("WScript.Shell")

objShell.run strShutdown


Else

    objTextFile.writeline "Failure pinging Machine name -" & strCompname & ": " & strPingStatus
End If
Loop


oTS.Close
objTextFile.close

Function PingStatus(strCompname)

    On Error Resume Next
   
    Set objWMIService = GetObject("winmgmts:" _
      & "{impersonationLevel=impersonate}!\\" & strCompname & "\root\cimv2")
    Set colPings = objWMIService.ExecQuery _
      ("SELECT * FROM Win32_PingStatus WHERE Address = '" & strCompname & "'")
    For Each objPing in colPings
        Select Case objPing.StatusCode
            Case 0 PingStatus = "Success"
            Case 11001 PingStatus = "Status code 11001 - Buffer Too Small"
            Case 11002 PingStatus = "Status code 11002 - Destination Net Unreachable"
            Case 11003 PingStatus = "Status code 11003 - Destination Host Unreachable"
            Case 11004 PingStatus = _
              "Status code 11004 - Destination Protocol Unreachable"
            Case 11005 PingStatus = "Status code 11005 - Destination Port Unreachable"
            Case 11006 PingStatus = "Status code 11006 - No Resources"
            Case 11007 PingStatus = "Status code 11007 - Bad Option"
            Case 11008 PingStatus = "Status code 11008 - Hardware Error"
            Case 11009 PingStatus = "Status code 11009 - Packet Too Big"
            Case 11010 PingStatus = "Status code 11010 - Request Timed Out"
            Case 11011 PingStatus = "Status code 11011 - Bad Request"
            Case 11012 PingStatus = "Status code 11012 - Bad Route"
            Case 11013 PingStatus = "Status code 11013 - TimeToLive Expired Transit"
            Case 11014 PingStatus = _
              "Status code 11014 - TimeToLive Expired Reassembly"
            Case 11015 PingStatus = "Status code 11015 - Parameter Problem"
            Case 11016 PingStatus = "Status code 11016 - Source Quench"
            Case 11017 PingStatus = "Status code 11017 - Option Too Big"
            Case 11018 PingStatus = "Status code 11018 - Bad Destination"
            Case 11032 PingStatus = "Status code 11032 - Negotiating IPSEC"
            Case 11050 PingStatus = "Status code 11050 - General Failure"
            Case Else PingStatus = "Status code " & objPing.StatusCode & _
               " - Unable to determine cause of failure."
        End Select
    Next

End Function
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Hallo! I guess almost every Windows Administrator must have got stumped with this question "Where does WINDOWS store a users cached credentials? Every user who had once logged onto a Server/Desktop while it was connected to the domain could sti…
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…

972 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