Jump to content

Recommended Posts

Posted (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 by protocol
Posted
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?
  • Thanks 1
Posted

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 1
Posted
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 :)

Posted

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

  • Thanks 1
Posted
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.
  • Thanks 1
Posted

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.

  • Thanks 1
Posted

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

  • Thanks 1
Posted

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.

Posted

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

  • Thanks 1
Posted (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 by protocol
Posted
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 :-)
Posted

@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!

Posted

@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.

Posted
@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 :doh: so I'll be in a better place to give feedback tomorrow.
Posted (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 by protocol

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 account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...