New-QRCodeGeolocation.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
function New-QRCodeGeolocation
{
    <#
            .SYNOPSIS
            Creates a QR code graphic containing a geo location
 
            .DESCRIPTION
            Creates a QR code graphic in png format that - when scanned by a smart device - opens the geo location in a browser.
 
            .PARAMETER Latitude
            The location latitude
 
            .PARAMETER Longitude
            The location longitude
 
            .PARAMETER Width
            Height and Width of generated graphics (in pixels). Default is 100.
 
            .PARAMETER Show
            Opens the generated QR code in associated program
 
            .PARAMETER OutPath
            Path to generated png file. When omitted, a temporary file name is used.
 
            .EXAMPLE
            New-QRCodeGeoLocation -Latitude 21.12 -Longitude 22.87 -Width 200 -Show -OutPath "$home\Desktop\qr.png"
            Creates a QR code png graphics on your desktop, and opens it with the associated program
 
            .NOTES
            Compatible with all PowerShell versions including PowerShell 6/Core
            Uses binaries from https://github.com/codebude/QRCoder/wiki
 
            .LINK
            https://github.com/codebude/QRCoder/wiki
    #>


    [CmdletBinding(DefaultParameterSetName="Address")]
    param
    (
        [Parameter(Mandatory,ParameterSetName='Location')]
        [double]
        $Latitude,

        [Parameter(Mandatory,ParameterSetName='Location')]
        [double]
        $Longitude,
        
        [Parameter(Mandatory,ParameterSetName='Address')]
        [string]
        $Address,

        [ValidateRange(10,2000)]
        [int]
        $Width = 100,

        [Switch]
        $Show,

        [string]
        $OutPath = "$env:temp\qrcode.png"
    )

    if ($PSCmdlet.ParameterSetName -eq "Address")
    {
        $AddressEncoded = [System.Net.WebUtility]::UrlEncode($Address)
        $response = Invoke-RestMethod -Uri "http://maps.googleapis.com/maps/api/geocode/xml?address=$addressEncoded"
        if ($response.GeocodeResponse.status -ne 'OK') { throw "Unable to retrieve location. You may be offline." }
        $Latitude = $response.GeocodeResponse.result.geometry.location.lat
        $Longitude = $response.GeocodeResponse.result.geometry.location.lng
    }
    
    $payload = @"
geo:$Latitude,$Longitude
"@


    Add-Type -Path $PSScriptRoot\binaries\QRCoder.dll
 
    $generator = New-Object -TypeName QRCoder.QRCodeGenerator
    $data = $generator.CreateQrCode($payload, 'Q')
    $code = new-object -TypeName QRCoder.PngByteQRCode -ArgumentList ($data)
    $byteArray = $code.GetGraphic($Width)
    [System.IO.File]::WriteAllBytes($outPath, $byteArray)

    if ($Show) { Invoke-Item -Path $outPath }
}