-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConvertTo-HashTable.ps1
40 lines (38 loc) · 1.12 KB
/
ConvertTo-HashTable.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
function ConvertTo-Hashtable {
<#
.SYNOPSIS
Convert InputObject to Hashtable based on KeyField and ValueField.
.DESCRIPTION
Convert InputObject to Hashtable based on KeyField and ValueField.
.EXAMPLE
gci | ConvertTo-Hashtable -KeyField Name -ValueField LastwriteTime
#>
[CmdletBinding(PositionalBinding = $false)]
[Alias()]
[OutputType([Hashtable])]
Param (
# Param1 help description
[Parameter(Mandatory = $true, ValueFromPipeline = $true)]
[ValidateNotNull()]
[object[]] $InputObject,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true, Position = 0)]
[string] $KeyField,
[ValidateNotNullOrEmpty()]
[Parameter(Mandatory = $true, Position = 1)]
[String] $ValueField
)
begin {
$result = @{}
}
process {
foreach ($item in $InputObject) {
$key = $item | Select-Object -ExpandProperty $KeyField
$value = $item | Select-Object -ExpandProperty $ValueField
$result.Add($key, $value)
}
}
end {
$result
}
}