-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathImport-JSON.ps1
46 lines (43 loc) · 995 Bytes
/
Import-JSON.ps1
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
Function Import-JSON
{
[cmdletbinding()]
param
(
[parameter(Mandatory)]
[ValidateScript( {Test-Path -Path $_})]
$Path
,
[parameter()]
[validateSet('Unicode', 'UTF7', 'UTF8', 'ASCII', 'UTF32', 'BigEndianUnicode', 'Default', 'OEM')]
$Encoding
)
begin
{
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}
end
{
$GetContentParams = @{
Path = $Path
Raw = $true
}
if ($null -ne $Encoding)
{$GetContentParams.Encoding = $Encoding}
try
{
$Content = Get-Content @GetContentParams
}
catch
{
$_
}
if ($null -eq $content -or $content.Length -lt 1)
{
throw("No content found in file $Path")
}
else
{
ConvertFrom-Json -InputObject $Content
}
}
}