Windows Server - GPO: Scheduled task to defrag servers

Asked By paulreim
05-Aug-08 09:06 PM
Hello,
I am using this small script to automatically created scheduled tasks
via Group Policy on each one of our servers:

Set WshShell = WScript.CreateObject("WScript.Shell")
Dim fso, d, dc, i, Command
i=0
Set fso = CreateObject("Scripting.FileSystemObject")
Set dc = fso.Drives
For Each d in dc
i=i+1
If d.DriveType = 2 Then
Return = WshShell.Run("echo ""N""|schtasks.exe /create /SC WEEKLY /D
MON /TN Defrag" &i& " /TR ""defrag.exe "&d &" -f"" /ST 15:50:00 /SD
04/08/2008 /RU SYSTEM", 1, TRUE)
End If
Next
Set WshShell = Nothing

The echo ""N"" is necessary because on the second restart, Windows
asks to overwrite the scheduled task. If I omit this, the GPO works
fine the first time, but the after the second reboot, it prompts to
overwrite the task, so I manually have to confirm.....


When I run the script now with  the echo ""N""|, I get an error "The
system cannot find the file specified".

My question is now, what's wrong with the "Wshshell.run"-command or is
there any possibility like a "/N" to confirm that I do not want to
overwrite the scheduled task....

Best regards
PR
Scripting.FileSystemObject
(1)
WScript.CreateObject
(1)
WScript.Shell
(1)
VB
(1)
VBScript
(1)
CreateObject
(1)
WshShell
(1)
Maillist
(1)
  Florian Frommherz [MVP] replied...
05-Aug-08 05:25 AM
Paul,


I'm not a VBScript guy but would know another way to do that. You might
think about it if there are no other responses from smarter VB Script
people:

(option 1) Create a script that does two things: First, it copies a
batch file that runs the defrag. Second, it creates a scheduled task
using schtasks.exe to schedule the task with the first batch. Then
(after the first runthorugh) create a text file somewhere you can check
for. Before the whole script runs, always check for the text file. Itf
the script ran once, the text file is there and you can abort. If the
text file isn't there, run the whole script (as it hasn't run before -
otherwise the file would be there).

(option 2) Use Group Policy Preferences. They come with a Scheduled Task
Preference.

Why is it, you need to defrag your servers, anyway?

cheers,

Florian
--
Microsoft MVP - Group Policy
eMail: prename [at] frickelsoft [dot] net.
blog: http://www.frickelsoft.net/blog.
Maillist (german): http://frickelsoft.net/cms/index.php?page=mailingliste
help
name on command line has maximum length of 28 characters / / = = = = = = = = = = = = = = = = = = = = = = = = = = = [ Variables ] = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = var fso = new ActiveXObject("Scripting.FileSystemObject"); var FileHandle, ExitStatus = 0, BackupStatus = 0; var ForReading = 1, ForWriting = 2, ForAppending = 8; var EmailBody is enabled", LogHandle); IoXMLDataRaw("close", "Tape_Drives", LogHandle, ""); } return(tape0); } function GetTempPath(os) { var wsh; wsh = WScript.CreateObject("WScript.Shell").environment; return(wsh("TEMP")); } / / = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = / / JScript Library module: email functions / / Author: David Hewison / / Version: 2007.02 var ExitCode, LogF = 0x2; if (FsLogFlag(LogF)) IoLogEvent("open", LogHandle, "FsCheckFileExists"); var fso = new ActiveXObject("Scripting.FileSystemObject"); ExitCode = fso.FileExists(FileName) if (FsLogFlag(LogF)) { if (ExitCode) IoLogEvent("content", LogHandle, FileName +" does exist
processing of this batch?" set / a Count+ = 1 echo %Count% times ) Windows Server Scripting Discussions Scripting.FileSystemObject (1) WScript.CreateObject (1) OFSO.CreateFolder (1) OFSO.FolderExists (1) WScript.Arguments (1) OFSO.GetFolder (1) ToFSO.MoveFile Instead of using a batch file and the legacy "ask" utility, you could use a VB Script solution and get a nice GUI interface thrown in free of charge. Have a look at this one: 01. Set oFSO = CreateObject("Scripting.FileSystemObject") 02. Set oArgs = WScript.Arguments 03. Set oShell WScript.CreateObject("WScript.Shell") 04. sSource = Replace(oArgs(0) & " \ ", " \ ", " \ ") 05. sTarget = Replace(oArgs(1) & " \ ", " \ ", " \ ") 06. 07
could try this script: sServerList = "d: \ temp \ Servers.txt" sOutputFolder = "d: \ temp \ " Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close C: \ DST" Set objNet = CreateObject("WScript.NetWork") Set objComputer = CreateObject("Shell.LocalMachine") Set oFSO = CreateObject("Scripting.FileSystemObject") Set oServerList = oFSO.OpenTextFile(sServerList) While Not oServerList.AtEndOfStream ProcessServer(oServerList.ReadLine) Wend oServerList.Close root \ cimv2") Set objNet = CreateObject("WScript.NetWork") Set objComputer = CreateObject("Shell.LocalMachine") Set objFSO = CreateObject("Scripting.FileSystemObject") Set oFilesys = CreateObject("Scripting.FileSystemObject") Set oFiletxt = oFilesys.CreateTextFile("C: \ DST \ server_dst.log", True) sPath = oFilesys.GetAbsolutePathName("C: \ DST \ server_dst