Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Remote script for defrag

Posted on 2009-03-30
20
Medium Priority
?
1,300 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 8
  • 2
  • +1
20 Comments
 
LVL 67

Expert Comment

by:sirbounty
ID: 24019039
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
ID: 24019178
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
ID: 24019220
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:bheroniphr
ID: 24019249
Hi Sirbounty,

No, it's an INF.

thanks,

Ben
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24019263
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
ID: 24019802
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
ID: 24020305
what about using rundll32?

rundll32 %systemroot%\inf\maint.inf
0
 

Author Comment

by:bheroniphr
ID: 24020712
Hi Sirbounty.

Nope, same thing.

Ben
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24020761
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
ID: 24021045
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
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 24028195
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
ID: 24030867
sirbounty, wouldn't you want to run chkdsk before defrag? :)
0
 
LVL 6

Expert Comment

by:Justin_W_Chandler
ID: 24030870
i mean chkntfs
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 24030973
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
ID: 24087347
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
ID: 24087549
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
ID: 24089341
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
ID: 24089356
oh...just drop a /f (force) at the end...that'll replace it...
0
 

Author Comment

by:bheroniphr
ID: 24099884
Nice and easy!

Looks great. Thanks for all your help!

Ben
0
 
LVL 1

Expert Comment

by:mxrider_420
ID: 27405273
How do i do this without a domain account?
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

AutoHotkey is an excellent, free, open source programming/scripting language for Windows. It started out as a keyboard/mouse macros product, but has expanded into a robust language. This article provides an introduction to it, with links to addition…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

610 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