Home All Groups Group Topic Archive Search About

Schedule Defrag through GPO (non admin)

Author
16 Mar 2009 8:53 PM
twiggo
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 -- twiggo ------------------------------------------------------------------------ twiggo's Profile: http://forums.techarena.in/members/twiggo.htm View this thread: http://forums.techarena.in/server-scripting/1141820.htmhttp://forums.techarena.in

Author
16 Mar 2009 10:02 PM
Lanwench [MVP - Exchange]
twiggo <twiggo.3p5tnd@DoNotSpam.com> wrote:
Show quoteHide quote
> 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
Author
17 Mar 2009 9:09 PM
twiggo
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
Another question...I can run it with scheduled task but I need to run with Non admin account and finally don't work...
Could you help me modifing scripts?

Thanks

P.S: Sorry for my bad english ^_^ -- twiggo ------------------------------------------------------------------------ twiggo's Profile: http://forums.techarena.in/members/twiggo.htm View this thread: http://forums.techarena.in/server-scripting/1141820.htmhttp://forums.techarena.in
Author
17 Mar 2009 10:14 PM
Lanwench [MVP - Exchange]
twiggo <twiggo.3p7obb@DoNotSpam.com> 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.
Author
18 Mar 2009 1:18 AM
RemyMaza
On Mar 17, 5:09 pm, twiggo <twiggo.3p7...@DoNotSpam.com> 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


Having the script above do some reporting isn't too bad.  You just
need to add a function that calls a command prompt and adds command
line to it.  Here's an example:

Call CMD(Echo %computername% %date% %time% >> "\\server\share
\defraglog_%computername%.log")

Sub CMD(strCommand)
       WshShell.Run "%comspec% /k strCommand" '/k keeps the command
prompt open and is easy to use for troubleshooting; /c keeps the
command prompt closed and allows automation
End Sub

You will not be able to run the defrag command without admin
priveledges though.  I'm not sure what it would take either to open
defrag up to a standard user.  I briefly looked over GPO but that
didn't turn up much.  Process Monitor a defrag with a standard account
and you'll see all of the denied access entries.  You may have to stop
right there with pursuing opening defrag to standard users due to
securtiy, but I'm not sure what that will uncover.  Good Luck!

Regards,
Matt
Author
18 Mar 2009 8:18 AM
twiggo
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.htmhttp://forums.techarena.in
Author
18 Mar 2009 12:50 PM
Lanwench [MVP - Exchange]
twiggo <twiggo.3p8iva@DoNotSpam.com> wrote:
Show quoteHide quote
> 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.
Show quoteHide quote
>
> Thanks!
Author
20 Mar 2009 1:16 AM
John Fullbright
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)?



Show quoteHide quote
"twiggo" <twiggo.3p8iva@DoNotSpam.com> wrote in message
news:twiggo.3p8iva@DoNotSpam.com...
>
> 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
>