-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathDownloadFileWithCredentials.psm1
More file actions
47 lines (38 loc) · 1.86 KB
/
Copy pathDownloadFileWithCredentials.psm1
File metadata and controls
47 lines (38 loc) · 1.86 KB
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
<#
This function downloads assets locally to a destination folder location for which a source URL is provided
#>
Function Invoke-DownloadFileWithCredentialsTask {
[CmdletBinding(SupportsShouldProcess = $true)]
param(
[Parameter(Mandatory = $true)]
[string]$SourceUri,
[Parameter(Mandatory = $true)]
[ValidateScript( { Test-Path -Path (Split-Path -Path $_ -Parent) })]
[ValidateScript( { Test-Path -Path $_ -PathType Leaf -IsValid })]
[string]$Destinationfolder,
[PSCredential]$Credentials,
[string]$Assetfilename,
[string]$TypeSource
)
if ($PSCmdlet.ShouldProcess($SourceUri, "Download $SourceUri to $Destinationfolder")) {
$ProgressPreference = 'SilentlyContinue'
try {
Write-Verbose "Downloading $SourceUri to $Destinationfolder"
$DestinationPath = Join-Path $Destinationfolder $Assetfilename
if ($Credentials -and ($TypeSource -eq "sitecore")) {
$user = $Credentials.GetNetworkCredential().username
$password = $Credentials.GetNetworkCredential().password
$loginRequest = Invoke-RestMethod -Uri https://dev.sitecore.net/api/authorization -Method Post -ContentType "application/json" -Body "{username: '$user', password: '$password'}" -SessionVariable session -UseBasicParsing
Invoke-WebRequest -Uri $SourceUri -OutFile $DestinationPath -WebSession $session -UseBasicParsing
}
elseif ($TypeSource -eq "github")
{
Write-Verbose "here"
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-WebRequest -Uri $SourceUri -OutFile $DestinationPath -UseBasicParsing
}
} catch {
Write-Error -Message ("Error downloading $SourceUri" + ": $($_.Exception.Message)")
}
}
}