-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMotwCleaner-Fr.ps1
More file actions
188 lines (158 loc) · 7.91 KB
/
Copy pathMotwCleaner-Fr.ps1
File metadata and controls
188 lines (158 loc) · 7.91 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<#
.SYNOPSIS
MotwCleaner v1.0 - Déverrouilleur Windows MOTW (Mark of the Web)
S'installe dans le menu contextuel – fonctionne sur les dossiers, l'arrière-plan et les fichiers
.DESCRIPTION
Windows marque les fichiers téléchargés d'internet avec "Mark of the Web" (MOTW),
ce qui empêche leur exécution. Ce script supprime ce verrou de façon récursive.
#>
param([string]$StartDir)
# --- REDÉMARRAGE EN ADMIN ---
# Si non exécuté en tant qu'administrateur, redémarrage avec privilèges élevés
if (-not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
$CurrentDir = if ($StartDir) { $StartDir } else { (Get-Location).Path }
$Arguments = "-NoProfile -ExecutionPolicy Bypass -File `"$PSCommandPath`" -StartDir `"$CurrentDir`""
Start-Process powershell.exe -ArgumentList $Arguments -Verb RunAs
exit
}
# Définir le répertoire de travail – supprimer les guillemets et espaces du chemin
if ($StartDir) {
$StartDir = $StartDir.Trim().Trim('"').Trim("'")
# Si un fichier a été cliqué, utiliser son dossier parent comme cible
if (Test-Path $StartDir -PathType Leaf) {
$StartDir = Split-Path -Parent $StartDir
}
if (Test-Path $StartDir) {
Set-Location $StartDir
} else {
Write-Host " [AVERTISSEMENT] StartDir n'existe pas: '$StartDir'" -ForegroundColor Yellow
Write-Host " Utilisation du répertoire actuel: $((Get-Location).Path)" -ForegroundColor Yellow
}
}
$ScriptName = "MotwCleaner.ps1"
$TargetDir = "$env:SystemRoot\Scripts"
$FinalPath = Join-Path $TargetDir $ScriptName
# --- 1. LOGIQUE D'INSTALLATION ---
# Si le script ne s'exécute pas depuis son emplacement final, proposer l'installation
$CurrentLocation = $PSCommandPath
if (-not ($CurrentLocation.StartsWith($TargetDir, [System.StringComparison]::OrdinalIgnoreCase))) {
Write-Host "--- MODE INSTALLATION ---" -ForegroundColor Yellow
$Choice = Read-Host "Installer/Mettre a jour le script dans le systeme? (o/n)"
if ($Choice -ieq 'o') {
# Créer le répertoire cible s'il n'existe pas
if (-not (Test-Path $TargetDir)) {
New-Item -Path $TargetDir -ItemType Directory -Force | Out-Null
}
Copy-Item -Path $CurrentLocation -Destination $FinalPath -Force
Write-Host "Script copie vers: $FinalPath" -ForegroundColor Cyan
# --- Supprimer aussi les anciennes entrées RepoFixer (renommage du projet) ---
$OldEntries = @("Directory\\shell\\RepoFixer", "Directory\\Background\\shell\\RepoFixer", "*\\shell\\RepoFixer")
$HKCRClean = [Microsoft.Win32.Registry]::ClassesRoot
foreach ($OE in $OldEntries) { try { $HKCRClean.DeleteSubKeyTree($OE, $false) } catch {} }
# --- Opérations de registre via l'API .NET ---
# Microsoft.Win32.Registry gère directement la clé HKCR\*,
# sans blocage comme le fournisseur PS ou reg.exe.
$MenuLabel = "Supprimer le verrou (MotwCleaner)"
$MenuIcon = "powershell.exe,0"
# Commande pour dossier / arrière-plan: StartDir = le dossier lui-même
$CmdFolder = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$FinalPath`" -StartDir `"%1`""
$CmdBg = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$FinalPath`" -StartDir `"%V`""
# Commande pour fichiers: %1 = le fichier lui-même – le script extrait le dossier parent au démarrage
$CmdFile = "powershell.exe -NoProfile -ExecutionPolicy Bypass -File `"$FinalPath`" -StartDir `"%1`""
$RegEntries = @(
@{ Hive = "Directory\shell\MotwCleaner"; Cmd = $CmdFolder },
@{ Hive = "Directory\Background\shell\MotwCleaner"; Cmd = $CmdBg },
@{ Hive = "*\shell\MotwCleaner"; Cmd = $CmdFile }
)
$HKCR = [Microsoft.Win32.Registry]::ClassesRoot
foreach ($Entry in $RegEntries) {
Write-Host " Registre: HKCR\$($Entry.Hive)" -ForegroundColor DarkCyan
try {
# Nettoyage: supprimer l'ancienne clé si elle existe
try { $HKCR.DeleteSubKeyTree($Entry.Hive, $false) } catch {}
# Créer la nouvelle clé
$Key = $HKCR.CreateSubKey($Entry.Hive)
$Key.SetValue("", $MenuLabel)
$Key.SetValue("Icon", $MenuIcon)
$Key.Close()
$CmdKey = $HKCR.CreateSubKey("$($Entry.Hive)\command")
$CmdKey.SetValue("", $Entry.Cmd)
$CmdKey.Close()
Write-Host " OK" -ForegroundColor Green
}
catch {
Write-Host " ERREUR: $_" -ForegroundColor Red
}
}
Write-Host ""
Write-Host "Installation reussie!" -ForegroundColor Green
Write-Host "Le menu contextuel affiche maintenant: 'Supprimer le verrou (MotwCleaner)'" -ForegroundColor Green
Write-Host "Appuyez sur une touche pour quitter..."
try { $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") } catch { Read-Host }
exit
}
}
# --- 2. DÉVERROUILLAGE (le travail réel) ---
$Target = (Get-Location).Path
# --- LISTE DE BLOCAGE DE SÉCURITÉ ---
# Ne jamais traiter les dossiers système, même accidentellement
$BlockedPaths = @(
$env:SystemRoot,
"$env:SystemRoot\System32",
"$env:SystemRoot\SysWOW64",
"$env:SystemRoot\WinSxS",
"$env:SystemRoot\Scripts",
$env:ProgramFiles,
${env:ProgramFiles(x86)},
$env:ProgramData,
[System.Environment]::GetFolderPath("System"),
[System.Environment]::GetFolderPath("Windows")
)
foreach ($Blocked in $BlockedPaths) {
if (-not $Blocked) { continue }
if ($Target.TrimEnd('\') -eq $Blocked.TrimEnd('\') -or
$Target.StartsWith($Blocked.TrimEnd('\') + '\', [System.StringComparison]::OrdinalIgnoreCase)) {
Write-Host ""
Write-Host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" -ForegroundColor Red
Write-Host " DOSSIER SYSTEME PROTEGE - ACCES REFUSE " -ForegroundColor Red
Write-Host " Cible : $Target" -ForegroundColor Red
Write-Host " Raison : dans une zone protegee: $Blocked" -ForegroundColor Red
Write-Host "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" -ForegroundColor Red
Write-Host ""
Write-Host "Appuyez sur une touche pour quitter..." -ForegroundColor Yellow
try { $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") } catch { Read-Host }
exit 1
}
}
Write-Host ""
Write-Host "--- MotwCleaner ACTIF ---" -ForegroundColor Cyan
Write-Host " Cible: $Target" -ForegroundColor White
Write-Host ""
$Files = Get-ChildItem -Path $Target -Recurse -File -ErrorAction SilentlyContinue
$Total = $Files.Count
$Counter = 0
$Unblocked = 0
$Skipped = 0
foreach ($File in $Files) {
$Counter++
$Percent = [int](($Counter / [Math]::Max($Total, 1)) * 100)
Write-Progress -Activity "Suppression des verrous..." -Status "$Counter / $Total - $($File.Name)" -PercentComplete $Percent
try {
# Zone.Identifier stream meglétét Get-Item -Stream-mel ellenőrizzük
$WasLocked = $null -ne (Get-Item -Path $File.FullName -Stream "Zone.Identifier" -ErrorAction SilentlyContinue)
Unblock-File -Path $File.FullName -ErrorAction Stop
if ($WasLocked) { $Unblocked++ }
}
catch {
Write-Host " [SKIP] $($File.FullName): $_" -ForegroundColor DarkYellow
$Skipped++
}
}
Write-Progress -Activity "Suppression des verrous..." -Completed
Write-Host ""
Write-Host "TERMINE!" -ForegroundColor Green
Write-Host " Déverrouillé : $Unblocked fichier(s)"
Write-Host " Ignoré : $Skipped fichier(s) (accès refusé ou déjà déverrouillé)"
Write-Host ""
Write-Host "Appuyez sur une touche pour fermer..." -ForegroundColor Yellow
try { $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") } catch { Read-Host }