-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-DataFromSql.ps1
More file actions
73 lines (70 loc) · 2.9 KB
/
Get-DataFromSql.ps1
File metadata and controls
73 lines (70 loc) · 2.9 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
function Get-DataFromSql {
[CmdletBinding()]
param(
[Parameter(Mandatory = $false)]
[string[]]$sql = $(Throw ((Get-ResStr 'PARAM_MANDATORY_MISSED') -f 'sql', $myInvocation.Mycommand))
,
[Parameter(Mandatory = $false)]
[string]$arrRoot = 'Items'
,
[Parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidateConn -conn $_ })]
$conn
,
[Parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidatePathUDL -path $_ })]
[string]$udl
,
[Parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidateConnStr -connStr $_ })]
[string]$connStr
)
begin {
Write-Verbose -Message ((Get-ResStr 'STARTING_FUNCTION') -f $myInvocation.Mycommand)
Test-ValidateSingle -validParams (Get-SingleConnection) @PSBoundParameters
New-Variable -Name 'field' -Scope 'Private' -Value ($null)
New-Variable -Name 'i' -Scope 'Private' -Value ([int32]0)
New-Variable -Name 'myConn' -Scope 'Private' -Value ($null)
New-Variable -Name 'record' -Scope 'Private' -Value ($null)
New-Variable -Name 'rs' -Scope 'Private' -Value ($null)
New-Variable -Name 'result' -Scope 'Private' -Value ([System.Collections.ArrayList]@())
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
if (! $conn) { $closeConn = $true } else { $closeConn = $false }
$myConn = Get-Conn -conn $conn -udl $udl -connStr $connStr
$result = New-Object System.Collections.ArrayList
$rs = $Null
$rs = $myConn.Execute($sql[0])
$rs = Get-AdoRs -recordset $rs
if ($rs) {
while (! $rs.eof) {
$record = [ordered]@{}
for ($i=0; $i -lt $rs.fields.count; $i++) {
$field = $rs.fields[$i]
$record.add($field.name, $field.value) | Out-Null
}
if ($sql.Count -gt 1) {
# Detail exists
[string]$sqlTemp= $sql[1]
if ($sql.Count -gt 2) {
# Master / Detail Link exists
[string]$sqlLink= $rs.fields[$sql[2]].value.replace("'","''")
$sqlTemp = ($sqlTemp -f $sqlLink)
}
$record.add($arrRoot, (Get-DataFromSql -sql @($sqlTemp) -conn $myConn -udl $udl -connStr $connStr )) | Out-Null
}
$result.Add($record) | Out-Null
$rs.MoveNext() | Out-Null
}
}
if ($closeConn) {
$myConn.close()
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# Test: Get-DataFromSql -sql @('SELECT TOP 10 ArtNummer, Vk, Kurztext1 FROM Artikel') -udl '.\source\tests\Eulanda_1 Pester.udl'
}