forked from jeffcutsinger-hb/PoshAws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.ps1
127 lines (98 loc) · 3.22 KB
/
Utility.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
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
# This allows you work on the file as a script
$_privateCommand = Get-Command -Name "__MakeWildcard"
if (!$_privateCommand) {
. "$PSScriptRoot\QuotePrivateUnquoteFunctions.ps1"
}
function Import-AwsSdk {
[CmdletBinding()]
param(
[string]$SearchPath,
[switch]$Force
)
$_needToLoad = $Force.ToBool()
try {
New-Object Amazon.S3.Model.ListObjectsRequest | Out-Null
} catch {
$_needToLoad = $true
}
if ($_needToLoad) {
$_lookIn = $PWD
if ($SearchPath) {
$_lookIn = $SearchPath
}
Write-Debug "Searching $_lookIn for AWS SDK."
$_sdkAssembly = Get-ChildItem -Path $_lookIn -Filter "AWSSDK.dll" -Recurse `
| Sort-Object -Descending -Property VersionInfo `
| Select-Object -First 1
if ($_sdkAssembly) {
$_sdkAssemblyName = $_sdkAssembly.FullName
Write-Debug "Using AWS SDK from $_sdkAssemblyName."
# Copy it to prevent file locking issues.
$_shadowPath = Join-Path (Join-Path $env:TEMP (Get-Random)) .\awscommands\AWSSDK.dll
New-Item -Path (Split-Path $_shadowPath) -ItemType directory -ErrorAction SilentlyContinue | Out-Null
Copy-Item $_sdkAssemblyName $_shadowPath -Force
Add-Type -Path $_shadowPath
}
else {
throw "Could not locate AWS SDK!"
}
}
}
function ConvertTo-Hashtable {
[CmdletBinding()]
param(
[Parameter(ValueFromPipeline = $true, Position = 0)]
$InputObject,
[switch]$TreatFalseyAsEmpty
)
if ($InputObject) {
if ($InputObject -is [hashtable]) {
$InputObject
} elseif ($InputObject -is [PSCustomObject]) {
$_table = @{}
$InputObject.PSObject.Properties `
| Where-Object {
$_.IsGettable -and $_.IsInstance
} `
| ForEach-Object {
$_table.Add($_.Name, $_.Value)
}
$_table
} else {
$_inputType = $InputObject.GetType()
throw "Can't convert $_inputType to a hashtable."
}
} elseif ($TreatFalseyAsEmpty) {
@{}
} else {
[hashtable]$null
}
}
function Merge-Objects {
[CmdletBinding()]
param(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
$InputObject,
[Parameter(Position = 1)]
$AdditionalValues,
[ValidateSet("Hashtable", "PSCustomObject", IgnoreCase = $true)]
[Parameter(Position = 2)]
$OutputType = "Hashtable"
)
[hashtable]$_inputHashtable = ConvertTo-Hashtable -InputObject $InputObject
[hashtable]$_additionalHashtable = ConvertTo-Hashtable -InputObject $AdditionalValues -TreatFalseyAsEmpty
$_merged = @{}
$_inputHashtable.GetEnumerator() `
| ForEach-Object {
$_merged.Add($_.Key, $_.Value)
}
$_additionalHashtable.GetEnumerator() `
| ForEach-Object {
$_merged[$_.Key] = $_.Value
}
if ($OutputType -eq "Hashtable") {
$_merged
} else {
New-Object psobject -Property $_merged
}
}