Skip to content

Commit

Permalink
♻️ refactor: cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jacquindev committed Jan 6, 2025
1 parent 47ef43f commit d0cbda9
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 167 deletions.
1 change: 0 additions & 1 deletion appList.json
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@
"commitizen",
"cz-git",
"degit",
"git-open",
"newman"
],
"install": true
Expand Down
75 changes: 0 additions & 75 deletions dotposh/Modules/Assets/readme-template.md

This file was deleted.

9 changes: 5 additions & 4 deletions dotposh/Modules/Get-EnvPath.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ function Add-EnvPath {
}

function Remove-EnvPath {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
[string] $Path,
Expand Down Expand Up @@ -69,6 +70,7 @@ function Remove-EnvPath {
}

function Get-EnvPath {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[ValidateSet('Machine', 'User')]
Expand All @@ -77,15 +79,14 @@ function Get-EnvPath {

if ($PSBoundParameters.Count -eq 0) {
return $Env:PATH -Split ";"
}
else {
} else {
$containerMapping = @{
Machine = [EnvironmentVariableTarget]::Machine
User = [EnvironmentVariableTarget]::User
}
$containerType = $containerMapping[$Container]

[Environment]::GetEnvironmentVariable("Path", $containerType) -split ";" |
Where-Object { $_ }
}
}
}
46 changes: 22 additions & 24 deletions dotposh/Modules/Get-Netstats.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ function Write-AnimatedProgress {
<#
.SYNOPSIS
Show animated animations while waiting for progress done.
.NOTES
References:
- https://github.com/Jaykul/Spinner/blob/master/Spinner.ps1
- https://github.com/DBremen/Write-TerminalProgress/blob/main/Write-TerminalProgress.ps1
- https://github.com/sindresorhus/cli-spinners/blob/07c83e7b9d8a08080d71ac8bda2115c83501d9d6/spinners.json
.LINK
https://github.com/Jaykul/Spinner/blob/master/Spinner.ps1
https://github.com/DBremen/Write-TerminalProgress/blob/main/Write-TerminalProgress.ps1
https://github.com/sindresorhus/cli-spinners/blob/07c83e7b9d8a08080d71ac8bda2115c83501d9d6/spinners.json
#>
[CmdletBinding()]
param (
Expand Down Expand Up @@ -73,9 +72,9 @@ function Get-Netstats {
or
> Get-Netstats -RemotePort 443
.NOTES
This function was originally from Mike Pruett, please check the file below for more details:
This function was originally from Mike Pruett, please check the file below for more details:
- https://github.com/mikepruett3/dotposh/blob/master/functions/Get-Netstat.ps1
I rewrite this with the purpose of simplifying the code and adding extra waiting animations.
#>
[CmdletBinding()]
Expand All @@ -86,50 +85,50 @@ function Get-Netstats {
[string] $LocalPort,
[string] $RemotePort
)

$Results = @()

if ($PSBoundParameters.Count -eq 0) {
Write-AnimatedProgress -Label "Collecting TCP & UDP connections ..."
$TCPConnections = Get-NetTCPConnection -ErrorAction SilentlyContinue
$UDPConnections = Get-NetUDPEndpoint -ErrorAction SilentlyContinue
$Sort = "LocalAddress"
}

if ($Listen) {
Write-AnimatedProgress -Label "Collecting TCP Connections of Listening State ..."
$TCPConnections = Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue
$Sort = "LocalAddress"
}

if ($Process) {
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Process Name - $Process ..."
$ProcessId = (Get-Process -Name $Process -ErrorAction SilentlyContinue).Id
$TCPConnections = Get-NetTCPConnection -OwningProcess $ProcessId -ErrorAction SilentlyContinue
$UDPConnections = Get-NetUDPEndpoint -OwningProcess $ProcessId -ErrorAction SilentlyContinue
$Sort = "LocalAddress"
}

if ($ID) {
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Process ID - $ID ..."
$TCPConnections = Get-NetTCPConnection -OwningProcess $ID -ErrorAction SilentlyContinue
$UDPConnections = Get-NetUDPEndpoint -OwningProcess $ID -ErrorAction SilentlyContinue
$Sort = "LocalAddress"
}

if ($LocalPort) {
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Local Port - $LocalPort ..."
$TCPConnections = Get-NetTCPConnection -LocalPort $LocalPort -ErrorAction SilentlyContinue
$UDPConnections = Get-NetUDPEndpoint -LocalPort $LocalPort -ErrorAction SilentlyContinue
$Sort = "ProcessName"
}

if ($RemotePort) {
Write-AnimatedProgress -Label "Collecting TCP Connections of Remote Port - $RemotePort ..."
$TCPConnections = Get-NetTCPConnection -RemotePort $RemotePort -ErrorAction SilentlyContinue
$Sort = "ProcessName"
}

foreach ($Connection in $TCPConnections) {
$Result = [PSCustomObject]@{
CreationTime = $Connection.CreationTime
Expand All @@ -142,17 +141,16 @@ function Get-Netstats {
RemotePort = $Connection.RemotePort
State = $Connection.State
}

if (Resolve-DNSName -Name $Connection.RemoteAddress -DnsOnly -ErrorAction SilentlyContinue) {
$Result | Add-Member -MemberType NoteProperty -Name "RemoteAddress" -Value (Resolve-DNSName -Name $Connection.RemoteAddress -DnsOnly).NameHost
} else {
$Result | Add-Member -MemberType NoteProperty -Name 'RemoteAddress' -Value $Connection.RemoteAddress
}
else {
$Result | Add-Member -MemberType NoteProperty -Name 'RemoteAddress' -Value $Connection.RemoteAddress
}


$Results += $Result
}

foreach ($Connection in $UDPConnections) {
$Result = [PSCustomObject]@{
CreationTime = $Connection.CreationTime
Expand All @@ -168,11 +166,11 @@ function Get-Netstats {
}
$Results += $Result
}

$Results |
Select-Object Protocol, LocalAddress, LocalPort, RemoteAddress, RemotePort, ProcessName, ID |
Sort-Object -Property @{Expression = "Protocol" }, @{Expression = $Sort } |
Format-Table -AutoSize -Wrap
}
}

Set-Alias -Name 'netstats' -Value 'Get-Netstats'
Set-Alias -Name 'netstats' -Value 'Get-Netstats'
108 changes: 45 additions & 63 deletions dotposh/Modules/Update-Modules.ps1
Original file line number Diff line number Diff line change
@@ -1,82 +1,64 @@
function Test-IsElevated {
return (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
}

function Update-Modules {
<#
.SYNOPSIS
Update all installed modules
.EXAMPLE
Update-Modules -Scope CurrentUser
.PARAMETER AllowPrerelease
Updating module to latest prerelease version.
.PARAMETER WhatIf
Run function without actually executing it.
.PARAMETER Name
Name of the module you want to update.
.PARAMETER Scope
The scope apply when updating modules.
#>
[CmdletBinding()]
param (
[Alias('p')][switch]$AllowPrerelease,
[Alias('n', '-dry-run')][switch]$WhatIf,
[switch]$AllowPrerelease,

[switch]$WhatIf,

[string]$Name = "*",
[ValidateSet('AllUsers', 'CurrentUser')][string]$Scope = 'CurrentUser'

[ValidateSet('AllUsers', 'CurrentUser')]
[string]$Scope = "CurrentUser"
)

# Test elevated permissions for scope allusers
if ($Scope -eq "AllUsers") {
if ((Test-IsElevated) -eq $False) {
if ((New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) -eq $False) {
Write-Warning "Function $($MyInvocation.MyCommand) needs admin privileges to perform actions."
Break
break
}
}

# Status of allow prerelease switch
if ($AllowPrerelease) {
''
Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline
Write-Host "prerelease " -ForegroundColor "Yellow" -NoNewline
Write-Host "versions..." -ForegroundColor "Blue"
Write-Host "-------------------------------------------------" -ForegroundColor "Blue"
} else {
''
Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline
Write-Host "stable " -ForegroundColor "Yellow" -NoNewline
Write-Host "versions..." -ForegroundColor "Blue"
Write-Host "---------------------------------------------" -ForegroundColor "Blue"
}
$CurrentModules = Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Select-Object -Property Name, Version | Sort-Object Name

# Get all installed modules
if ((Test-Path "$Env:DOTFILES\modules.lock.json") -and ($Name -eq "*")) {
$CurrentModules = Get-Content "$Env:DOTFILES\modules.lock.json" | ConvertFrom-Json | Select-Object -Property Name, Version | Sort-Object Name
} else {
$CurrentModules = Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Select-Object -Property Name, Version | Sort-Object Name
}
if (!$CurrentModules) { Write-Host "No modules found." -ForegroundColor Red; return }
else {
$i = 1
$moduleCount = $CurrentModules.Count
''; Write-Host "$moduleCount " -ForegroundColor Yellow -NoNewline; Write-Host "module(s) installed." -ForegroundColor Green; ''

# If no modules installed.
if (-not $CurrentModules) {
''
Write-Host "No modules found." -ForegroundColor "Red"
Return
} else {
''
$ModulesCount = $CurrentModules.Count
Write-Host "$ModulesCount " -ForegroundColor "White" -NoNewLine
Write-Host "module(s) installed." -ForegroundColor "Green"
}
if ($AllowPrerelease) { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "prerelease " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue }
else { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "stable " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue }

foreach ($Module in $CurrentModules) {
''
Write-Host "Checking for updated version of module: " -ForegroundColor "Gray" -NoNewLine
Write-Host "$($Module.Name)" -ForegroundColor "White"
$OnlineVersion = (Find-Module -Name $Module.Name -AllVersions | Sort-Object PublishedDate -Descending)[0].Version
$CurrentVersion = (Get-InstalledModule -Name $Module.Name).Version
if ($CurrentVersion -lt $OnlineVersion) {
try {
Update-Module -Name $Module.Name -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent
$CurrentVersion = $CurrentModules | Where-Object Name -EQ $Module.Name
if ($CurrentVersion.Version -notlike $Module.Version) {
Write-Host "Updated module $($Module.Name) from version $($CurrentVersion.Version) to $($Module.Version)" -ForegroundColor "Green"
}
foreach ($Version in $CurrentVersion) {
if ($Version.Version -ne $CurrentVersion[0].Version) {
Uninstall-Module -Name $Module.Name -RequiredVersion $Version.Version -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent
}
foreach ($module in $CurrentModules) {
$OnlineVersion = (Find-Module -Name $($module.Name) -AllVersions | Sort-Object PublishedDate -Descending)[0].Version
$CurrentVersion = (Get-InstalledModule -Name $($module.Name)).Version

if ($CurrentVersion -ne $OnlineVersion) {
try {
Update-Module -Name $($module.Name) -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -WhatIf:$WhatIf.IsPresent
} catch {
Write-Error "Error occurred while updating module $($module.Name): $_"
}
} catch {
Write-Error "Error occurred while updating module $($Module.Name): $_"
}
} else {
Write-Host "Already up-to-date!" -ForegroundColor "Blue"

[int]$percentCompleted = ($i / $moduleCount) * 100
Write-Progress -Activity "Updating Module $($module.Name)" -Status "$percentCompleted% Completed - $($module.Name) v$OnlineVersion" -PercentComplete $percentCompleted
$i++
}
if ($?) { Write-Host "Everything is up-to-date!" -ForegroundColor Green }
}
''
}

0 comments on commit d0cbda9

Please sign in to comment.