Link to home
Start Free TrialLog in
Avatar of ISC-IT-Admin
ISC-IT-AdminFlag for United Arab Emirates

asked on

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

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,
Avatar of ven16
ven16

ASKER CERTIFIED SOLUTION
Avatar of Anuroopsundd
Anuroopsundd
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of ISC-IT-Admin

ASKER

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,
change line below so that immediately machine is shutdown.

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

 to
strShutdown = "shutdown -s  -f -m \\" & strCompname
How can I set above script to run only on Friday for example?
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
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.
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.
Please close this if you think this has been done. .....
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,
Delete the below line from the script
Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "
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