Solved

Remote script for defrag

Posted on 2009-03-30
20
1,277 Views
Last Modified: 2012-06-27
Hi,

I'm trying to create a script that I can deploy over the network that will run MS defrag at a scheduled time and then shut the system down. I have a mix of XP & Vista desktops all on x64.

I've got 3 scripts, first one to run the defrag(maint.bat), another to provide the shutdown facility and lastly the actual script the task will run (defrag.bat). Unfortunately, it doesn't quite work! Can anyone help out?

Here is what I have so far:

----------------------------------------------------------------
MAINT.BAT
cd\
c:
cd windows\system32
defrag c: -w -v
chkntfs c:

----------------------------------------------------------------
MAINT.INF
[version]
Signature= "$CHICAGO$"
AdvancedINF= 2.5,"advpack.dll"

; This is the install part.

[DefaultInstall]
RunPreSetupCommands=Tst.PreSetup
RunPostSetupCommands=Tst.PostSetup

[Tst.PreSetup]
c:\Users\ben.nias\Desktop\defrag\maint.bat

[Tst.PostSetup]
c:\WINDOWS\SYSTEM32\SHUTDOWN.EXE -s -t 0 -f

----------------------------------------------------------------
DEFRAG.BAT
c:\WINDOWS\System32\rundll32.exe advpack.dll,LaunchINFSectionEx
c:\Users\ben.nias\Desktop\defrag\maint.inf,DefaultInstall,,32
0
Comment
Question by:bheroniphr
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
1) Does it run if you launch it manually?
2) What user is the task scheduled under? (If default SYSTEM, then you'll never see it run...)
0
 

Author Comment

by:bheroniphr
Comment Utility
Hi,

1) Sort of, it gets so far but pops up the inf in notepad as if there is a syntax error

2) Not tried yet,. |This is the easy bit so I'm actually just trying to get the script to run first.

Thanks

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
This is supposed to be a batch file?

MAINT.INF
[version]
Signature= "$CHICAGO$"
AdvancedINF= 2.5,"advpack.dll"

; This is the install part.

[DefaultInstall]
RunPreSetupCommands=Tst.PreSetup
RunPostSetupCommands=Tst.PostSetup

[Tst.PreSetup]
c:\Users\ben.nias\Desktop\defrag\maint.bat

[Tst.PostSetup]
c:\WINDOWS\SYSTEM32\SHUTDOWN.EXE -s -t 0 -f

Are you trying to create that file?  This will actually build it for you...if that's what you're trying to do?
Set filename=%systemroot%\inf\Maint.inf

echo.MAINT.INF >%filename%

echo.[version]>>%filename%

echo.Signature= "$CHICAGO$">>%filename%

echo.AdvancedINF= 2.5,"advpack.dll">>%filename%

echo.>>%filename%

echo.; This is the install part.>>%filename%

echo.>>%filename%

echo.[DefaultInstall]>>%filename%

echo.RunPreSetupCommands=Tst.PreSetup>>%filename%

echo.RunPostSetupCommands=Tst.PostSetup>>%filename%

echo.>>%filename%

echo.[Tst.PreSetup]>>%filename%

echo.c:\Users\ben.nias\Desktop\defrag\maint.bat>>%filename%

echo.>>%filename%

echo.[Tst.PostSetup]>>%filename%

echo.c:\WINDOWS\SYSTEM32\SHUTDOWN.EXE -s -t 0 -f>>%filename%

Open in new window

0
 

Author Comment

by:bheroniphr
Comment Utility
Hi Sirbounty,

No, it's an INF.

thanks,

Ben
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
where/how do you call it though?

You can try passing it through start:

start maint.inf

unless it's associated with notepad, it should launch the installer...
0
 

Author Comment

by:bheroniphr
Comment Utility
Hi Sirbounty,

Nope, calling it like that just makes notepad open with it.

I'm pretty sure it does that because that is the default behaviour for WIndows when an INF has an error in it.

Ben
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
what about using rundll32?

rundll32 %systemroot%\inf\maint.inf
0
 

Author Comment

by:bheroniphr
Comment Utility
Hi Sirbounty.

Nope, same thing.

Ben
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Is there any reason you can't deviate from this process?
For instance, were I to deploy this to a number of remote targets, I would use this method (from a command line):


for /f %a in (c:\computers.txt) do schtasks /create /s %a /ru system /sc weekly /st 01:00:00 /tn DefragAndOff /tr "defrag c: -w -v & chkntfs c: & SHUTDOWN.EXE -s -t 0 -f"
0
 

Author Comment

by:bheroniphr
Comment Utility
Wow, that looks great!

