-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImport-ConfigFile.ps1
More file actions
74 lines (73 loc) · 2.69 KB
/
Copy pathImport-ConfigFile.ps1
File metadata and controls
74 lines (73 loc) · 2.69 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
function Import-ConfigFile {
<#
.SYNOPSIS
Imports config file via provided FilePath based on its file type.
.DESCRIPTION
Imports config file via provided FilePath based on its file type to be used with dot notation and iterated through
.NOTES
Author: Nick Welter
Function Created: 03/28/2024
Function Updated: 04/17/2024
Currently supported file types:
.json
.xml
.ini
.txt
.EXAMPLE
Import-ConfigFile.ps1 -Verbose
Imports config file via provided FilePath based on its file type.
#>
[CmdletBinding()]
param (
[Parameter(Mandatory)]
[string]$FilePath
)
begin {
#- Test-Path of Config File
Write-Verbose -Message "Checking file path of provided config file."
if(!(Test-Path -Path $FilePath -ErrorAction Stop)){
Write-Error -Message "File path `"$FilePath`" does not exist or cannot be accessed."
throw
}else{
try {
$ConfigFileSource = Get-Item -Path "$FilePath" -Verbose -ErrorAction Stop
Write-Verbose -Message "Config file found."
}
catch {
Write-Error -Message "Failed to get item at path '$FilePath'. Error: $_"
throw
}
}
}
process {
#- import its contents
switch ($ConfigFileSource.Extension) {
".json" {
$ConfigFile = Get-Content -Path $FilePath | Out-String | ConvertFrom-Json
Write-Verbose -Message "$($ConfigFileSource.Name) has been imported from a JSON file"
}
".xml" {
$XMLFile = Get-Content -Path $FilePath; $ConfigFile = [xml]$XMLFile
Write-Verbose -Message "$($ConfigFileSource.Name) has been imported from a XML file"
}
".ini" {
$ConfigFile = Get-Content -Path $FilePath | Select-Object -Skip 1 | ConvertFrom-StringData
Write-Verbose -Message "$($ConfigFileSource.Name) has been imported from a INI file"
}
Default {
try {
$ConfigFile = Get-Content -Path $FilePath
Write-Verbose -Message "$($ConfigFileSource.Name) has been imported with default settings."
}
catch {
Write-Error -Message "Failed to read the configuration file at path '$FilePath'. Error: $_"
throw
}
}
}
}
end {
#- Return the contents of the config file
return $ConfigFile
}
}