Link to home
Start Free TrialLog in
Avatar of bheroniphr
bheroniphrFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Remote script for defrag

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
Avatar of sirbounty
sirbounty
Flag of United States of America image

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...)
Avatar of bheroniphr

ASKER

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

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

Hi Sirbounty,

No, it's an INF.

thanks,

Ben
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...
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
what about using rundll32?

rundll32 %systemroot%\inf\maint.inf
Hi Sirbounty.

Nope, same thing.

Ben
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"
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
ASKER CERTIFIED SOLUTION
Avatar of sirbounty
sirbounty
Flag of United States of America 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
sirbounty, wouldn't you want to run chkdsk before defrag? :)
i mean chkntfs
Yes, I was processing the code as outlined above, neglecting to double-check the order of operations...need more coffee...
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

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

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

oh...just drop a /f (force) at the end...that'll replace it...
Nice and easy!

Looks great. Thanks for all your help!

Ben
Avatar of mxrider_420
mxrider_420

How do i do this without a domain account?