@@ -143,54 +143,91 @@ function Import-EnvPath {
143143 [string ]$EnvTarget = ' All'
144144 )
145145
146+ Write-Debug " 开始重新加载PATH,模式: $EnvTarget "
147+ Write-Debug " 当前PATH长度: $ ( $env: Path.Length ) 字符"
148+
146149 switch ($EnvTarget ) {
147150 ' Machine' {
148- $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::Machine)
151+ $newPath = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::Machine)
152+ Write-Debug " 系统级PATH长度: $ ( $newPath.Length ) 字符"
153+ Write-Debug " 系统级PATH内容: $newPath "
154+ $env: Path = $newPath
149155 Write-Verbose " 已重新加载系统级PATH"
150156 }
151157 ' User' {
152- $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::User)
158+ $newPath = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::User)
159+ Write-Debug " 用户级PATH长度: $ ( $newPath.Length ) 字符"
160+ Write-Debug " 用户级PATH内容: $newPath "
161+ $env: Path = $newPath
153162 Write-Verbose " 已重新加载用户级PATH"
154163 }
155164 ' Process' {
156- $env: Path = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::Process )
165+ # [System.EnvironmentVariableTarget]::Process 获取的是当前这个 PowerShell 进程已经拥有的 Path 变量。所以,$newPath 的值和执行这行代码之前的 $env:Path 的值是完全一样的。
166+ # 这是一个无用操作
167+ $newPath = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::Process )
168+ Write-Debug " 进程级PATH长度: $ ( $newPath.Length ) 字符"
169+ Write-Debug " 进程级PATH内容: $newPath "
170+ $env: Path = $newPath
157171 Write-Verbose " 已重新加载进程级PATH"
158172 }
159173 ' All' {
160174 # 获取系统级和用户级PATH
161175 $machinePath = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::Machine)
162176 $userPath = [System.Environment ]::GetEnvironmentVariable(" Path" , [System.EnvironmentVariableTarget ]::User)
163177
178+ Write-Debug " 系统级PATH长度: $ ( $machinePath.Length ) 字符`n "
179+ Write-Debug " 用户级PATH长度: $ ( $userPath.Length ) 字符"
180+ Write-Debug " 系统级PATH: $machinePath "
181+ Write-Debug " 用户级PATH: $userPath "
182+
164183 # 合并PATH,系统级在前,用户级在后,并去除重复项
165184 $allPaths = @ ()
166185
167186 # 添加系统级PATH
168187 if ($machinePath ) {
169- $allPaths += $machinePath -split ' ;' | Where-Object { $_.Trim () -ne ' ' }
188+ $machinePaths = $machinePath -split ' ;' | Where-Object { $_.Trim () -ne ' ' }
189+ Write-Debug " 系统级PATH分割后数量: $ ( $machinePaths.Count ) "
190+ $allPaths += $machinePaths
170191 }
171-
192+ pn
172193 # 添加用户级PATH
173194 if ($userPath ) {
174- $allPaths += $userPath -split ' ;' | Where-Object { $_.Trim () -ne ' ' }
195+ $userPaths = $userPath -split ' ;' | Where-Object { $_.Trim () -ne ' ' }
196+ Write-Debug " 用户级PATH分割后数量: $ ( $userPaths.Count ) "
197+ $allPaths += $userPaths
175198 }
176199
200+ Write-Debug " 合并前总PATH数量: $ ( $allPaths.Count ) "
201+
177202 # 去除重复项,保持顺序(系统级优先)
178203 $uniquePaths = @ ()
179204 $seenPaths = @ {}
205+ $duplicateCount = 0
180206
181207 foreach ($path in $allPaths ) {
182208 $normalizedPath = $path.Trim ().TrimEnd(' \' ).ToLower()
183209 if (-not $seenPaths.ContainsKey ($normalizedPath ) -and $normalizedPath -ne ' ' ) {
184210 $uniquePaths += $path.Trim ()
185211 $seenPaths [$normalizedPath ] = $true
212+ Write-Debug " 添加唯一路径: $ ( $path.Trim ()) "
213+ }
214+ else {
215+ $duplicateCount ++
216+ Write-Debug " 跳过重复路径: $ ( $path.Trim ()) "
186217 }
187218 }
188219
189- $env: Path = $uniquePaths -join ' ;'
190- Write-Verbose " 已重新加载合并的系统级和用户级PATH,共 $ ( $uniquePaths.Count ) 个唯一路径"
220+ Write-Debug " 去重后唯一PATH数量: $ ( $uniquePaths.Count ) "
221+ Write-Debug " 跳过的重复PATH数量: $duplicateCount "
222+
223+ $finalPath = $uniquePaths -join ' ;'
224+ Write-Debug " 最终PATH长度: $ ( $finalPath.Length ) 字符"
225+ $env: Path = $finalPath
226+ Write-Verbose " 已重新加载合并的系统级和用户级PATH,共 $ ( $uniquePaths.Count ) 个唯一路径,去除了 $duplicateCount 个重复项"
191227 }
192228 }
193229
230+ Write-Debug " PATH重新加载完成,最终长度: $ ( $env: Path.Length ) 字符"
194231 Write-Host " PATH已重新加载" - ForegroundColor Green
195232}
196233
0 commit comments