Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > Re: Schedule Defrag through GPO (non admin)

Reply
Thread Tools Display Modes

Re: Schedule Defrag through GPO (non admin)

 
 
Lanwench [MVP - Exchange]
Guest
Posts: n/a

 
      03-16-2009
twiggo <> wrote:
> Hi all, I'm searching for a script that can add a scheduled task
> running a silent defrag 1 day a month at 1 A.M. and when it's
> finished generate the log of the process and place it on a server's
> shared folder (for example in \\server\share).
>
> It could be great if after the logs created in share folder send an
> email with the same log. The script must be run with an "system
> account" because all the users are only "power users"
> After that I would push it through gpo login script.
>
> Someone could help me? I found some scripts on the net but noone works
> at all in my case.
>
> Thanks in advance twiggo from Italy


I am no scripting guru, and I've never used this script but found it a while
ago on Doug Knox's very helpful website.

I don't know how to get it to create the scheduled task itself, but that
might be easy to look up. It's going to have to run as a domain account, if
you want the logs stored on a network drive. As far as emailing afterwards,
you'd have to figure out how to do that yourself - could use BLAT.

Third party defrag software (Raxco, Diskkeeper) can probably do a lot more
than this. The built-in Windows defrag is fairly limited.

[Note that in XP & up, letting users have 'power user' rights is nearly as
bad as letting them be local admins. ]

---------------------------------------------------------
'defrag_all2.vbs
'Defrags all hard disks - Can be run as a Scheduled Task
'Modified to create an error log and display it
'© Doug Knox - 4/13/2002

Option Explicit

Dim WshShell, fso, d, dc, ErrStr(), Return, X, A(), MyFile, I, MyBox, Drive

Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")
X = 0

Set dc = fso.Drives
For Each d in DC
If d.DriveType = 2 Then
X = X + 1

'Determine drive letter of first fixed disk
'This is the drive that the error report will be placed on
If X = 1 Then
Drive = d
End If
End If
Next

ReDim A(X)
ReDim ErrStr(X)

X = 0
For Each d in dc
If d.DriveType = 2 Then
X = X + 1
Return = WshShell.Run("defrag " & d & " -f", 1, TRUE)

'Determine the Error code returned by Defrag for the current drive and save
it
If return = 0 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag completed successfully" &
vbCRLF
elseif return = 1 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (defrag was cancelled manually) " & vbCRLF
elseif return = 2 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (there was a command line error. Check your command line for
valid switches and drives)" & vbCRLF
elseif return = 3 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (there was an unknown error)" & vbCRLF
elseif return = 4 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (defrag could not run due to insufficient memory resources)"
& vbCRLF
'errorlevel 5 is not currently used
elseif return = 5 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (general error)" & vbCRLF
elseif return = 6 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & "(System error: either the account used to run defrag is not an
administrator, there is a problem loading the resource DLL, or a defrag
engine could not be accessed. Check for proper user permissions and run
Sfc.exe to validate system files)" & vbCRLF
elseif return = 7 then
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with error level
" & return & " (There is not enough free space on the drive. Defrag needs
15% free space to run on a volume)" & vbCRLF
else
ErrStr(x) = ErrStr(x) & "Drive " & d & " Defrag aborted with an unknown
error level: " & return & vbCRLF
end if

End If
Next

'Create the Error Report in the root of the first fixed disk.
Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
MyFile.WriteLine(Date) & vbCRLF
MyFile.WriteLine(Time) & vbCRLF
For I = 1 to X
MyFile.WriteLine(ErrStr(I))
Next
MyFile.Close

Return = WshShell.Run(Drive & "\defragreport.txt",3,True)

Set WshShell = Nothing
Set fso = Nothing



 
Reply With Quote
 
 
 
 
Lanwench [MVP - Exchange]
Guest
Posts: n/a

 
      03-17-2009
twiggo <> wrote:
> Thanks for replY!
>
> I tried this script but I have a problem..
> I need to customize this scripts saving logfile in server shared
> folder with this format : for ex. defraglog_%computername%.log


I'm sure that's possible, but it isn't something I know how to do.

> Another question...I can run it with scheduled task but I need to run
> with Non admin account


You have to be an admin to run a defrag. And if you want this to save on a
network drive it has to be a domain account with local admin rights.

> and finally don't work...
> Could you help me modifing scripts?
>
> Thanks
>
> P.S: Sorry for my bad english ^_^


No worries - your English is fine. My scripting is not so good.



 
Reply With Quote
 
Lanwench [MVP - Exchange]
Guest
Posts: n/a

 
      03-18-2009
twiggo <> wrote:
> thanks RemyMaza & Lanwench
>
> How can I insert those lines of codes you pasted?
>
> At this point I think....
>
> 'Create the Error Report in the root of the first fixed disk.
> Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
> MyFile.WriteLine(Date) & vbCRLF
> MyFile.WriteLine(Time) & vbCRLF
> For I = 1 to X
> MyFile.WriteLine(ErrStr(I))
> Next
> MyFile.Close
>
> Return = WshShell.Run(Drive & "\defragreport.txt",3,True)
>
> Set WshShell = Nothing
> Set fso = Nothing
>
>
> Could you make an example? And...are you sure working with UNC name
> with command shell?
>
> And...
>
> Last but not the least...I need to shedule it (with non administrative
> privilege)


You can't do that.
>
> Thanks!




 
Reply With Quote
 
John Fullbright
Guest
Posts: n/a

 
      03-20-2009
That's the limitation of the built in defrag cmdlet; you can's schedule it.
You might need to but the full version. BTW: why are you doing this( the
defrag that is)?



"twiggo" <> wrote in message
news:...
>
> thanks RemyMaza & Lanwench
>
> How can I insert those lines of codes you pasted?
>
> At this point I think....
>
> 'Create the Error Report in the root of the first fixed disk.
> Set MyFile = fso.OpenTextFile(Drive & "\defragreport.txt", 2, True)
> MyFile.WriteLine(Date) & vbCRLF
> MyFile.WriteLine(Time) & vbCRLF
> For I = 1 to X
> MyFile.WriteLine(ErrStr(I))
> Next
> MyFile.Close
>
> Return = WshShell.Run(Drive & "\defragreport.txt",3,True)
>
> Set WshShell = Nothing
> Set fso = Nothing
>
>
> Could you make an example? And...are you sure working with UNC name
> with command shell?
>
> And...
>
> Last but not the least...I need to shedule it (with non administrative
> privilege)
>
> Thanks!
>
>
> --
> twiggo
> ------------------------------------------------------------------------
> twiggo's Profile: http://forums.techarena.in/members/twiggo.htm
> View this thread: http://forums.techarena.in/server-scripting/1141820.htm
>
> http://forums.techarena.in
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
schedule defrag to mulitple servers ( schedule task) mr555 Windows Server 6 08-09-2009 09:53 PM
How to disable defrag schedule? Crazy Noddy Windows Vista General Discussion 3 08-17-2007 11:59 PM
Vista Defrag schedule? Lakesidezx Windows Vista Performance 7 02-02-2007 05:54 PM
Schedule to Defrag a certain drive samG Windows Server 3 12-08-2005 08:50 PM
Schedule Auto Defrag Roget Windows Server 2 09-22-2004 01:32 PM



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59