Public/Import-OriAzBopPsd.ps1
<#
.SYNOPSIS Install PowerShell module from online gallery based on psd1 file .DESCRIPTION Install PowerShell module from online gallery based on psd1 file .PARAMETER DevOpsAccount The name of the dev ops account. Default value is 'oriflame' .PARAMETER RegisterPsRepoFeedList Powershell Repository feed to register if needed .PARAMETER RegisterNugetRepoFeedList Nuget Repository feed to register if needed .PARAMETER Repository Repository feed to register if needed for getting powershell modules. .PARAMETER Path Path to psd1 file .PARAMETER Credential Repository Credential if needed .PARAMETER SkipImport When is set Import-Module will be skipped .EXAMPLE $password = ConvertTo-SecureString 'xbchuuuuhaaaatest' -AsPlainText -Force $RepositoryCredential = New-Object System.Management.Automation.PSCredential 'PackageManage@oriflame.net',$password Import-OriAzBopPsd ` -Name $(Build.SourcesDirectory)\src\$(Build.Repository.Name).psd1 ` -Credential $RepositoryCredential #> function Import-OriAzBopPsd { [CmdLetBinding()] param ( [Parameter(Mandatory = $false, HelpMessage = "The name of the dev ops account")] [String] $DevOpsAccount = $Script:VstsAccount, [Parameter(Mandatory = $false, HelpMessage = "Powershell Repository feed to register if needed")] [String[]] $RegisterPsRepoFeedList = @('PackageManagementFeed'), [Parameter(Mandatory = $false, HelpMessage = "Nuget Repository feed to register if needed")] [String[]] $RegisterNugetRepoFeedList = @('DeploymentPackages'), [Parameter(Mandatory = $false, HelpMessage = "Repository feed to register if needed for getting powershell modules")] [String] $Repository = 'PackageManagementFeed', [Parameter(Mandatory = $true, HelpMessage = "Path to psd1 file")] [string] $Path, [Parameter(Mandatory=$false, HelpMessage = "Repository Credential if needed")] [PSCredential] $Credential = $null, [Parameter(Mandatory = $false, HelpMessage = "When is set import-module will be skipped.")] [switch] $SkipImport ) $ErrorActionPreference = 'Stop' Write-Debug "-- Import-OriAzBopPsd --" Write-Debug "Name: $Name" Write-Debug "Path: $Path" # Fix credential provider Invoke-WebRequest -Uri "https://raw.githubusercontent.com/microsoft/artifacts-credprovider/master/helpers/installcredprovider.ps1" -UseBasicParsing | Invoke-Expression |Out-Null Install-OriAzBopPrerequisity -Name PowerShellGet -MinimumVersion 2.2.3 -SkipImport:$SkipImport Install-OriAzBopPrerequisity -Name PackageManagement -MinimumVersion 1.4.6 -SkipImport:$SkipImport # Required module needs to be installed and imported Register-OriAzBopRepository ` -DevOpsAccount $DevOpsAccount ` -RepositoryCredential $Credential ` -PsProjectName $RegisterPsRepoFeedList ` -NugetProjectName $RegisterNugetRepoFeedList ` -SkipPrompt:$true ` -Verbose:$VerbosePreference ` -Debug:$DebugPreference Invoke-ResolveDependency ` -InstalledLocation $Path ` -Credential $Credential ` -Repository $Repository ` -Verbose:$VerbosePreference ` -Debug:$DebugPreference Write-Verbose "Re-Import Module Path: $Path" # Note: Following import does NOT work. # Import-Module -Name $Name -RequiredVersion $RequiredVersion -Verbose # Any using of -RequiredVersion skip execution of init.ps1 # This problem we're bpassing via using Import-Module on installed path of module. if($SkipImport.IsPresent) { Write-Debug "Skip of Import-Module" } else { Import-Module $Path ` -Verbose:$VerbosePreference ` -Debug:$DebugPreference ` -Force } Write-Debug "-- End of Import-OriAzBopPsd --" } |