Solved

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

Posted on 2012-04-07
13
416 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 500 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
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Preface Having the need * to contact many different companies with different infrastructures * do remote maintenance in their network required us to implement a more flexible routing solution. As RAS, PPTP, L2TP and VPN Client connections are no…
The password reset disk is often mentioned as the best solution to deal with the lost Windows password problem. In Windows 2008, 7, Vista and XP, a password reset disk can be easily created. But besides Windows 7/Vista/XP, Windows Server 2008 and ot…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

758 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

22 Experts available now in Live!

Get 1:1 Help Now