Public/Install-DTXModule.ps1
function Install-DTXModule { param( [Parameter(Mandatory = $true)] [string]$Name, [Parameter(Mandatory = $true)] [string]$Version, [Parameter(Mandatory = $false)] [string]$BucketName = "dtx-software-repo", [Parameter(Mandatory = $false)] [string]$BucketRegion = "eu-west-1", [Parameter(Mandatory = $false)] [string]$ObjectKey = "powershell-modules/$Name/$Version/$Name-v$Version.zip" , [switch]$SkipTestImport ) Begin { # Process the Name parameter to make sure it is in the correct format # e.g. DTX should always be all capitals. All other names should be in PascalCase. $Name = ($Name -split '\.').ForEach({ if ($_ -ieq "dtx") { "DTX" } else { $_.Substring(0, 1).ToUpper() + $_.Substring(1).ToLower() } }) -join '.' } Process { $params = @{ Name = $Name Version = $Version.ToLower() BucketName = $BucketName.ToLower() BucketRegion = $BucketRegion.ToLower() ObjectKey = $ObjectKey.ToLower() } if ($IsWindows) { # Install the module for Windows Install-ModuleForWindows @params } elseif ($IsMacOS -or $IsLinux) { # Install the module for non-Windows Install-ModuleForNonWindows @params } else { throw "Unsupported operating system." } if (-not $SkipTestImport) { # Test if the module can be imported try { Write-Host "Testing if module $($Name) can be imported..." Import-Module -Name $Name -ErrorAction Stop Write-Host "Module $($Name) imported successfully." } catch { Write-Error $_.Exception.Message throw "Failed to import module $($Name)." } } } } |