-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathSelect-DataTable.ps1
69 lines (63 loc) · 3.8 KB
/
Select-DataTable.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
function Select-Datatable
{
<#
.Synopsis
Selects data from an in-memory database
.Description
Selects data from System.Data.Datatable, which is an in-memory database
.Example
$dt = dir | Select Name, LastWriteTime, LastAccessTime, CreationTime | ConvertTo-DataTable
Select-DataTable -DataTable $dt -Sort LastWriteTime -SortOrder Descending
.Link
Update-DataTable
.Link
New-DataTable
.Link
ConvertTo-DataTable
.Link
Export-DataTable
.Link
Import-DataTable
#>
[OutputType([Data.DataRow])]
param(
# The datatable object
[Parameter(Mandatory=$true,Position=0)]
[Data.DataTable]
$DataTable,
# The condition. This can contain any normal SQL operators
[Parameter(Position=1,ValueFromPipelineByPropertyName=$true)]
[string]
$Where,
# The sort order
[Parameter(Position=2,ValueFromPipelineByPropertyName=$true)]
[string[]]
$Sort,
# The type of sort, either ascending or descding
[Parameter(Position=3,ValueFromPipelineByPropertyName=$true)]
[ValidateSet("A", "Asc,", "Ascending", "D", "Desc","Descending")]
[string[]]
$SortOrder
)
process {
$realSort = if ($Sort) {
@(for ($i =0; $i -lt $sort.count; $i++) {
$s = $sort[$i]
if ($i -lt $SortOrder.Count) {
if ($SortOrder[$i].StartsWith("A")) {
"$s ASC"
} elseif ($SortOrder[$i].StartsWith("D")) {
"$s DESC"
}
} else {
"$s"
}
}) -join ' '
}
if ($realSort) {
$DataTable.Select($Where, $realSort)
} else {
$DataTable.Select($Where)
}
}
}