Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1304
  • Last Modified:

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
0
bheroniphr
Asked:
bheroniphr
  • 9
  • 8
  • 2
  • +1
1 Solution
 
sirbountyCommented:
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
 
bheroniphrAuthor Commented:
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
 
sirbountyCommented:
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
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
bheroniphrAuthor Commented:
Hi Sirbounty,

No, it's an INF.

thanks,

Ben
0
 
sirbountyCommented:
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
 
bheroniphrAuthor Commented:
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
 
sirbountyCommented:
what about using rundll32?

rundll32 %systemroot%\inf\maint.inf
0
 
bheroniphrAuthor Commented:
Hi Sirbounty.

Nope, same thing.

Ben
0
 
sirbountyCommented:
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
 
bheroniphrAuthor Commented:
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
 
sirbountyCommented:
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
 
Justin_W_ChandlerCommented:
sirbounty, wouldn't you want to run chkdsk before defrag? :)
0
 
Justin_W_ChandlerCommented:
i mean chkntfs
0
 
sirbountyCommented:
Yes, I was processing the code as outlined above, neglecting to double-check the order of operations...need more coffee...
0
 
bheroniphrAuthor Commented:
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
 
sirbountyCommented:
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
 
bheroniphrAuthor Commented:
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
 
sirbountyCommented:
oh...just drop a /f (force) at the end...that'll replace it...
0
 
bheroniphrAuthor Commented:
Nice and easy!

Looks great. Thanks for all your help!

Ben
0
 
mxrider_420Commented:
How do i do this without a domain account?
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

  • 9
  • 8
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now