-
Notifications
You must be signed in to change notification settings - Fork 0
/
Defrag_and_optimize_VHDX.ps1
58 lines (49 loc) · 1.96 KB
/
Defrag_and_optimize_VHDX.ps1
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
###################################################################################
# Script provenant de : https://github.com/davidande/AUTO-DEFRAG-AND-OPTIMIZE-UPD #
###################################################################################
<# Vérification et installation si besoin des cmdlets Hyper-V (fonctionnalité Windows)
# Pour une éxécution depuis un serveur
$fonctionnalite = Get-WindowsFeature -Name Hyper-V-PowerShell
if (-not $feature.Installed) {
Install-WindowsFeature -Name Hyper-V-PowerShell
}
# Pour une execution depuis un poste
$feature = Get-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell
if ($feature.State -ne 'Enabled') {
Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Hyper-V-Management-PowerShell -All
}
#>
# Chemin des VHDX
$VHDXPath = '\\localhost\users_profils$'
# VHDX à exclure
$VHDXExclusion = 'UVHD-template.vhdx'
# Pourcentage de fragmentation max avant action
$VHDXfragmax = 10
# Traitement
$VHDXS = Get-ChildItem $VHDXPath -Recurse -Filter *.vhdx | Where-Object {$_.name -NotContains $VHDXExclusion} | Select-Object -ExpandProperty fullname
foreach ($VHDX in $VHDXS)
{
$VHDXPROP = Get-VHD $VHDX -ErrorAction Ignore
$VHDXDEFRAG = $VHDXPROP.FragmentationPercentage
if ($VHDXDEFRAG -igt $VHDXfragmax)
{
mount-VHD $VHDX
write-host "Traitement de" $VHDX -ForegroundColor Cyan
Start-Sleep -Seconds 3
$Drivebrut= Get-Partition (Get-DiskImage -ImagePath $VHDX).number | Get-Volume
$Drivefinal = $Drivebrut.DriveLetter + ':'
defrag $Drivefinal /h /x
defrag $Drivefinal /h /k /l
defrag $Drivefinal /h /x
defrag $Drivefinal /h /k
dismount-vhd $VHDX
Start-Sleep -Seconds 3
optimize-vhd $VHDX -Mode Full
write-host "Le disque" $VHDX "a été optimisé" -ForegroundColor Cyan
}
else
{
write-host "Le disque" $VHDX "n'est pas assez fragmenté pour être traité" -ForegroundColor Green
}
}
exit