Private/Set-UriPrefix.ps1

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
51
52
53
54
function Set-UriPrefix {
    <#
    .SYNOPSIS
        Prefixes a URI with a specific domain if one is not already present.
 
    .DESCRIPTION
        Some web pages store URIs as relative paths, this function will prefix such URLs with a chosen domain.
 
    .NOTES
        Site: https://packageology.com
        Author: Dan Gough
        Twitter: @packageologist
 
    .LINK
        https://github.com/DanGough/Nevergreen
 
    .PARAMETER Uri
        The URI to process.
 
    .PARAMETER Prefix
        The prefix to add to the URI.
 
    .EXAMPLE
        Set-UriPrefix -Uri '/somefile.exe' -Prefix 'http://somewhere.com'
 
        Description:
        Returns 'http://somewhere.com/somefile.exe'
    #>

    [CmdletBinding(SupportsShouldProcess = $False)]
    param (
        [Parameter(
            Mandatory = $true,
            Position = 0,
            ValueFromPipeline,
            ValueFromPipelineByPropertyName)]
        [ValidateNotNullOrEmpty()]
        [Alias('Url')]
        [System.String] $Uri,
        [Parameter(
            Mandatory = $true,
            Position = 1)]
        [ValidatePattern('^(http|https)://')]
        [System.String] $Prefix
    )

    if ($Uri -like '^http') {
        # Nothing to do, return input
        $Uri
    }
    else {
        # Output joined strings
        $Prefix.TrimEnd('/') + "/" + $Uri.TrimStart('/')
    }
}