urlshrtPS.psm1

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
function Add-ShortUrl {
    [CmdletBinding()]
        param (
            [Parameter(Mandatory,Position=0)]
            [ValidateScript({[Uri]::CheckHostName($_) -eq 'Dns'})]
            $Zone,
            [Parameter(Mandatory,Position=1)]
            $Name,
            [Parameter(Mandatory,Position=2)]
            [ValidateScript({[Uri]::IsWellFormedUriString($_,[UriKind]::Absolute)})]
            $Url,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [ValidateRange(10000,99999)]
            [int]$Account,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [System.Security.SecureString]$AccessToken
            )
    
        $record = Get-ZoneRecord -Zone $Zone -Name $Name -Account $Account -AccessToken $AccessToken
        if ($record) {
            Write-Error "Record with name $Name and type URL already exists in zone $Zone"
            return
        }
        Add-ZoneRecord -Zone $Zone -RecordType URL -Name $Name -Content $Url -Account $Account -AccessToken $AccessToken
}

function Remove-ShortUrl{
    [CmdletBinding()]
        param (
            [Parameter(Mandatory,Position=0)]
            [ValidateScript({[Uri]::CheckHostName($_) -eq 'Dns'})]
            $Zone,
            [Parameter(Mandatory,Position=1)]
            $Name,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [ValidateRange(10000,99999)]
            [int]$Account,
            [Parameter(Mandatory,ValueFromPipelineByPropertyName)]
            [System.Security.SecureString]$AccessToken
            )
    
        $record = Get-ZoneRecord -Zone $Zone -Name $Name -Account $Account -AccessToken $AccessToken
        if (-not($record)) {
            Write-Output "Record with name $Name and type URL does not exist in zone $Zone"
            return
        }
        Remove-ZoneRecord -Zone $Zone -Id $record.id -Account $Account -AccessToken $AccessToken
}