forked from ChrisTitusTech/winutil
-
Notifications
You must be signed in to change notification settings - Fork 0
Runspace #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Runspace #9
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
6e739df
Runspace
Real-MullaC e003292
Merge branch 'main' of https://github.com/Real-MullaC/winutil-micro
Real-MullaC 7617233
Remove rubbish
Real-MullaC 1a5f3fd
Delete functions/microwin/Set-WimFilesWritable.ps1
CodingWonders 54c9d4e
Update functions/microwin/Get-ProcessesUsingPath.ps1
Real-MullaC 73db0d0
Update functions/microwin/Force-CleanupMountDirectory.ps1
Real-MullaC 6e66d63
Update
Real-MullaC 46fdd82
Update
Real-MullaC 0366f3e
Update
Real-MullaC 5f5815c
Update
Real-MullaC 6785b2a
Update Copy-Files.ps1
Real-MullaC File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| function Force-CleanupMountDirectory { | ||
| <# | ||
| .SYNOPSIS | ||
| Forces cleanup of a mount directory by closing processes that have files open | ||
|
|
||
| .DESCRIPTION | ||
| This function attempts to clean up a mount directory by unloading registry hives, | ||
| releasing file handles, and removing readonly attributes. | ||
|
|
||
| .PARAMETER MountPath | ||
| The path to the mount directory to clean up | ||
|
|
||
| .PARAMETER TimeoutSeconds | ||
| Maximum time to wait for processes to close (default: 30 seconds) | ||
| #> | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$MountPath, | ||
|
|
||
| [int]$TimeoutSeconds = 30 | ||
| ) | ||
|
|
||
| try { | ||
| # Attempt to unload any registry hives that might still be loaded | ||
| $hiveNames = @("HKLM\zCOMPONENTS", "HKLM\zDEFAULT", "HKLM\zNTUSER", "HKLM\zSOFTWARE", "HKLM\zSYSTEM") | ||
| foreach ($hiveName in $hiveNames) { | ||
| try { | ||
| $null = reg query $hiveName 2>$null | ||
| if ($LASTEXITCODE -eq 0) { | ||
| # Registry hive is loaded, try to unload it with retries | ||
| while ($true) { | ||
| reg unload $hiveName 2>$null | ||
| if ($LASTEXITCODE -eq 0) { | ||
| break | ||
| } | ||
| Start-Sleep -Milliseconds 100 | ||
| } | ||
| } | ||
| } catch { | ||
| # Hive not loaded or error checking - continue | ||
| } | ||
| } | ||
|
|
||
| # Force garbage collection to release any PowerShell file handles | ||
| Invoke-GarbageCollection -WaitSeconds 2 | ||
|
|
||
| # Try to set the mount directory and its contents to not readonly | ||
| try { | ||
| if (Test-Path "$MountPath") { | ||
| & attrib -R "$MountPath\*" /S /D 2>$null | ||
| } | ||
| } catch { | ||
| # Ignore attrib errors | ||
| } | ||
|
|
||
| # Restart Windows Search service if it's running (helps release file handles) | ||
| try { | ||
| $searchService = Get-Service -Name "WSearch" -ErrorAction SilentlyContinue | ||
| if ($searchService -and $searchService.Status -eq "Running") { | ||
| Stop-Service -Name "WSearch" -Force -ErrorAction SilentlyContinue | ||
| Start-Sleep -Seconds 1 | ||
| Start-Service -Name "WSearch" -ErrorAction SilentlyContinue | ||
| } | ||
| } catch { | ||
| # Ignore service restart errors | ||
| } | ||
|
|
||
| # Final cleanup | ||
| Invoke-GarbageCollection | ||
|
|
||
| return $true | ||
|
|
||
| } catch { | ||
| return $false | ||
| } | ||
| } |
Real-MullaC marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| function Get-ProcessesUsingPath { | ||
| <# | ||
| .SYNOPSIS | ||
| Identifies processes that may be using files in a specific path | ||
|
|
||
| .DESCRIPTION | ||
| This function attempts to identify processes that have files open in the specified path, | ||
| which can help diagnose unmount issues. | ||
|
|
||
| .PARAMETER Path | ||
| The path to check for process usage | ||
|
|
||
| .EXAMPLE | ||
| Get-ProcessesUsingPath -Path "F:\Scratch" | ||
| #> | ||
| param( | ||
| [Parameter(Mandatory = $true)] | ||
| [string]$Path | ||
| ) | ||
|
|
||
| Write-Host "Checking for processes using path: $Path" -ForegroundColor Cyan | ||
|
|
||
| $foundProcesses = @() | ||
|
|
||
| try { | ||
| # Method 1: Check process modules and loaded files | ||
| $allProcesses = Get-Process -ErrorAction SilentlyContinue | ||
| foreach ($process in $allProcesses) { | ||
| try { | ||
| if ($process.ProcessName -match "^(System|Registry|Idle)$") { | ||
| continue | ||
| } | ||
|
|
||
| # Check process modules | ||
| try { | ||
| $modules = $process.Modules | ||
| } catch { | ||
| $modules = $null | ||
| } | ||
| if ($modules) { | ||
| foreach ($module in $modules) { | ||
| if ($module.FileName -and $module.FileName.StartsWith($Path, [System.StringComparison]::OrdinalIgnoreCase)) { | ||
| $foundProcesses += @{ | ||
| ProcessName = $process.ProcessName | ||
| PID = $process.Id | ||
| File = $module.FileName | ||
| Method = "Module" | ||
| } | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| # Check working directory | ||
| try { | ||
| $startInfo = $process.StartInfo | ||
| if ($startInfo -and $startInfo.WorkingDirectory -and $startInfo.WorkingDirectory.StartsWith($Path, [System.StringComparison]::OrdinalIgnoreCase)) { | ||
| $foundProcesses += @{ | ||
| ProcessName = $process.ProcessName | ||
| PID = $process.Id | ||
| File = $startInfo.WorkingDirectory | ||
| Method = "WorkingDirectory" | ||
| } | ||
| } | ||
| } catch { | ||
| # Ignore access denied | ||
| } | ||
|
|
||
| } catch { | ||
| # Ignore processes we can't access | ||
| continue | ||
| } | ||
| } | ||
|
|
||
| # Method 2: Check common interfering processes | ||
| $suspiciousProcesses = Get-Process -ErrorAction SilentlyContinue | Where-Object { | ||
| $_.ProcessName -match "SearchIndexer|SearchProtocolHost|SearchFilterHost|MsMpEng|NisSrv|avp|avgnt|avast|mcshield|explorer" | ||
| } | ||
|
|
||
| if ($suspiciousProcesses) { | ||
| Write-Host "`nPotentially interfering processes (may not be directly using the path):" -ForegroundColor Yellow | ||
| foreach ($proc in $suspiciousProcesses) { | ||
| Write-Host " - $($proc.ProcessName) (PID: $($proc.Id))" -ForegroundColor Yellow | ||
| } | ||
| } | ||
|
|
||
| # Display results | ||
| if ($foundProcesses.Count -gt 0) { | ||
| Write-Host "`nProcesses found using path:" -ForegroundColor Red | ||
| foreach ($proc in $foundProcesses) { | ||
| Write-Host " - $($proc.ProcessName) (PID: $($proc.PID)) - $($proc.File) [$($proc.Method)]" -ForegroundColor Red | ||
| } | ||
| } else { | ||
| Write-Host "No processes found directly using the specified path." -ForegroundColor Green | ||
| } | ||
|
|
||
| return $foundProcesses | ||
|
|
||
| } catch { | ||
| Write-Host "Error checking processes: $($_.Exception.Message)" -ForegroundColor Red | ||
| return @() | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.