Powershell : reset de Windows Update

Toujours dans la lignée de la campagne de patchs pour Meltdown et Spectre, j’ai développé un script permettant d’effectuer les manipulations nécessaires à remettre le composant Windows Update en bon état de marche. Il arrive que le service Windows Update soit incapable de trouver des patchs à installer ou qu’il bute sur des erreurs lors de l’installation des patchs. Une fois qu’on a écarté les causes système habituelles (pas de contact du serveur WSUS, espace disque insuffisant sur le système, service non démarré…), il peut être nécessaire de supprimer les fichiers de Windows Update pour le forcer à reprendre comme si le serveur était fraîchement installé, sans que cela ne retire les patchs, bien évidemment.

J’ai donc développé un script en Powershell qui va couper les services, renommer – et pourquoi pas – supprimer ces répertoires.

echo "Stopping Cryptographic Service Provider."
net stop cryptsvc
echo "Stopping the Windows Update service."
net stop wuauserv
echo "Checking presence of .old directories."
if ((Test-Path C:\Windows\SoftwareDistribution.old) -eq $true) {
$delSD = Read-Host -Prompt "The script needs to delete C:\Windows\SoftwareDistribution.old which probably is a backup of the SoftwareDistribution folder. Type 'N' if you want to abort."
if ($delSD -eq 'N') {break}
Remove-Item C:\Windows\SoftwareDistribution.old -Force -Recurse
}
if ((Test-Path C:\Windows\System32\catroot2.old) -eq $true) {
$delCR = Read-Host -Prompt "The script needs to delete C:\Windows\System32\catroot2.old which probably is a backup of the catroot2 folder. Type 'N' if you want to abort."
if ($delCR -eq 'N') {break}
Remove-Item C:\Windows\System32\catroot2.old -Force -Recurse
}
echo "Renaming WindowsSoftwareDistribution to SoftwareDistribution.old"
$SD = $true
try {
Rename-Item -Path C:\Windows\SoftwareDistribution -NewName C:\Windows\SoftwareDistribution.old -ErrorAction Stop
}
catch {
echo "Folder doesn't exist, can't rename."
$SD = $false
}
echo "Renaming Windows\System32\catroot2 to catroot2.old"
$CR = $true
try {
Rename-Item -Path C:\Windows\System32\catroot2 -NewName C:\Windows\System32\catroot2.old -ErrorAction Stop
}
catch {
echo "Folder doesn't exist, can't rename."
$CR = $false
if ($SD -eq $false -And $CR -eq $false) {
echo "Both folders are missing. Aborting."
break
}
}
$delYN = Read-Host -Prompt "Do you want to delete the old folders ? The script will only do so if you answer 'Y'"
if ($delYN -eq 'Y')
{
if ($SD -eq $true) {Remove-Item C:\Windows\SoftwareDistribution.old -Force -Recurse}
if ($CR -eq $true) {Remove-Item C:\Windows\System32\catroot2.old -Force -Recurse}
}
echo "Starting the services previously stopped."
net start cryptsvc
net start wuauserv
echo "Done."
Lien pour marque-pages : Permaliens.

Laisser un commentaire

Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.