Skip to content

Commit b65d3cc

Browse files
committed
Fixed some issues
1 parent d42e58d commit b65d3cc

1 file changed

Lines changed: 42 additions & 2 deletions

File tree

Microsoft.PowerShell_profile.ps1

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1609,6 +1609,7 @@ function Uninstall-Profile {
16091609
Write-Host ' Skipping PSFzf module uninstall under CI/agent environment.' -ForegroundColor DarkGray
16101610
}
16111611
elseif ($PSCmdlet.ShouldProcess('PSFzf', 'Uninstall module')) {
1612+
$uninstalled = $false
16121613
try {
16131614
# Try to unload the module from the current session first
16141615
Remove-Module -Name PSFzf -Force -ErrorAction SilentlyContinue
@@ -1617,8 +1618,43 @@ function Uninstall-Profile {
16171618
try {
16181619
Uninstall-Module -Name PSFzf -AllVersions -Force -ErrorAction Stop
16191620
Write-Host ' Uninstalled PSFzf module.' -ForegroundColor Green
1621+
$uninstalled = $true
1622+
}
1623+
catch {
1624+
Write-Warning " Failed to uninstall PSFzf in current session: $_"
1625+
}
1626+
1627+
if (-not $uninstalled) {
1628+
# Fallback: spawn a background shell to attempt uninstall so this session
1629+
# does not keep the module "in use".
1630+
$psExe = $null
1631+
$cmd = Get-Command pwsh -ErrorAction SilentlyContinue
1632+
if ($cmd) {
1633+
$psExe = $cmd.Source
1634+
}
1635+
else {
1636+
$cmd = Get-Command powershell -ErrorAction SilentlyContinue
1637+
if ($cmd) { $psExe = $cmd.Source }
1638+
}
1639+
1640+
if ($psExe) {
1641+
try {
1642+
Start-Process -FilePath $psExe -ArgumentList @(
1643+
'-NoProfile'
1644+
'-NonInteractive'
1645+
'-Command'
1646+
"try { Uninstall-Module -Name PSFzf -AllVersions -Force -ErrorAction SilentlyContinue } catch { `$null = `$_ }"
1647+
) -WindowStyle Hidden | Out-Null
1648+
Write-Host ' Scheduled PSFzf uninstall in background session.' -ForegroundColor Yellow
1649+
}
1650+
catch {
1651+
Write-Warning " Failed to schedule PSFzf uninstall in background session: $_"
1652+
}
1653+
}
1654+
else {
1655+
Write-Warning ' Could not locate pwsh or powershell to retry PSFzf uninstall in a background session.'
1656+
}
16201657
}
1621-
catch { Write-Warning " Failed to uninstall PSFzf: $_" }
16221658
}
16231659
}
16241660

@@ -2098,6 +2134,7 @@ function tlscert {
20982134

20992135
# Quick TCP port scan
21002136
function portscan {
2137+
[CmdletBinding()]
21012138
param(
21022139
[Parameter(Mandatory)][string]$Hostname,
21032140
[int[]]$Ports = @(21, 22, 25, 53, 80, 443, 445, 3306, 3389, 5432, 5900, 6379, 8080, 8443, 27017)
@@ -2115,7 +2152,10 @@ function portscan {
21152152
$open++
21162153
}
21172154
}
2118-
catch { Write-Verbose "Port $port closed or filtered" }
2155+
catch {
2156+
if ($_.Exception -is [System.Management.Automation.PipelineStoppedException]) { throw }
2157+
Write-Verbose "Port $port closed or filtered"
2158+
}
21192159
finally { $tcp.Dispose() }
21202160
}
21212161
if ($open -eq 0) { Write-Host " No open ports found." -ForegroundColor Yellow }

0 commit comments

Comments
 (0)