1

I have the following code that works fine on my Windows 7 computer:

$Defrag = Get-WmiObject Win32_Volume -Filter "DriveLetter='c:'" -ComputerName "SRLVHOMAPP01" | 
Invoke-WmiMethod -Name Defrag

But when I am trying to run this on a Windows Server 2003, nothing runs.

Has anyone got any ideas? I'm running with domain administrator account...

EDIT: I've found an addition combination of code as follows, but it gives me return of "Error code 8" which is apparently "Defrag is already in progress." - which it is definitely not.

$Servers = "CSEPULTAPU01"

ForEach ($Server in $Servers)
{
    $vol = Get-WmiObject -Class Win32_Volume -ComputerName $Server -Filter "DriveLetter = 'c:'"
    $res = $vol.Defrag($false)

    if ($res.ReturnValue -eq 0)
    {
        Write-Host "Defrag succeeded."
    }
    else 
    {
        Write-Host "Defrag failed Result code: " $res.ReturnValue
    }
}
The Woo
  • 447
  • 2
  • 6
  • 20

0 Answers0