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