ISC-IT-Admin
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,
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,
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.
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,
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
strShutdown = "shutdown -s -t 120 -f -m \\" & strCompname
to
strShutdown = "shutdown -s -f -m \\" & strCompname
ASKER
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
http://www.windowsnetworking.com/articles_tutorials/Working-Windows-Server-2008-Task-Scheduler-Part1.html
ASKER
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.
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.
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. .....
ASKER
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,
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 "
Wscript.Echo "Script has run Succesfully. Please see C:\Result.txt file for results "
ASKER
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("WScr ipt.Shell" )
Set oFS = CreateObject("Scripting.Fi leSystemOb ject")
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objShell = CreateObject("WScript.Shel l")
Set oTS = oFS.OpenTextFile("C:\compu ter.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.Shel l")
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=imper sonate}!\\ " & 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
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("WScr
Set oFS = CreateObject("Scripting.Fi
Set objFSO = CreateObject("Scripting.Fi
Set objShell = CreateObject("WScript.Shel
Set oTS = oFS.OpenTextFile("C:\compu
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.Shel
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=imper
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
the below one is same you want may be it will help you
http://www.tekforums.net/guides-projects/batch-script-to-turn-on-backup-to-and-turn-off-second-pc/