-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertTo-HashTable.ps1
More file actions
49 lines (42 loc) · 1.18 KB
/
ConvertTo-HashTable.ps1
File metadata and controls
49 lines (42 loc) · 1.18 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
Function ConvertTo-HashTable
{
[OutputType([hashtable])]
Param
(
[Parameter(Mandatory = $true, ValueFromPipeline = $true, Position = 0)]
[Alias('PSCustomObject', 'PSObject')]
[ValidateNotNullOrEmpty()]
[PSCustomObject[]]
$Object,
[Parameter(Position = 1)]
[switch]
$RemoveNullOrEmpty,
[Parameter(Position = 2)]
[switch]
$ReplaceNullWithEmpty
)
Process
{
$Object | ForEach-Object -Process {
$_.PSObject.Properties | ForEach-Object -Begin {
# Create empty hashtable
$HashTable = [hashtable]@{}
} -Process {
# Fill the hashtable
If (!($RemoveNullOrEmpty.IsPresent) -or ($RemoveNullOrEmpty.IsPresent -and !([string]::IsNullOrEmpty($_.Value)))) {
# Check for null values
If ($ReplaceNullWithEmpty.IsPresent -and $null -eq $_.Value) {
# Create the key with an empty value
$HashTable[$_.Name] = ''
} Else {
# Create the key with the matching value
$HashTable[$_.Name] = $_.Value
}
}
} -End {
# Return the hashtable
$HashTable
}
}
}
}