forked from LeDragoX/Win-Debloat-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Request-FileDownload.psm1
78 lines (67 loc) · 2.91 KB
/
Request-FileDownload.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
77
78
Import-Module BitsTransfer
Import-Module -DisableNameChecking "$PSScriptRoot\Get-TempScriptFolder.psm1"
Import-Module -DisableNameChecking "$PSScriptRoot\Title-Templates.psm1"
function Request-FileDownload {
[CmdletBinding()]
[OutputType([String[]])]
param (
[Alias('URI')]
[Parameter(Position = 0, Mandatory)]
[String] $FileURI,
[Alias('Folder', 'OutFolder')]
[Parameter(Position = 1)]
[String] $OutputFolder = "$(Get-TempScriptFolder)\downloads",
[Alias('File', 'OutFile')]
[Parameter(Position = 2, Mandatory)]
[String] $OutputFile,
[Alias('RelativeFolder')]
[Parameter(Position = 3)]
[String] $ExtendFolder
)
Write-Verbose "[?] I'm at: $PWD"
Write-Verbose "[?] Downloading at: $OutputFolder + $ExtendFolder"
Write-Verbose "START '$OutputFolder' $($OutputFolder.GetType())"
If ($ExtendFolder) {
$OutputFolder = Join-Path -Path $OutputFolder -ChildPath $ExtendFolder
}
If (!(Test-Path $OutputFolder)) {
Write-Status -Types "@" -Status "$OutputFolder doesn't exist, creating folder..."
New-Item -Path $OutputFolder -ItemType Directory -Force
}
$FileLocation = Join-Path -Path $OutputFolder -ChildPath $OutputFile
Write-Status -Types "@" -Status "Downloading from: '$FileURI' as '$OutputFile'"
Write-Status -Types "@" -Status "On: '$FileLocation'"
Start-BitsTransfer -Source "$FileURI" -Destination "$FileLocation" -Dynamic -DisplayName $OutputFile -TransferType Download | Wait-Job
Write-Verbose "END '$FileLocation' $($FileLocation.GetType())"
return "$FileLocation"
}
function Get-APIFile {
[CmdletBinding()]
param (
[Parameter(Position = 0, Mandatory)]
[String] $URI,
[Parameter(Position = 1, Mandatory)]
[String] $ObjectProperty,
[Parameter(Position = 2, Mandatory)]
[String] $FileNameLike,
[Parameter(Position = 3, Mandatory)]
[String] $PropertyValue,
[Alias('Folder', 'OutFolder')]
[Parameter(Position = 4)]
[String] $OutputFolder,
[Parameter(Position = 5, Mandatory)]
[String] $OutputFile
)
$Response = Invoke-RestMethod -Method Get -Uri $URI | ForEach-Object $ObjectProperty | Where-Object name -like $FileNameLike
$FileURI = $Response."$PropertyValue"
If ($OutputFolder) {
return Request-FileDownload -FileURI $FileURI -OutputFolder $OutputFolder -OutputFile $OutputFile
} Else {
return Request-FileDownload -FileURI $FileURI -OutputFile $OutputFile
}
}
<#
Example:
$FileOutput = Request-FileDownload -FileURI "https://www.example.com/download/file.exe" -OutputFile "AnotherFileName.exe"
$WSLgOutput = Get-APIFile -URI "https://api.github.com/repos/microsoft/wslg/releases/latest" -ObjectProperty "assets" -FileNameLike "*$OSArch*.msi" -PropertyValue "browser_download_url" -OutputFile "wsl_graphics_support.msi"
#>