-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFix-PSModulePath.ps1
More file actions
162 lines (141 loc) · 5.53 KB
/
Copy pathFix-PSModulePath.ps1
File metadata and controls
162 lines (141 loc) · 5.53 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
[CmdletBinding()]
param(
[switch]$Apply,
[switch]$Cleanup
)
# Neue Modulpfade (außerhalb von OneDrive)
function Get-NewModulePath {
return @{
"PowerShell" = Join-Path $env:LOCALAPPDATA "PowerShell\Modules"
"WindowsPowerShell" = Join-Path $env:LOCALAPPDATA "WindowsPowerShell\Modules"
}
}
# OneDrive-Modulpfade (zu entfernen)
function Get-OneDriveModulePath {
$paths = @()
$roots = @($env:OneDrive, $env:OneDriveCommercial) | Where-Object { $_ }
$docNames = @("Documents", "Dokumente")
$moduleTypes = @("PowerShell\Modules", "WindowsPowerShell\Modules")
foreach ($root in $roots) {
foreach ($doc in $docNames) {
foreach ($type in $moduleTypes) {
$path = Join-Path (Join-Path $root $doc) $type
if (Test-Path $path) {
$paths += $path
}
}
}
}
return $paths
}
# Prüft ob ein Pfad ein OneDrive-Pfad ist
function Test-OneDrivePath {
param([string]$Path)
return ($Path -match '(?i)\\OneDrive( - [^\\]+)?(\\|$)')
}
# Filtert OneDrive-Pfade und fügt neue hinzu
function Get-CleanedModulePath {
param(
[string]$CurrentPath,
[hashtable]$NewPaths
)
$currentPaths = @()
if ($CurrentPath) {
$currentPaths = ($CurrentPath -split ';') | Where-Object { $_ }
}
$filteredPaths = $currentPaths | Where-Object { -not (Test-OneDrivePath $_) }
$newPathList = @()
foreach ($newPath in $NewPaths.Values) {
$normalized = $newPath.TrimEnd('\').ToLowerInvariant()
$exists = $filteredPaths | Where-Object { $_.TrimEnd('\').ToLowerInvariant() -eq $normalized }
if (-not $exists) {
$newPathList += $newPath
}
}
$finalPaths = @($newPathList) + @($filteredPaths) | Where-Object { $_ }
return @{
CurrentPaths = $currentPaths
FinalPaths = $finalPaths
NewUserPath = ($finalPaths -join ';')
}
}
# Hauptlogik
function Invoke-PSModulePathFix {
param(
[switch]$Apply,
[switch]$Cleanup
)
$newPaths = Get-NewModulePath
$userPathRaw = [System.Environment]::GetEnvironmentVariable("PSModulePath", "User")
$result = Get-CleanedModulePath -CurrentPath $userPathRaw -NewPaths $newPaths
$currentPaths = $result.CurrentPaths
$finalPaths = $result.FinalPaths
$newUserPath = $result.NewUserPath
# Status anzeigen
Write-Host "=== PSModulePath Fix ===" -ForegroundColor Cyan
Write-Host ""
Write-Host "Aktueller User-PSModulePath:" -ForegroundColor Yellow
if ($currentPaths.Count -eq 0) {
Write-Host " (leer)" -ForegroundColor DarkGray
} else {
$currentPaths | ForEach-Object {
$color = if (Test-OneDrivePath $_) { "Red" } else { "White" }
Write-Host " $_" -ForegroundColor $color
}
}
Write-Host ""
Write-Host "Neuer User-PSModulePath:" -ForegroundColor Yellow
$finalPaths | ForEach-Object { Write-Host " $_" -ForegroundColor Green }
# OneDrive-Ordner finden
$oneDriveFolders = Get-OneDriveModulePath
if ($oneDriveFolders.Count -gt 0) {
Write-Host ""
Write-Host "OneDrive-Modulordner gefunden:" -ForegroundColor Yellow
$oneDriveFolders | ForEach-Object { Write-Host " $_" -ForegroundColor Red }
}
if ($Apply) {
# Neue Verzeichnisse erstellen
foreach ($path in $newPaths.Values) {
if (-not (Test-Path $path)) {
New-Item -ItemType Directory -Path $path -Force | Out-Null
Write-Host "Erstellt: $path" -ForegroundColor Green
}
}
# User-Level PSModulePath setzen
[System.Environment]::SetEnvironmentVariable("PSModulePath", $newUserPath, "User")
Write-Host ""
Write-Host "User-PSModulePath aktualisiert." -ForegroundColor Green
# Aktuelle Session aktualisieren
$machinePath = [System.Environment]::GetEnvironmentVariable("PSModulePath", "Machine")
$env:PSModulePath = (@($newUserPath, $machinePath) | Where-Object { $_ }) -join ';'
if ($Cleanup -and $oneDriveFolders.Count -gt 0) {
Write-Host ""
foreach ($folder in $oneDriveFolders) {
$items = @(Get-ChildItem -Path $folder -ErrorAction SilentlyContinue)
if ($items.Count -gt 0) {
Write-Host "WARNUNG: $folder enthält $($items.Count) Module:" -ForegroundColor Yellow
$items | ForEach-Object { Write-Host " - $($_.Name)" -ForegroundColor DarkYellow }
}
$confirm = Read-Host "Löschen: $folder ? (ja/nein)"
if ($confirm -match '^(ja|j|yes|y)$') {
Remove-Item -Path $folder -Recurse -Force -ErrorAction SilentlyContinue
Write-Host "Gelöscht: $folder" -ForegroundColor Green
} else {
Write-Host "Übersprungen: $folder" -ForegroundColor Yellow
}
}
}
Write-Host ""
Write-Host "Fertig. Bitte PowerShell neu starten." -ForegroundColor Green
} else {
Write-Host ""
Write-Host "Simulation - keine Änderungen. Verwende -Apply zum Ausführen." -ForegroundColor Yellow
if ($oneDriveFolders.Count -gt 0) {
Write-Host "Verwende -Apply -Cleanup um OneDrive-Ordner zu löschen." -ForegroundColor Yellow
}
}
}
# Nur ausführen wenn nicht dot-sourced (ermöglicht Testen)
if ($MyInvocation.InvocationName -ne '.') {
Invoke-PSModulePathFix -Apply:$Apply -Cleanup:$Cleanup
}