aboutsummaryrefslogtreecommitdiffstats
path: root/coin/pre-provisioning/qtci-windows-10-x86_64/disable-defragment.ps1
blob: bb449e56f6b1d8878a5f7d31a25352d79ec0602b (plain)
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
# Copyright (C) 2019 The Qt Company Ltd.
# SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only

# Windows 7 does not have Get-ScheduledTask and Unregister-ScheduledTask
# thus needing its own version.
Write-Host "Disabling defragmentation"
$version = Get-CimInstance Win32_OperatingSystem | Select-Object -ExpandProperty Caption
if ($version -like '*Windows 7*'){
    $pi = New-Object System.Diagnostics.ProcessStartInfo
    $pi.FileName = "C:\Windows\System32\schtasks.exe"
    $pi.RedirectStandardError = $true
    $pi.UseShellExecute  = $false
    $pi.Arguments = "/Delete /TN `"\Microsoft\Windows\Defrag\ScheduledDefrag`" /F"
    $prog = New-Object System.Diagnostics.Process
    $prog.StartInfo = $pi
    $prog.Start() | Out-Null
    $err = $prog.StandardError.ReadToEnd()
    $prog.WaitForExit()
    if ($prog.ExitCode -eq 0){
        Write-Host "Scheduled defragmentation removed"
    } else {
        if ($err -like '*cannot find the file*'){
            Write-Host "No scheduled defragmentation task found"
            exit 0
        } else {
            Write-Host "Error while deleting scheduled defragmentation task: $err"
        }
    }
}
else {
    try {
        $state = (Get-ScheduledTask -ErrorAction Stop -TaskName "ScheduledDefrag").State
        Write-Host "Scheduled defragmentation task found in state: $state"
    }
    catch {
        Write-Host "No scheduled defragmentation task found"
        exit 0
    }
    Write-Host "Unregistering scheduled defragmentation task"
    Unregister-ScheduledTask -ErrorAction Stop -Confirm:$false -TaskName ScheduledDefrag
    Write-Host "Scheduled Defragmentation task was cancelled"
}