]>
Commit | Line | Data |
---|---|---|
1e59de90 TL |
1 | #Requires -Version 6.0 |
2 | Set-StrictMode -Version 2 | |
3 | ||
4 | <# | |
5 | .SYNOPSIS | |
6 | Returns whether the specified command exists in the current environment. | |
7 | ||
8 | .DESCRIPTION | |
9 | Get-CommandExists takes a string as a parameter, | |
10 | and returns whether it exists in the current environment; | |
11 | either a function, alias, or an executable in the path. | |
12 | It's somewhat equivalent to `which`. | |
13 | ||
14 | .PARAMETER Name | |
15 | Specifies the name of the command which may or may not exist. | |
16 | ||
17 | .INPUTS | |
18 | System.String | |
19 | The name of the command. | |
20 | ||
21 | .OUTPUTS | |
22 | System.Boolean | |
23 | Whether the command exists. | |
24 | #> | |
25 | function Get-CommandExists | |
26 | { | |
27 | [CmdletBinding()] | |
28 | [OutputType([Boolean])] | |
29 | Param( | |
30 | [Parameter(ValueFromPipeline)] | |
31 | [String]$Name | |
32 | ) | |
33 | ||
34 | $null -ne (Get-Command -Name $Name -ErrorAction SilentlyContinue) | |
35 | } | |
36 | ||
37 | <# | |
38 | .SYNOPSIS | |
39 | Downloads a file and checks its hash. | |
40 | ||
41 | .DESCRIPTION | |
42 | Get-RemoteFile takes a URI and a hash, | |
43 | downloads the file at that URI to OutFile, | |
44 | and checks that the hash of the downloaded file. | |
45 | It then returns a FileInfo object corresponding to the downloaded file. | |
46 | ||
47 | .PARAMETER OutFile | |
48 | Specifies the file path to download to. | |
49 | ||
50 | .PARAMETER Uri | |
51 | The URI to download from. | |
52 | ||
53 | .PARAMETER Sha256 | |
54 | The expected SHA256 of the downloaded file. | |
55 | ||
56 | .INPUTS | |
57 | None | |
58 | ||
59 | .OUTPUTS | |
60 | System.IO.FileInfo | |
61 | The FileInfo for the downloaded file. | |
62 | #> | |
63 | function Get-RemoteFile | |
64 | { | |
65 | [CmdletBinding(PositionalBinding=$False)] | |
66 | [OutputType([System.IO.FileInfo])] | |
67 | Param( | |
68 | [Parameter(Mandatory=$True)] | |
69 | [String]$OutFile, | |
70 | [Parameter(Mandatory=$True)] | |
71 | [String]$Uri, | |
72 | [Parameter(Mandatory=$True)] | |
73 | [String]$Sha256 | |
74 | ) | |
75 | ||
76 | Invoke-WebRequest -OutFile $OutFile -Uri $Uri | |
77 | $actualHash = Get-FileHash -Algorithm SHA256 -Path $OutFile | |
78 | ||
79 | if ($actualHash.Hash -ne $Sha256) { | |
80 | throw @" | |
81 | Invalid hash for file $OutFile; | |
82 | expected: $Hash | |
83 | found: $($actualHash.Hash) | |
84 | Please make sure that the hash in the powershell file is correct. | |
85 | "@ | |
86 | } | |
87 | ||
88 | Get-Item $OutFile | |
89 | } | |
90 |