Skip to content

Commit 72a8b14

Browse files
Add Import-PSModule function to import build PS modules
1 parent 95b8b04 commit 72a8b14

File tree

1 file changed

+47
-0
lines changed

1 file changed

+47
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
function Import-PSModule {
2+
<#
3+
.SYNOPSIS
4+
Imports a build PS module.
5+
6+
.DESCRIPTION
7+
Imports a build PS module.
8+
9+
.EXAMPLE
10+
Import-PSModule -SourceFolderPath $ModuleFolderPath -ModuleName $moduleName
11+
12+
Imports a module located at $ModuleFolderPath with the name $moduleName.
13+
#>
14+
[CmdletBinding()]
15+
[Diagnostics.CodeAnalysis.SuppressMessageAttribute(
16+
'PSAvoidUsingWriteHost', '', Scope = 'Function',
17+
Justification = 'Want to just write to the console, not the pipeline.'
18+
)]
19+
param(
20+
# Path to the folder where the module source code is located.
21+
[Parameter(Mandatory)]
22+
[string] $Path
23+
)
24+
25+
$moduleName = Split-Path -Path $Path -Leaf
26+
$manifestFilePath = Join-Path -Path $Path "$moduleName.psd1"
27+
28+
Write-Host " - Manifest file path: [$manifestFilePath]"
29+
Resolve-PSModuleDependency -ManifestFilePath $manifestFilePath
30+
31+
Write-Host ' - List installed modules'
32+
Get-InstalledPSResource | Format-Table -AutoSize | Out-String
33+
34+
Write-Host " - Importing module [$moduleName] v999"
35+
Import-Module $Path
36+
37+
Write-Host ' - List loaded modules'
38+
$availableModules = Get-Module -ListAvailable -Refresh -Verbose:$false
39+
$availableModules | Select-Object Name, Version, Path | Sort-Object Name | Format-Table -AutoSize | Out-String
40+
Write-Host ' - List commands'
41+
$commands = Get-Command -Module $moduleName -ListImported
42+
Get-Command -Module $moduleName -ListImported | Format-Table -AutoSize | Out-String
43+
44+
if ($moduleName -notin $commands.Source) {
45+
throw 'Module not found'
46+
}
47+
}

0 commit comments

Comments
 (0)