-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-ConfigFile.ps1
More file actions
151 lines (148 loc) · 5 KB
/
Copy pathGet-ConfigFile.ps1
File metadata and controls
151 lines (148 loc) · 5 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
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
function Get-ConfigFile
{
<#
.SYNOPSIS
Downloads/Copies over a config file from a remote location
.DESCRIPTION
Downloads/Copies over a config file from a remote location. Returns a file path location for the config file.
.NOTES
Author: Nick Welter
Function Created: 04/17/2024
Function Updated: 04/17/2024
Currently supports only UNC paths or HTTP(S) paths
.PARAMETER DownloadSource
Either UNC Path or HTTP(S) path of the config file location. Ends with the filename and extention
.PARAMETER Destination
Location to place the downloaded config file. Ends with the filename and extention
.EXAMPLE
Get-ConfigFile -DownloadSource "\\Server\Test\Config.json" -Destination "C:\temp\config.json" -Verbose
Attempts to Download/Copy over a config file from a remote location with Verbose logging.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory = $true)]
[String]$DownloadSource,
[Parameter(Mandatory = $true)]
[String]$Destination
)
process
{
#Determine if download source is a URL or UNC path
Write-Verbose -Message "Checking download source type"
if ($DownloadSource -like "\\*")
{
$DownloadSourceType = "UNC"
Write-Verbose -Message "Download source type: UNC"
}
elseif ($DownloadSource -like "http*")
{
$DownloadSourceType = "HTTP"
Write-Verbose -Message "Download source type: HTTP"
}
else
{
Write-Error -Message "Unable to determine download source type. Please verify DownloadSource parameter."
throw
}
#Test paths of Locations
Write-Verbose -Message "Testing Path of Downloadsource"
if ($DownloadSourceType -eq "UNC")
{
#UNC Test-Path
if (-not(Test-Path -Path $DownloadSource -ErrorAction SilentlyContinue))
{
Write-Error -Message "Was unable to reach the DownloadSource: $DownloadSource. Please verify DownloadSource parameter."
throw
}
else
{
Write-Verbose -Message "Verified DownloadSource location"
}
}
else
{
#HTTP Test Path
$certCallback = @"
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
public class ServerCertificateValidationCallback
{
public static void Ignore()
{
if(ServicePointManager.ServerCertificateValidationCallback ==null)
{
ServicePointManager.ServerCertificateValidationCallback +=
delegate
(
Object obj,
X509Certificate certificate,
X509Chain chain,
SslPolicyErrors errors
)
{
return true;
};
}
}
}
"@
Add-Type $certCallback
[ServerCertificateValidationCallback]::Ignore()
if (-not((Invoke-WebRequest -Uri $DownloadSource -UseBasicParsing).Statuscode -eq 200))
{
Write-Error -Message "Was unable to reach the DownloadSource: $DownloadSource. Please verify DownloadSource parameter."
throw
}
else
{
Write-Verbose -Message "Verified DownloadSource location"
}
}
#Download the config file
Write-Verbose -Message "Attemping to download the config file"
if ($DownloadSourceType -eq "UNC")
{
#UNC Copy
try
{
Copy-Item -Path $DownloadSource -Destination $Destination -Force -ErrorAction Stop
$ConfigFileLocation = $Destination
Write-Verbose -Message "ConfigFile successfully downloaded"
}
catch
{
Write-Error -Message "Download of Config file failed."
throw
}
}
else
{
#HTTP download
try
{
Invoke-WebRequest -Uri $DownloadSource -OutFile $Destination -UseBasicParsing -ErrorAction Stop
$ConfigFileLocation = $Destination
Write-Verbose -Message "ConfigFile successfully downloaded"
}
catch
{
Write-Error -Message "Download of Config file failed."
throw
}
}
}
end
{
if (Test-Path -Path $ConfigFileLocation -ErrorAction SilentlyContinue)
{
return $ConfigFileLocation
}
else
{
Write-Error -Message "Was unable to verify new config was sucessfully downloaded"
throw
}
}
}