No problem at all with using a different solution. Could you describe what the constituent parts of the command do? (I'd rather understand fully how it works)

Thanks!

Ben
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 67

Accepted Solution

by:
sirbounty earned 125 total points
Comment Utility
The above line is a simple statement run from a command line (since technically this would only need to be run once).
You can set it up as a batch file, but the rule is - you have to replace all %a with %%a.
So, for an explanation of what the command does, in a nut shell:


for /f is a command/parameter that allows you to loop through a collection.  In this case, the collection is computers.txt, a text file holding your computer names in the format of
computer1
computer2
etc
(basically only a computer name on each line and a carriage return at the end).

So, what happens is, as each line is read from the file, it is temporarily assigned a variable, in this case %a (or %%a if you're using the batch version).
So, the first 'pass', %a is now equal to computer1.
Now that we have a variable assignment, the command is executed after the 'do' portion, i.e. (with variable assignment:

schtasks /create /s computer1 /ru system /sc weekly /st 01:00:00 /tn DefragAndOff /tr "defrag c: -w -v & chkntfs c: & SHUTDOWN.EXE -s -t 0 -f"

which is simply utilizing the command-line version of schtasks to create a task on the remote computer (/s), that would run under system credentials (/ru), scheduled to run weekly (/sc) (you can also specify a day modifier if you need to with /SD mm/dd/yyyy to specify the start date).  The /st specifies the start time (in the above example, 1am), and the task name is defined as DefragAndOff (could be anything, but if it contains spaces, it gets tricky in this setup, but can be done.  Lastly, the task run information combines all 3 of your commands with the & sign.  Run one, then (&) the next, etc...
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
Comment Utility
sirbounty, wouldn't you want to run chkdsk before defrag? :)
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
Comment Utility
i mean chkntfs
0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Yes, I was processing the code as outlined above, neglecting to double-check the order of operations...need more coffee...
0
 

Author Comment

by:bheroniphr
Comment Utility
Hi guys,

Getting on very well btw. Thanks so much for all your help so far!

I've modded the script to allow auto completion of the txt from AD. I've added all relevant depts to the list (just left 3 in the script for demo purposes)

This does work OK but have a few issues left.

1. MS defrag (as we all know) is naff. I'm just building an MSI of a excellent free defragger to do a  network rollout.
2. How can I change the order of chkntfs / defrag? All systems will be having this run on C: and so require a restart. Will I need to make two separate sched tasks?
3. (Please don't laugh at this one) How can I clean up the very end of the script? I'm piping in a text file with the letter "y" in it to get round the overwrite query from each host present from second install onwards. There must be a better way!

Thanks,

Ben




@ECHO OFF

REM 		--- Automated Defrag Script ---
 
 

REM Deleting old HOSTNAME list

cd\

cd winnt\sysvol\sysvol\XXXX.com\scripts\defragger

del /f computers.txt
 
 

REM Pulling HOSTNAME variables from AD and writing to file

setlocal

set BaseOU=OU=clients,DC=XXXX,DC=com

dsquery computer OU=XXXX1,%BaseOU% > computers.txt

dsquery computer OU=XXXX2,%BaseOU% >> computers.txt

dsquery computer OU=XXXX3,%BaseOU% >> computers.txt
 
 

REM Creating tasks from HOSTNAME list

for /f "Tokens=2 Delims=cn=," %%a in (c:\WINNT\SYSVOL\sysvol\XXXX.com\scripts\Defragger\computers.txt) do schtasks /create /s %%a /ru system /sc weekly /D THU /st 02:00:00 /tn DefragAndOff /tr "defrag c: -w -v & chkntfs c: & SHUTDOWN.EXE -s -t 0 -f" < "yes.txt"

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
Try this
Although I'm unsure what the issue is with the Y prompt - what's generating that?
You can typically pipe it in pre or post command:
i.e
echo y|do command

you mentioned overwriting hosts file?  I don't see it (but still working on first cup of coffee)


@ECHO OFF

REM             --- Automated Defrag Script ---

 

 

REM Deleting old HOSTNAME list

cd\

cd winnt\sysvol\sysvol\XXXX.com\scripts\defragger

del /f computers.txt

 

 

REM Pulling HOSTNAME variables from AD and writing to file

setlocal

set BaseOU=OU=clients,DC=XXXX,DC=com

REM if the OU is really sequential, you could also use for /l ...

REM I would also specify the path for computers.txt, but I tend to be overly cautious, particularly in your del line above

for %%A in (XXXXX1 XXXXX2 XXXXX3) do dsquery computer OU=%%A, %BaseOU% >> computers.txt

 

 

REM Creating tasks from HOSTNAME list

for /f "Tokens=2 Delims=cn=," %%a in (c:\WINNT\SYSVOL\sysvol\XXXX.com\scripts\Defragger\computers.txt) do schtasks /create /s %%a /ru system /sc weekly /D THU /st 02:00:00 /tn DefragAndOff /tr "defrag c: -w -v & chkntfs c: & SHUTDOWN.EXE -s -t 0 -f" < "yes.txt"

Open in new window

0
 

Author Comment

by:bheroniphr
Comment Utility
Hi,

Well, the computers.txt file (regenerated on each run) obviously causes the schtasks to try and generate a NEW task when one already exists on each client that has already had the script run against it. The "y" is to confirm overwrite of the task. I need it to run this way to ensure we include any new clients added to dept OU's.

So, is the syntax at the end of the script to be:
...SHUTDOWN.EXE -s -t 0 -f"
echo y
?

And no mention of overwriting the HOSTS file :-)

0
 
LVL 67

Expert Comment

by:sirbounty
Comment Utility
oh...just drop a /f (force) at the end...that'll replace it...
0
 

Author Comment

by:bheroniphr
Comment Utility
Nice and easy!

Looks great. Thanks for all your help!

Ben
0
 
LVL 1

Expert Comment

by:mxrider_420
Comment Utility
How do i do this without a domain account?
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

This article was inspired by a question here at Experts Exchange (http://www.experts-exchange.com/Software/Photos_Graphics/Images_and_Photos/Q_28629170.html). The requirements stated in that question are (1) reduce the file size of a large number of…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

763 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

6 Experts available now in Live!

Get 1:1 Help Now