Skip to content

Commit d0cbda9

Browse files
committed
♻️ refactor: cleanup
1 parent 47ef43f commit d0cbda9

File tree

5 files changed

+72
-167
lines changed

5 files changed

+72
-167
lines changed

appList.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,6 @@
125125
"commitizen",
126126
"cz-git",
127127
"degit",
128-
"git-open",
129128
"newman"
130129
],
131130
"install": true

dotposh/Modules/Assets/readme-template.md

Lines changed: 0 additions & 75 deletions
This file was deleted.

dotposh/Modules/Get-EnvPath.ps1

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function Add-EnvPath {
3939
}
4040

4141
function Remove-EnvPath {
42+
[CmdletBinding()]
4243
param(
4344
[Parameter(Mandatory = $true, ValueFromPipelineByPropertyName = $true)]
4445
[string] $Path,
@@ -69,6 +70,7 @@ function Remove-EnvPath {
6970
}
7071

7172
function Get-EnvPath {
73+
[CmdletBinding()]
7274
param(
7375
[Parameter(Mandatory = $false)]
7476
[ValidateSet('Machine', 'User')]
@@ -77,15 +79,14 @@ function Get-EnvPath {
7779

7880
if ($PSBoundParameters.Count -eq 0) {
7981
return $Env:PATH -Split ";"
80-
}
81-
else {
82+
} else {
8283
$containerMapping = @{
8384
Machine = [EnvironmentVariableTarget]::Machine
8485
User = [EnvironmentVariableTarget]::User
8586
}
8687
$containerType = $containerMapping[$Container]
87-
88+
8889
[Environment]::GetEnvironmentVariable("Path", $containerType) -split ";" |
8990
Where-Object { $_ }
9091
}
91-
}
92+
}

dotposh/Modules/Get-Netstats.ps1

Lines changed: 22 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@ function Write-AnimatedProgress {
22
<#
33
.SYNOPSIS
44
Show animated animations while waiting for progress done.
5-
.NOTES
6-
References:
7-
- https://github.com/Jaykul/Spinner/blob/master/Spinner.ps1
8-
- https://github.com/DBremen/Write-TerminalProgress/blob/main/Write-TerminalProgress.ps1
9-
- https://github.com/sindresorhus/cli-spinners/blob/07c83e7b9d8a08080d71ac8bda2115c83501d9d6/spinners.json
5+
.LINK
6+
https://github.com/Jaykul/Spinner/blob/master/Spinner.ps1
7+
https://github.com/DBremen/Write-TerminalProgress/blob/main/Write-TerminalProgress.ps1
8+
https://github.com/sindresorhus/cli-spinners/blob/07c83e7b9d8a08080d71ac8bda2115c83501d9d6/spinners.json
109
#>
1110
[CmdletBinding()]
1211
param (
@@ -73,9 +72,9 @@ function Get-Netstats {
7372
or
7473
> Get-Netstats -RemotePort 443
7574
.NOTES
76-
This function was originally from Mike Pruett, please check the file below for more details:
75+
This function was originally from Mike Pruett, please check the file below for more details:
7776
- https://github.com/mikepruett3/dotposh/blob/master/functions/Get-Netstat.ps1
78-
77+
7978
I rewrite this with the purpose of simplifying the code and adding extra waiting animations.
8079
#>
8180
[CmdletBinding()]
@@ -86,50 +85,50 @@ function Get-Netstats {
8685
[string] $LocalPort,
8786
[string] $RemotePort
8887
)
89-
88+
9089
$Results = @()
91-
90+
9291
if ($PSBoundParameters.Count -eq 0) {
9392
Write-AnimatedProgress -Label "Collecting TCP & UDP connections ..."
9493
$TCPConnections = Get-NetTCPConnection -ErrorAction SilentlyContinue
9594
$UDPConnections = Get-NetUDPEndpoint -ErrorAction SilentlyContinue
9695
$Sort = "LocalAddress"
9796
}
98-
97+
9998
if ($Listen) {
10099
Write-AnimatedProgress -Label "Collecting TCP Connections of Listening State ..."
101100
$TCPConnections = Get-NetTCPConnection -State Listen -ErrorAction SilentlyContinue
102101
$Sort = "LocalAddress"
103102
}
104-
103+
105104
if ($Process) {
106105
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Process Name - $Process ..."
107106
$ProcessId = (Get-Process -Name $Process -ErrorAction SilentlyContinue).Id
108107
$TCPConnections = Get-NetTCPConnection -OwningProcess $ProcessId -ErrorAction SilentlyContinue
109108
$UDPConnections = Get-NetUDPEndpoint -OwningProcess $ProcessId -ErrorAction SilentlyContinue
110109
$Sort = "LocalAddress"
111110
}
112-
111+
113112
if ($ID) {
114113
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Process ID - $ID ..."
115114
$TCPConnections = Get-NetTCPConnection -OwningProcess $ID -ErrorAction SilentlyContinue
116115
$UDPConnections = Get-NetUDPEndpoint -OwningProcess $ID -ErrorAction SilentlyContinue
117116
$Sort = "LocalAddress"
118117
}
119-
118+
120119
if ($LocalPort) {
121120
Write-AnimatedProgress -Label "Collecting TCP & UDP Connections of Local Port - $LocalPort ..."
122121
$TCPConnections = Get-NetTCPConnection -LocalPort $LocalPort -ErrorAction SilentlyContinue
123122
$UDPConnections = Get-NetUDPEndpoint -LocalPort $LocalPort -ErrorAction SilentlyContinue
124123
$Sort = "ProcessName"
125124
}
126-
125+
127126
if ($RemotePort) {
128127
Write-AnimatedProgress -Label "Collecting TCP Connections of Remote Port - $RemotePort ..."
129128
$TCPConnections = Get-NetTCPConnection -RemotePort $RemotePort -ErrorAction SilentlyContinue
130129
$Sort = "ProcessName"
131130
}
132-
131+
133132
foreach ($Connection in $TCPConnections) {
134133
$Result = [PSCustomObject]@{
135134
CreationTime = $Connection.CreationTime
@@ -142,17 +141,16 @@ function Get-Netstats {
142141
RemotePort = $Connection.RemotePort
143142
State = $Connection.State
144143
}
145-
144+
146145
if (Resolve-DNSName -Name $Connection.RemoteAddress -DnsOnly -ErrorAction SilentlyContinue) {
147146
$Result | Add-Member -MemberType NoteProperty -Name "RemoteAddress" -Value (Resolve-DNSName -Name $Connection.RemoteAddress -DnsOnly).NameHost
147+
} else {
148+
$Result | Add-Member -MemberType NoteProperty -Name 'RemoteAddress' -Value $Connection.RemoteAddress
148149
}
149-
else {
150-
$Result | Add-Member -MemberType NoteProperty -Name 'RemoteAddress' -Value $Connection.RemoteAddress
151-
}
152-
150+
153151
$Results += $Result
154152
}
155-
153+
156154
foreach ($Connection in $UDPConnections) {
157155
$Result = [PSCustomObject]@{
158156
CreationTime = $Connection.CreationTime
@@ -168,11 +166,11 @@ function Get-Netstats {
168166
}
169167
$Results += $Result
170168
}
171-
169+
172170
$Results |
173171
Select-Object Protocol, LocalAddress, LocalPort, RemoteAddress, RemotePort, ProcessName, ID |
174172
Sort-Object -Property @{Expression = "Protocol" }, @{Expression = $Sort } |
175173
Format-Table -AutoSize -Wrap
176-
}
174+
}
177175

178-
Set-Alias -Name 'netstats' -Value 'Get-Netstats'
176+
Set-Alias -Name 'netstats' -Value 'Get-Netstats'

dotposh/Modules/Update-Modules.ps1

Lines changed: 45 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,64 @@
1-
function Test-IsElevated {
2-
return (New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)
3-
}
4-
51
function Update-Modules {
2+
<#
3+
.SYNOPSIS
4+
Update all installed modules
5+
.EXAMPLE
6+
Update-Modules -Scope CurrentUser
7+
.PARAMETER AllowPrerelease
8+
Updating module to latest prerelease version.
9+
.PARAMETER WhatIf
10+
Run function without actually executing it.
11+
.PARAMETER Name
12+
Name of the module you want to update.
13+
.PARAMETER Scope
14+
The scope apply when updating modules.
15+
#>
616
[CmdletBinding()]
717
param (
8-
[Alias('p')][switch]$AllowPrerelease,
9-
[Alias('n', '-dry-run')][switch]$WhatIf,
18+
[switch]$AllowPrerelease,
19+
20+
[switch]$WhatIf,
21+
1022
[string]$Name = "*",
11-
[ValidateSet('AllUsers', 'CurrentUser')][string]$Scope = 'CurrentUser'
23+
24+
[ValidateSet('AllUsers', 'CurrentUser')]
25+
[string]$Scope = "CurrentUser"
1226
)
1327

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

22-
# Status of allow prerelease switch
23-
if ($AllowPrerelease) {
24-
''
25-
Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline
26-
Write-Host "prerelease " -ForegroundColor "Yellow" -NoNewline
27-
Write-Host "versions..." -ForegroundColor "Blue"
28-
Write-Host "-------------------------------------------------" -ForegroundColor "Blue"
29-
} else {
30-
''
31-
Write-Host "Updating modules to latest " -ForegroundColor "Blue" -NoNewline
32-
Write-Host "stable " -ForegroundColor "Yellow" -NoNewline
33-
Write-Host "versions..." -ForegroundColor "Blue"
34-
Write-Host "---------------------------------------------" -ForegroundColor "Blue"
35-
}
35+
$CurrentModules = Get-InstalledModule -Name $Name -ErrorAction SilentlyContinue | Select-Object -Property Name, Version | Sort-Object Name
3636

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

44-
# If no modules installed.
45-
if (-not $CurrentModules) {
46-
''
47-
Write-Host "No modules found." -ForegroundColor "Red"
48-
Return
49-
} else {
50-
''
51-
$ModulesCount = $CurrentModules.Count
52-
Write-Host "$ModulesCount " -ForegroundColor "White" -NoNewLine
53-
Write-Host "module(s) installed." -ForegroundColor "Green"
54-
}
43+
if ($AllowPrerelease) { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "prerelease " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue }
44+
else { Write-Host "Trying to update modules to latest " -ForegroundColor Blue -NoNewline; Write-Host "stable " -NoNewline -ForegroundColor Magenta; Write-Host "version" -ForegroundColor Blue }
5545

56-
foreach ($Module in $CurrentModules) {
57-
''
58-
Write-Host "Checking for updated version of module: " -ForegroundColor "Gray" -NoNewLine
59-
Write-Host "$($Module.Name)" -ForegroundColor "White"
60-
$OnlineVersion = (Find-Module -Name $Module.Name -AllVersions | Sort-Object PublishedDate -Descending)[0].Version
61-
$CurrentVersion = (Get-InstalledModule -Name $Module.Name).Version
62-
if ($CurrentVersion -lt $OnlineVersion) {
63-
try {
64-
Update-Module -Name $Module.Name -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent
65-
$CurrentVersion = $CurrentModules | Where-Object Name -EQ $Module.Name
66-
if ($CurrentVersion.Version -notlike $Module.Version) {
67-
Write-Host "Updated module $($Module.Name) from version $($CurrentVersion.Version) to $($Module.Version)" -ForegroundColor "Green"
68-
}
69-
foreach ($Version in $CurrentVersion) {
70-
if ($Version.Version -ne $CurrentVersion[0].Version) {
71-
Uninstall-Module -Name $Module.Name -RequiredVersion $Version.Version -Force:$True -ErrorAction Stop -WhatIf:$WhatIf.IsPresent
72-
}
46+
foreach ($module in $CurrentModules) {
47+
$OnlineVersion = (Find-Module -Name $($module.Name) -AllVersions | Sort-Object PublishedDate -Descending)[0].Version
48+
$CurrentVersion = (Get-InstalledModule -Name $($module.Name)).Version
49+
50+
if ($CurrentVersion -ne $OnlineVersion) {
51+
try {
52+
Update-Module -Name $($module.Name) -AllowPrerelease:$AllowPrerelease -Scope:$Scope -Force:$True -WhatIf:$WhatIf.IsPresent
53+
} catch {
54+
Write-Error "Error occurred while updating module $($module.Name): $_"
7355
}
74-
} catch {
75-
Write-Error "Error occurred while updating module $($Module.Name): $_"
7656
}
77-
} else {
78-
Write-Host "Already up-to-date!" -ForegroundColor "Blue"
57+
58+
[int]$percentCompleted = ($i / $moduleCount) * 100
59+
Write-Progress -Activity "Updating Module $($module.Name)" -Status "$percentCompleted% Completed - $($module.Name) v$OnlineVersion" -PercentComplete $percentCompleted
60+
$i++
7961
}
62+
if ($?) { Write-Host "Everything is up-to-date!" -ForegroundColor Green }
8063
}
81-
''
8264
}

0 commit comments

Comments
 (0)