Skip to content

Commit 5fe14db

Browse files
Refactor module import and removal logic in PowerShell scripts to improve clarity and maintainability
1 parent ede26e4 commit 5fe14db

File tree

4 files changed

+39
-8
lines changed

4 files changed

+39
-8
lines changed

scripts/helpers/Build-PSModuleDocumentation.ps1

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,15 @@
5353

5454
LogGroup 'Build docs - Generate markdown help' {
5555
Add-PSModulePath -Path (Split-Path -Path $ModuleOutputFolder -Parent)
56-
$ModuleName | Remove-Module -Force -ErrorAction SilentlyContinue
5756
Import-PSModule -Path $ModuleOutputFolder -ModuleName $ModuleName
5857
Write-Host ($ModuleName | Get-Module)
5958
$null = New-MarkdownHelp -Module $ModuleName -OutputFolder $DocsOutputFolder -Force -Verbose
59+
Get-ChildItem -Path $DocsOutputFolder -Recurse -Force -Include '*.md' | ForEach-Object {
60+
$fileName = $_.Name
61+
LogGroup " - [$fileName]" {
62+
Show-FileContent -Path $_
63+
}
64+
}
6065
}
6166

6267
LogGroup 'Build docs - Fix markdown code blocks' {

scripts/helpers/Import-PSModule.ps1

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,19 +33,15 @@
3333
$manifestFile = Get-ModuleManifest -Path $manifestFilePath -As FileInfo -Verbose
3434

3535
Write-Host "Manifest file path: [$($manifestFile.FullName)]" -Verbose
36-
$existingModule = Get-Module -Name $ModuleName -ListAvailable
37-
$existingModule | Remove-Module -Force -Verbose
38-
$existingModule.RequiredModules | ForEach-Object { $_ | Remove-Module -Force -Verbose -ErrorAction SilentlyContinue }
39-
$existingModule.NestedModules | ForEach-Object { $_ | Remove-Module -Force -Verbose -ErrorAction SilentlyContinue }
40-
# Get-InstalledPSResource | Where-Object Name -EQ $ModuleName | Uninstall-PSResource -SkipDependencyCheck -Verbose:$false
36+
Remove-PSModule -Name $ModuleName
4137
Resolve-PSModuleDependency -ManifestFilePath $manifestFile
4238
Import-Module -Name $ModuleName -RequiredVersion '999.0.0'
4339

4440
Write-Host 'List loaded modules'
4541
$availableModules = Get-Module -ListAvailable -Refresh -Verbose:$false
4642
$availableModules | Select-Object Name, Version, Path | Sort-Object Name | Format-Table -AutoSize
4743
Write-Host 'List commands'
48-
Write-Host (Get-Command -Module $moduleName | Format-Table -AutoSize | Out-String)
44+
Write-Host (Get-Command -Module $moduleName -ListImported | Format-Table -AutoSize | Out-String)
4945

5046
if ($ModuleName -notin $availableModules.Name) {
5147
throw 'Module not found'
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
filter Remove-PSModule {
2+
<#
3+
.SYNOPSIS
4+
Removes and uninstalls a PowerShell module.
5+
6+
7+
.EXAMPLE
8+
Remove-PSModule -ModuleName 'Utilities'
9+
10+
Removes a module 'Utilities' from the session then from the system.
11+
#>
12+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
13+
'PSAvoidUsingWriteHost', '', Scope = 'Function',
14+
Justification = 'Want to just write to the console, not the pipeline.'
15+
)]
16+
[CmdletBinding(SupportsShouldProcess)]
17+
param(
18+
# Name of the module.
19+
[Parameter(Mandatory, ValueFromPipeline)]
20+
[string] $Name
21+
)
22+
23+
if ($PSCmdlet.ShouldProcess('Target', "Remove module [$Name]")) {
24+
Write-Host "Removing module [$Name]"
25+
Get-Module -Name $Name -ListAvailable | Remove-Module -Force
26+
Get-InstalledPSResource -Name $Name | Uninstall-PSResource -SkipDependencyCheck
27+
}
28+
}

scripts/helpers/Resolve-PSModuleDependency.ps1

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@
4949
$installParams.Force = $true
5050
$installParams.Verbose = $false
5151

52-
Write-Host "[$($installParams.Name)] - Installing module"
5352
$VerbosePreferenceOriginal = $VerbosePreference
5453
$VerbosePreference = 'SilentlyContinue'
54+
Write-Host "[$($installParams.Name)] - Uninstalling module"
55+
Remove-PSModule -Name $installParams.Name
56+
Write-Host "[$($installParams.Name)] - Installing module"
5557
Retry -Count 5 -Delay 10 {
5658
Install-Module @installParams -AllowPrerelease:$false
5759
}

0 commit comments

Comments
 (0)