Public/Utils.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
<#
.SYNOPSIS
Attempts to clear the /tmp directory when run from Linux
#>

function Clear-Tmp {
    if ($IsLinux)
    {
        try
        {
            Remove-Item -Recurse -Path "/tmp/*"
        }
        catch
        {
            # Ignore
        }
    }
}

function New-LambdaTemporaryDirectory {
    $dir = "/tmp/$([guid]::NewGuid().ToString())"
    mkdir $dir | Out-Null
    return $dir
}

function New-GeneralTemporaryDirectory {
    $dir = New-TemporaryFile
    Remove-Item $dir | Out-Null
    mkdir $dir | Out-Null
    return $dir.ToString()
}

function New-TemporaryDirectory {
    if ($IsLinux) {
        return New-LambdaTemporaryDirectory
    }

    return New-GeneralTemporaryDirectory
}

function Expand-Archive
{
    param (
        [string]$zipfile,
        [string]$outpath
    )

    [System.IO.Compression.ZipFile]::ExtractToDirectory($zipfile, $outpath)
}

function Expand-RemoteArchive {
    param (
        [string]$tempDir,
        [string]$url,
        [string]$executableName = $null
    )

    $ProgressPreference = 'SilentlyContinue'

    $tmpFile = "$tempDir/$([guid]::NewGuid().ToString()).zip"
    Invoke-WebRequest -Uri $url -OutFile $tmpFile
    Expand-Archive $tmpFile $tempDir

    if ($executableName -ne $null) {
        if ($IsLinux)
        {
            chmod +x "$tempDir/$executableName"
        }
    }

    Remove-Item $tmpFile
}

function Enable-TLS12 {
    [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12, [Net.SecurityProtocolType]::Tls11, [Net.SecurityProtocolType]::Tls
}