public/Get-SpeedTestConfig.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
<#
    .SYNOPSIS
    Get the default server configurations for Internet and Local speed test servers.

    .DESCRIPTION
    Get the default server configurations for Internet and Local speed test servers.
    If '-PassThru' is used, converts the JSON configuration file into a PSCustomObject.

    .EXAMPLE
    Get-SpeedTestConfig

    .EXAMPLE
    Get-SpeedTestConfig -PassThru
#>


function Get-SpeedTestConfig {
    [CmdletBinding()]
    Param(
        [Switch]
        $PassThru
    )

    try {
        Write-Verbose -Message "Getting content of config.json and returning as a PSCustomObject."
        $config = Get-Content -Path "$($PSScriptRoot | Split-Path -Parent)\config.json" -ErrorAction "Stop" | ConvertFrom-Json

        if ($PassThru){
            return $config
        }
        else {
            Write-Host "Internet Server: $($config.defaultInternetServer.defaultServer)"
            Write-Host "Internet Port: $($config.defaultInternetServer.defaultPort)"
            Write-Host "Local Server: $($config.defaultLocalServer.defaultServer)"
            Write-Host "Local Port: $($config.defaultLocalServer.defaultPort)"
        }
    }
    catch {
        throw "Can't find the JSON configuration file. Use 'Set-SpeedTestConfig' to create one."
    }
}