Functions/ModuleManagement/Install-FpsRepository.ps1

<#
.SYNOPSIS
    Adds a new PowerShell Repository for the current user with a Nuget package source.
.DESCRIPTION
    1. Removes previous registered repositories and package sources with the same feed name (AzDoFeedName).
    2. Adds a new PowerShell Repository for the current user with a Nuget package source.
 
    Note: The registration of the Azure DevOps feed does not require access to the feed yet. However using the feed does.
.EXAMPLE
    Install-FpsRepository
.EXAMPLE
    Install-FpsRepository -AzDoOrganisation '4psnl' -AzDoProjectName '4PS Tools' -AzDoFeedName 'fpstools_internal'
#>


function Install-FpsRepository {
    param (
        # Azure DevOps Artifact Feed name
        $AzDoFeedName     = 'fpstools_internal',    
        
        # Azure DevOps organization name
        $AzDoOrganisation = '4psnl',
        
        # Azure DevOps project name
        $AzDoProjectName  = '4PS Tools',

        # Feed credential
        [PSCredential] $Credential,
        
        # Base URL to access Azure DevOps feed
        $AzDoFeedBaseUrl  = 'pkgs.dev.azure.com',

        # Nuget version to used to access the feed
        $NugetVersion     = 'v2'
    )

    # Convert organization and project name to URL friendly string
    if($AzDoOrganisation -match ' '){$AzDoOrganisation = [uri]::EscapeDataString($AzDoOrganisation)}
    if($AzDoProjectName -match ' '){$AzDoProjectName = [uri]::EscapeDataString($AzDoProjectName)}

    # Script to register 4PS PowerShell feed as trusted PSRepository for local user.
    $fpsPsRepoUrl = ('https://{0}/{1}/{2}/_packaging/{3}/nuget/{4}' -f 
                            $AzDoFeedBaseUrl, $AzDoOrganisation, $AzDoProjectName, $AzDoFeedName, $NugetVersion)

    'Used URL for registration: ''{0}''.' -f $fpsPsRepoUrl | Write-Host

    # Remove PSRepository feed if already exist
    if((Get-PSRepository -Name $AzDoFeedName -ErrorAction SilentlyContinue).Name -eq $AzDoFeedName){
        'Registration for PSRepository ''{0}'' found; Removing PSRepository.' -f $AzDoFeedName | Write-Host
        Unregister-PSRepository $AzDoFeedName
    }

    # Remove PackageSource feed if already exist
    $packageSource = Get-PackageSource -Name $AzDoFeedName -ErrorAction SilentlyContinue
    $packageSource | ForEach-Object {
        if($_.Name -eq $AzDoFeedName){
            'Registration for PackageSource ''{0}'' with ProviderName ''{1}'' found; Removing PackageSource.' -f 
                $_.Name, $_.ProviderName | Write-Host
            Unregister-PackageSource -Name $AzDoFeedName
        }
    }

    # Register feed as PSRepository, required to browse the feed with Find-Module
    'Registering PSRepository ''{0}''.' -f $AzDoFeedName | Write-Host
    $repoRegisterParams = @{
        Name                      = $AzDoFeedName
        SourceLocation            = $fpsPsRepoUrl
        PublishLocation           = ('{0}/Packages' -f $fpsPsRepoUrl)
        InstallationPolicy        = 'Trusted'
        PackageManagementProvider = 'Nuget'
        Verbose                   = $false 
    }
    if($Credential){
        $repoRegisterParams += @{
            Credential = $Credential
        }
    }
    Register-PSRepository @repoRegisterParams

    # Register NuGet as packagesource for the feed, required for Install-Module
    'Registering PackageSource ''{0}''.' -f $AzDoFeedName | Write-Host
    $sourceRegisterParams = @{
        Name         = $AzDoFeedName
        Location     = $fpsPsRepoUrl
        ProviderName = 'Nuget'
        Trusted      = $true
        SkipValidate = $true
        Verbose      = $false 
    }
    Register-PackageSource @sourceRegisterParams | Out-Null

    'Registration completed.' | Write-Host
}

Export-ModuleMember -Function Install-FpsRepository