Private/API/Get-SteamAPIKey.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
function Get-SteamAPIKey {
    <#
    .SYNOPSIS
    Grabs API key secure string from file and converts back to plaintext.

    .DESCRIPTION
    Grabs API key secure string from file and converts back to plaintext.

    .EXAMPLE
    Get-SteamAPIKey

    Returns the API key secure string in plain text.

    .NOTES
    Author: sysgoblin (https://github.com/sysgoblin) and Frederik Hjorslev Poulsen
    #>


    [CmdletBinding()]
    Param (
    )

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

    process {
        $SteamPSKey = Test-Path -Path "$env:AppData\SteamPS\SteamPSKey.json"
        if (-not ($SteamPSKey)) {
            $Exception = [Exception]::new("Steam Web API configuration file not found in '$env:AppData\SteamPS\SteamPSKey.json'. Run Connect-SteamAPI to configure an API key.")
            $ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
                $Exception,
                "SteamAPIKeyNotFound",
                [System.Management.Automation.ErrorCategory]::ObjectNotFound,
                $SteamPSKey # usually the object that triggered the error, if possible
            )
            $PSCmdlet.ThrowTerminatingError($ErrorRecord)
        } else {
            try {
                $Config = Get-Content "$env:AppData\SteamPS\SteamPSKey.json"
                $APIKeySecString = $Config | ConvertTo-SecureString
                $BSTR = [System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($APIKeySecString)
                $APIKey = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)

                Write-Output -InputObject $APIKey
            } catch {
                $Exception = [Exception]::new("Could not decrypt API key from configuration file not found in '$env:AppData\SteamPS\SteamPSKey.json'. Run Connect-SteamAPI to configure an API key.")
                $ErrorRecord = [System.Management.Automation.ErrorRecord]::new(
                    $Exception,
                    "InvalidAPIKey",
                    [System.Management.Automation.ErrorCategory]::ParserError,
                    $APIKey # usually the object that triggered the error, if possible
                )
                $PSCmdlet.ThrowTerminatingError($ErrorRecord)
            }
        }
    } # Process

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