I have a PowerShell script as follows:
$ListOfServers = "Server1", "Server2"
$RunDefrag =
{
param($Server)
$Volume = Get-WmiObject -Class Win32_Volume -ComputerName $Server -Filter "DriveLetter = 'c:'"
$Result = $Volume.Defrag($False)
if ($Result.ReturnValue -eq 0)
{ Write-Host "Defrag on $Server successful." }
else
{ Write-Host "Defrag failed. Result code: " $Result.ReturnValue }
}
$ListOfServers | ForEach {Start-Job -ScriptBlock $RunDefrag -ArgumentList $_}
This runs fine when executed manually as a PowerShell script from my machine (with Administrative rights).
My problem is when I try and set the task up as a Scheduled Task. Here are my basic settings:
Actions: Start a program.
Details: powershell "& \"C:\PowerShell Scripts\Defrag.ps1"
I have played around with various settings for the task details, but nothing works for me. The task Runs, but the defrag does not start on the servers.
If anyone has any ideas, I'd be grateful.