Public/API/Resolve-VanityURL.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
function Resolve-VanityURL {
    <#
    .SYNOPSIS
    Resolve a vanity URL (also named custom URL).

    .DESCRIPTION
    Resolve a vanity URL (also named custom URL) and return the 64 bit SteamID
    that belongs to said URL.

    .PARAMETER VanityURL
    Enter the vanity URL (also named custom URL) to get a SteamID for. Do not enter
    the fully qualified URL, but just the ID e.g. hjorslev instead of
    "https://steamcommunity.com/id/hjorslev/"

    .PARAMETER UrlType
    The type of vanity URL. 1 (default): Individual profile, 2: Group, 3: Official game group

    .PARAMETER OutputFormat
    Format of the output. Options are json (default), xml or vdf.

    .EXAMPLE
    Resolve-VanityURL -VanityURL hjorslev

    Returns a 64 bit Steam ID.

    .INPUTS
    String.

    .OUTPUTS
    64 bit Steam ID.

    .NOTES
    Author: Frederik Hjorslev Nylander

    .LINK
    https://hjorslev.github.io/SteamPS/Resolve-VanityURL.html
    #>


    [CmdletBinding()]
    param (
        [Parameter(Mandatory = $true,
            HelpMessage = 'Enter the vanity URL (also named custom URL) to get a SteamID for.')]
        [ValidateScript( {
                if (([System.URI]$_ ).IsAbsoluteUri -eq $true) {
                    throw "Do not enter the fully qualified URL, but just the ID (e.g.) everything after https://steamcommunity.com/id/"
                }
                $true
            })]
        [string]$VanityURL,

        [Parameter(Mandatory = $false,
            HelpMessage = 'The type of vanity URL. 1 (default): Individual profile, 2: Group, 3: Official game group.')]
        [ValidateSet(1, 2, 3)]
        [int]$UrlType = 1,

        [Parameter(Mandatory = $false,
            HelpMessage = 'Format of the output. Options are json (default), xml or vdf.')]
        [ValidateSet('json', 'xml', 'vdf')]
        [string]$OutputFormat = 'json'
    )

    begin {
        Write-Verbose -Message "[BEGIN ] Starting: $($MyInvocation.MyCommand)"
    }

    process {
        $Request = Invoke-WebRequest -Uri "https://api.steampowered.com/ISteamUser/ResolveVanityURL/v1/?key=$(Get-SteamAPIKey)&vanityurl=$VanityURL&url_type=$UrlType&format=$OutputFormat" -UseBasicParsing

        if (($Request.Content | ConvertFrom-Json).response.success -eq '1') {
            [PSCustomObject]@{
                'SteamID64' = ([int64]($Request.Content | ConvertFrom-Json).response.steamid)
            }
        } elseif (($Request.Content | ConvertFrom-Json).response.success -eq '42') {
            $Exception = [Exception]::new("Unable to find $VanityURL.")
            $ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
                $Exception,
                "VanityURLNotFound",
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                ($Request.Content | ConvertFrom-Json).response.success
            )
            $PSCmdlet.ThrowTerminatingError($ErrorRecord)
        }
    } # Process

    end {
        Write-Verbose -Message "[END ] Ending: $($MyInvocation.MyCommand)"
    }
} # Cmdlet