protocol Posted January 21, 2016 Posted January 21, 2016 (edited) I'm having trouble getting a batch script I've created working the way I want it to. I want it to ping every computer in a chosen OU and if there's a response then to give a warning prior to shutting the computer down. There is one computer that I need it not to shutdown though which is COMPUTER1 in my script below. Ideally I don't want it shutting down mine either but I can live with shutting my down till I can work out how to add another if statement that ignores mine too. I have set a scheduled task to run this at 8:55pm every evening. At the moment none of the computers I want to shutdown are doing so. @ECHO OFF DSQUERY COMPUTER -o rdn ou="Computers",dc=xxx,dc=xxxx,dc=xx,dc=xx -limit 0 >%tmp%\Computers.txt FOR /F "delims=," %%G IN (%tmp%\Computers.txt) DO ( PING -n 1 %%~G | FIND "TTL=" >nul IF ERRORLEVEL 1 ( REM ECHO %%~G : OFFLINE. ) ELSE ( ECHO %%~G : ONLINE. REM SHUTDOWN /M \\%%~G /F /S /T 300 REM ECHO SHUTTING DOWN %%~G IF %%~G==COMPUTER1 ( ECHO COMPUTER1 ) ) ) I don't have any paid for software to shutdown the computers like I did in my old job so thought a batch file would be my best solution. Any help will be greatly appreciated. Edited January 21, 2016 by protocol
sted Posted January 21, 2016 Posted January 21, 2016 is there any point in seeing if its online worst case it will try to send a shutdown command and fail adding a few seconds to the run time? as to missing out computer 1 could you not do an if statement something like if %%g="computer1" goto next or if if %%g="computer1" %%g = "zzzzzzzzzzzzzzzzzzzzzz" so it will just fail? 1
Jawloms Posted January 21, 2016 Posted January 21, 2016 You have "REM"d out a few lines, that'll stop it working! Also, following your logic through if the computer is on then it will be told to shut down (once the REMs are removed) which means that COMPUTER1 will be shut down if it's on. The other IF statement you have regarding "COMPUTER1" just means it will also ECHO COMPUTER1, but due to the first IF, if it is on it will receive the shutdown command. You'd need the IF %%~G==COMPUTER1 statement first, and then finish it with a "GOTO SKIPPED", and then have a ":SKIPPED" just before your FOR loop finishes. The other thing is, why check if it responds to a PING in the first place? You could just send the shutdown command to everything without all this complexity. 1
protocol Posted January 21, 2016 Author Posted January 21, 2016 is there any point in seeing if its online worst case it will try to send a shutdown command and fail adding a few seconds to the run time? as to missing out computer 1 could you not do an if statement something like if %%g="computer1" goto next or if if %%g="computer1" %%g = "zzzzzzzzzzzzzzzzzzzzzz" so it will just fail? You have "REM"d out a few lines, that'll stop it working! Also, following your logic through if the computer is on then it will be told to shut down (once the REMs are removed) which means that COMPUTER1 will be shut down if it's on. The other IF statement you have regarding "COMPUTER1" just means it will also ECHO COMPUTER1, but due to the first IF, if it is on it will receive the shutdown command. You'd need the IF %%~G==COMPUTER1 statement first, and then finish it with a "GOTO SKIPPED", and then have a ":SKIPPED" just before your FOR loop finishes. The other thing is, why check if it responds to a PING in the first place? You could just send the shutdown command to everything without all this complexity. Thanks both will give it a go. REM was due to me testing it and forgetting to remove it before pasting code due to lack of caffeine this morning. Used AB Tutor in my last place to handle this so batch files are a learning process
HPlum78 Posted January 21, 2016 Posted January 21, 2016 look back through my posts, use PowerShell. 1
markwilfan Posted January 23, 2016 Posted January 23, 2016 We use a powershell script deployed on a schedule via sccm to a collection. First looks at the time, if after x time it runs, this is to stop devices running it if they were off during the scheduled deployment as sccm runs missed schedules too. Will dig it out when I have a chance. May help a bit 1
Sephiroth Posted January 23, 2016 Posted January 23, 2016 I use a scheduled task from group policy on the OU. You can then use Item Level Targeting to exclude anything that you don't want to shutdown. I was using the same method that you're using but found this works much better and more efficiently. 1
ITGURU Posted January 23, 2016 Posted January 23, 2016 Why not use PSEXEC? I run a daily script file using task scheduler which shuts down the PCS. If one of offline or unreachable it will skip it after 5 seconds and go on to the next one. We don't have many PC name changes so hardly have to update the list only when new additions to add to the computer list. 1
caffrey Posted January 23, 2016 Posted January 23, 2016 I use this old chestnut, been in use for ages, shuts down the majority of the PC's To go with this you just need a text file, in this case snrictsuite.txt with a list of PCs in it, then just create a scheduled task for when you want them to shut down shutdown.vbs On Error Resume Next Dim oFSO, oTS, sClient, oWindows, oLocator, oConnection, oSys Dim sUser, sPassword 'set remote credentials sUser = "" sPassword = "" 'open list of client names Set oFSO = CreateObject("Scripting.FileSystemObject") Set oTS = oFSO.OpenTextFile("C:\Shutdown\SnrICTSuite.txt") Do Until oTS.AtEndOfStream 'get next client name sClient = oTS.ReadLine 'get WMI locator Set oLocator = CreateObject("WbemScripting.SWbemLocator") 'Connect to remote WMI Set oConnection = oLocator.ConnectServer(sClient, _ "root\cimv2", sUser, sPassword) 'issue shutdown to OS ' 4 = force logoff ' 5 = force shutdown ' 6 = force rebooot ' 12 = force power off Set oWindows = oConnection.ExecQuery("Select " & _ "Name From Win32_OperatingSystem") For Each oSys In oWindows oSys.Win32ShutDown(12) Next Loop 'close the text file oTS.Close 1
dapaulio Posted January 23, 2016 Posted January 23, 2016 I use to use psshutdown however something happened that broke it one day and never had the time to work out a fix. I replace psshutdown with the resident command shutdown which works however removes the ability for the user to abort the shutdown. I have a script i designed myself which pings all my vlan then creates a list of active computers. After finding them it will proceed to shut them down the computer after 10 minutes of warnings. I will post the link to another thread that I shared my script once i found it.
dapaulio Posted January 23, 2016 Posted January 23, 2016 (edited) http://www.edugeek.net/showthread.php?t=162105 You will see I have rem out the psshutdown command. Feel free to fix it if you want. Edited January 23, 2016 by dapaulio 1
protocol Posted January 25, 2016 Author Posted January 25, 2016 Wow thanks all was expecting a few "batch files are easy you n00b" replies but you've been more than helpful so far.
round2it Posted January 25, 2016 Posted January 25, 2016 have a look at emco remote shutdown cheap but good 1
jjohnsoncantell Posted February 2, 2016 Posted February 2, 2016 I have this one which I believe I got from edugeek (I think). It shutdown clients while logging to a file which ones responded and which ones didn't (if they are still awake). 'Dim FSO, strFile, strFolder, objFSO, objFile, objShellstrFile = "shutdownlog.txt" strFolder = "C:\Shutdown Script" strDate = Date() strTime = Time() set objShell = WScript.CreateObject ("WScript.Shell") Set FSO = CreateObject("Scripting.FileSystemObject") Set WSHShell = WScript.CreateObject("WScript.Shell") Set oFS = CreateObject("Scripting.FileSystemObject") Set objWMIService = GetObject("winmgmts:\\.\root\cimv2") Set oTS = oFS.OpenTextFile("C:\shutdown script\computers.txt") strWarning = "Your computer is scheduled to shut down 10 minutes from the appearance of this message. Please save your work. If you wish to carry on working please restart the machine after it has powered down. " strDelay = 600 'Delay given in seconds; change this value to your preference, or set it to 0 to give no delay at all Do Until oTS.AtEndOfStream 'get the next computer name 'store it in variable strComputer strComputer = oTS.ReadLine 'WScript.Echo strComputer Set colItems = objWMIService.ExecQuery("Select * from Win32_PingStatus Where Address = '" & strComputer & "'") For Each objItem in colItems If objItem.StatusCode = 0 Then WshShell.Run "C:\Windows\System32\shutdown.exe -m \" & strComputer & " -s -f -c " & Chr(34) & strWarning & Chr(34) & " -t " & strDelay 'Replace the "-r" switch with "-s" to make the computers shutdown instead 'Output to log set objFSO = CreateObject("Scripting.FileSystemObject") set objFile = objFSO.OpenTextFile(strFolder&strFile, 8, True) objFile.WriteLine(strComputer&" "&strDate&" "&strTime) objFile.Close Else If objItem.StatusCode = 11010 Then set objFSO = CreateObject("Scripting.FileSystemObject") set objFile = objFSO.OpenTextFile(strFolder&strFile, 8, True) objFile.WriteLine(strComputer&" Not responding") objFile.Close Else If objItem.StatusCode = 11003 Then set objFSO = CreateObject("Scripting.FileSystemObject") set objFile = objFSO.OpenTextFile(strFolder&strFile, 8, True) objFile.WriteLine(strComputer&" Not responding") objFile.Close End If End If End If Next Loop 'close the text file oTS.Close 1
protocol Posted February 2, 2016 Author Posted February 2, 2016 (edited) Thanks will give that a go if I can't get PSEXEC to work. At the moment I have psshutdown.exe, my computers.txt and my shutdown.bat file in a folder on my DC. If I run the .bat file myself it shuts down the computers like I'd expect it to but as soon as I put it as a scheduled task it stops working. I noticed that when I first ran the bat file, I had to accept some EULA agreement before the script kicked in. Thinking that was the issue I added it to my bat file but still no joy. psshutdown -accepteula @COMputers.txt -u adminuser -p adminpass -c -f -t 120 > C:\temp\psshutdown.txt 2>&1 -m "Your computer is about to shutdown. You have two minutes to save and close your work. Click cancel to cancel the shutdown process." I then added the command to create a log file every time it ran which gives me the following error: 'psshutdown' is not recognized as an internal or external command, operable program or batch file. I started looking into this over 2 weeks ago and still no further forward:( who knew it was so hard to shutdown some computers over night! Tried running this .bat file with and without admin details stored in it. Edited February 2, 2016 by protocol
Davit2005 Posted February 2, 2016 Posted February 2, 2016 We use GPO to create a Task Schedule on the target PC's even if someone is smart and has admin and they delete it puts the task back on the PC at the next reboot :-)
jaminben Posted February 2, 2016 Posted February 2, 2016 have a look at emco remote shutdown cheap but good +1 for this.... we use it and it's excellent.
jjohnsoncantell Posted February 2, 2016 Posted February 2, 2016 @protocol That was exactly the problem i was having and why i switched to the script i posted earlier. There is a reason why psshutdown doesnt play well with windows 7 but I cant remember exactly what it was, something about the program having to run in interactive mode on the desktop. Got it!
round2it Posted February 2, 2016 Posted February 2, 2016 just to add emco remote shutdown does wol and the ability to push a message to all machines. i find this very useful
protocol Posted February 3, 2016 Author Posted February 3, 2016 @jjohnsoncantell - Link doesn't work but having seen how the message appears on the desktop when I've been testing this I can see how it could be difficult to get working properly. For me it appears as a new item on the task bar that the user has to click which isn't an ideal solution anyway. I'll try your script today and see how I get on. Thanks for the help/suggestion everyone. If I still continue to struggle I'll look into a paid solution as recommended.
ricki Posted February 3, 2016 Posted February 3, 2016 Have a look at http://www.edugeek.net/blogs/simpsonj/1923-windows-7-shutdown-through-group-policy-preferences.html 1
jjohnsoncantell Posted February 4, 2016 Posted February 4, 2016 @protocol I don't know why that link was there, I don't remember typing it. I'd be interested to know how you get on if you try my script.
protocol Posted February 4, 2016 Author Posted February 4, 2016 @jjohnsoncantell I had to tweak the script slightly, as it wouldn't work even when I just changed the URL of the files needed. Tested it as a scheduled task and all went to plan... until I got into work this morning to notice them still on. I made a school boy error and set it to one occurrence only rather than every day so I'll be in a better place to give feedback tomorrow.
dapaulio Posted February 4, 2016 Posted February 4, 2016 try my solution on this thread http://www.edugeek.net/forums/windows-7/162105-windows-7-shutdown-gpo.html I have been using this for years and is very effective.
protocol Posted February 5, 2016 Author Posted February 5, 2016 (edited) try my solution on this thread http://www.edugeek.net/forums/windows-7/162105-windows-7-shutdown-gpo.html I have been using this for years and is very effective. Will check it out thanks. @jjohnsoncartell still no luck with your script. It ran and it's last run result is 0x80070103 and last log entry is that the task exceeded the time allocation. It is currently set to stop after 2 hours. Surely it shouldn't take more than 2 hours to run through a shutdown script?! Edited February 5, 2016 by protocol
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now