|
| 1 | +#Requires -Modules @{ ModuleName = 'Utilities'; ModuleVersion = '0.3.0' } |
| 2 | + |
| 3 | +function Import-PSModule { |
| 4 | + <# |
| 5 | + .SYNOPSIS |
| 6 | + Imports a build PS module. |
| 7 | +
|
| 8 | + .DESCRIPTION |
| 9 | + Imports a build PS module. |
| 10 | +
|
| 11 | + .EXAMPLE |
| 12 | + Import-PSModule -SourceFolderPath $ModuleFolderPath -ModuleName $ModuleName |
| 13 | +
|
| 14 | + Imports a module located at $ModuleFolderPath with the name $ModuleName. |
| 15 | + #> |
| 16 | + [CmdletBinding()] |
| 17 | + param( |
| 18 | + # Path to the folder where the module source code is located. |
| 19 | + [Parameter(Mandatory)] |
| 20 | + [string] $Path, |
| 21 | + |
| 22 | + # Name of the module. |
| 23 | + [Parameter(Mandatory)] |
| 24 | + [string] $ModuleName |
| 25 | + ) |
| 26 | + |
| 27 | + $moduleName = Split-Path -Path $Path -Leaf |
| 28 | + $manifestFileName = "$moduleName.psd1" |
| 29 | + $manifestFilePath = Join-Path -Path $Path $manifestFileName |
| 30 | + $manifestFile = Get-ModuleManifest -Path $manifestFilePath -As FileInfo -Verbose |
| 31 | + |
| 32 | + Write-Host "Manifest file path: [$($manifestFile.FullName)]" -Verbose |
| 33 | + $existingModule = Get-Module -Name $ModuleName -ListAvailable |
| 34 | + $existingModule | Remove-Module -Force -Verbose |
| 35 | + $existingModule.RequiredModules | ForEach-Object { $_ | Remove-Module -Force -Verbose -ErrorAction SilentlyContinue } |
| 36 | + $existingModule.NestedModules | ForEach-Object { $_ | Remove-Module -Force -Verbose -ErrorAction SilentlyContinue } |
| 37 | + # Get-InstalledPSResource | Where-Object Name -EQ $ModuleName | Uninstall-PSResource -SkipDependencyCheck -Verbose:$false |
| 38 | + Resolve-PSModuleDependency -ManifestFilePath $manifestFile |
| 39 | + Import-Module -Name $ModuleName -RequiredVersion '999.0.0' |
| 40 | + |
| 41 | + Write-Host 'List loaded modules' |
| 42 | + $availableModules = Get-Module -ListAvailable -Refresh -Verbose:$false |
| 43 | + $availableModules | Select-Object Name, Version, Path | Sort-Object Name | Format-Table -AutoSize |
| 44 | + Write-Host 'List commands' |
| 45 | + Write-Host (Get-Command -Module $moduleName | Format-Table -AutoSize | Out-String) |
| 46 | + |
| 47 | + if ($ModuleName -notin $availableModules.Name) { |
| 48 | + throw 'Module not found' |
| 49 | + } |
| 50 | +} |
0 commit comments