DownloadAndUnpack-Dynamics365Resource.psm1

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
function DownloadAndUnpack-Dynamics365Resource
{
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory=$true,
        Position=0)]
        [string]
        $ResourceUrl,
        [Parameter(Mandatory=$true,
        Position=1)]
        [string]
        $DirectoryPath,
        [Parameter(Mandatory=$false,
        Position=2)]
        [string]
        $ExpectedFileChecksum
    )
    if ( !( Get-ChildItem $directoryPath -ErrorAction Ignore ) ) {
        New-Item $directoryPath -ItemType Directory -Force | Out-Null;
        $resourceUrl -match '[^/\\&\?]+\.\w{3,4}(?=([\?&].*$|$))' | Out-Null
        $resourceFileName = $matches[0];
        $resourceFileName -match '\.[A-Za-z0-9]+$' | Out-Null
        if ( ( $matches[0] -eq ".exe" ) -and ( $resourceFileName -ne "vcredist_x64.exe" ) )
        {
            $tempDirName = [guid]::NewGuid().Guid;
            $tempDirPath = "$env:Temp\$tempDirName";
            $filePath = "$tempDirPath\$resourceFileName";
            New-Item $tempDirPath -ItemType Directory -Force | Out-Null;
            Write-Host "$(Get-Date) Downloading $resourceUrl to $filePath";
            $currentProgressPreference = $ProgressPreference;
            $ProgressPreference = 'SilentlyContinue'
            Invoke-WebRequest -Uri $resourceUrl -OutFile $filePath;
            $ProgressPreference = $currentProgressPreference;
            if ( Get-Item $filePath )
            {
                $fileHash = ( Get-FileHash $filePath -Algorithm SHA1 ).Hash;
                Write-Host "Hash of the downloaded file: $fileHash";
                if ( ( $fileHash -eq $expectedFileChecksum ) -or !$expectedFileChecksum )
                {
                    Write-Host "$(Get-Date) Unpacking $filePath to $directoryPath";
                    Start-Process -FilePath $filePath -ArgumentList "/extract:$directoryPath /passive /quiet" -Wait -NoNewWindow;
                    Write-Host "$(Get-Date) Finished unpacking";
                    Remove-Item $filePath;
                } else {
                    Write-Host "Hash does not match $expectedFileChecksum";
                    Throw "Hash does not match $expectedFileChecksum";
                    Remove-Item $filePath;
                }
            }
        } else {
            $filePath = "$directoryPath\$resourceFileName";
            Write-Host "$(Get-Date) Downloading $resourceUrl to $filePath";
            $currentProgressPreference = $ProgressPreference;
            $ProgressPreference = 'SilentlyContinue'
            Invoke-WebRequest -Uri $resourceUrl -OutFile $filePath;
            $ProgressPreference = $currentProgressPreference;
            if ( Get-Item $filePath )
            {
                $fileHash = ( Get-FileHash $filePath -Algorithm SHA1 ).Hash;
                Write-Host "Hash of the downloaded file: $fileHash";
                if ( ( $fileHash -eq $expectedFileChecksum ) -or !$expectedFileChecksum )
                {
                    Write-Host "$(Get-Date) Finished Downloading";
                } else {
                    Write-Host "$(Get-Date) Hash does not match $expectedFileChecksum";
                    Throw "Hash does not match $expectedFileChecksum";
                    Remove-Item $filePath;
                }
            }
        }
    } else {
        Write-Host "The target directory is not empty. Skipped downloading."
    }
}