-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOut-DataTable.ps1
More file actions
86 lines (81 loc) · 3.11 KB
/
Out-DataTable.ps1
File metadata and controls
86 lines (81 loc) · 3.11 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
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
<#
.SYNOPSIS
Converts a Object list into the DataTable.
.DESCRIPTION
Converts a Object list into the DataTable.
.PARAMETER Object
Pass Object
.OUTPUTS
System.Data.DataTable
.EXAMPLE
Out-DataTable $ObjectList
Converts a Object list into the DataTable from the object properties and assigns output to $dt variable .
#>
function Out-DataTable {
[CmdletBinding()]
Param(
[Parameter(Position = 0, ValueFromPipeline = $true)]
[PSObject[]]$InputObject
)
Begin {
$dt = new-object Data.datatable
$First = $true
}
Process {
try {
if ($InputObject.Count -le 0) {
return $null;
}
foreach ($object in $InputObject) {
$DR = $DT.NewRow()
foreach ($property in $object.PsObject.get_properties()) {
if ($first) {
$Col = new-object Data.DataColumn
$Col.ColumnName = $property.Name.ToString()
if ($property.value) {
if ($property.value -isnot [System.DBNull]) {
$Col.DataType = [System.Type]::GetType("$(Get-Type $property.TypeNameOfValue)")
}
}
$DT.Columns.Add($Col)
}
if ($property.Gettype().IsArray) {
$DR.Item($property.Name) = $property.value | ConvertTo-XML -AS String -NoTypeInformation -Depth 1
}
else {
switch ($property.TypeNameOfValue) {
"System.String" {
$DR.Item($property.Name) = Reset-ToValidString $property.value;
Break;
}
"System.Int16" {
$DR.Item($property.Name) = Reset-ToZeroStringIfNull $property.value;
Break;
}
"System.Int32" {
$DR.Item($property.Name) = Reset-ToZeroStringIfNull $property.value;
Break;
}
"System.Int64" {
$DR.Item($property.Name) = Reset-ToZeroStringIfNull $property.value;
Break;
}
Default {
$DR.Item($property.Name) = Reset-ToZeroStringIfNull $property.value;
Break;
}
}
}
}
$DT.Rows.Add($DR)
$First = $false
}
return @(, ($dt))
}
catch {
Write-Exception $_ "Throws an exception in 'OUT-DataTable'" $true
}
}
End {
}
}