-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGet-XmlEulandaAddress.ps1
More file actions
104 lines (98 loc) · 4.56 KB
/
Get-XmlEulandaAddress.ps1
File metadata and controls
104 lines (98 loc) · 4.56 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
function Get-XmlEulandaAddress {
[CmdletBinding()]
param(
[parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidateSelect -strParam $_ })]
[string]$select= '*'
,
[parameter(Mandatory = $false)]
[string[]]$filter
,
[parameter(Mandatory = $false)]
[ValidateScript({ Test-ValidateMapping -strValue $_ -mapping (Get-MappingAddressKeys) })]
[string]$alias = 'addressMatch'
,
[parameter(Mandatory = $false)]
[string]$order = $alias
,
[parameter(Mandatory = $false)]
[switch]$reorder
,
[parameter(Mandatory = $false)]
[switch]$revers
,
[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)
$alias = Test-ValidateMapping -strValue $alias -mapping (Get-MappingAddressKeys)
$order = Test-ValidateMapping -strValue $order -mapping (Get-MappingAddressKeys)
Test-ValidateSingle -validParams (Get-SingleConnection) @PSBoundParameters
New-Variable -Name 'memoryStream' -Scope 'Private' -Value ($null)
New-Variable -Name 'myConn' -Scope 'Private' -Value ($null)
New-Variable -Name 'nodeName' -Scope 'Private' -Value ([string]'')
New-Variable -Name 'nodeValue' -Scope 'Private' -Value ([string]'')
New-Variable -Name 'ParamsAddress' -Scope 'Private' -Value ([System.Collections.Hashtable]@{})
New-Variable -Name 'rs' -Scope 'Private' -Value ($null)
New-Variable -Name 'sql' -Scope 'Private' -Value ([string[]]@())
New-Variable -Name 'streamReader' -Scope 'Private' -Value ($null)
New-Variable -Name 'writer' -Scope 'Private' -Value ($null)
New-Variable -Name 'xmlWriterSettings' -Scope 'Private' -Value ($null)
New-Variable -Name 'result' -Scope 'Private' -Value ([string]'')
$initialVariables = Get-CurrentVariables -Debug:$DebugPreference
}
process {
$myConn = Get-Conn -conn $conn -udl $udl -connStr $connStr
$ParamsAddress = Get-UsedParameters -validParams (Get-SetAddressFilter) -boundParams $PSBoundParameters
[string[]]$sql= Get-AddressSql @ParamsAddress
$rs = $Null
$rs = $myConn.Execute($sql[0])
$rs = Get-AdoRs -recordset $rs
if ($rs) {
# Creating a MemoryStream object to store the XML text
$memoryStream = New-Object System.IO.MemoryStream
# Use xmlWriterSettings to change the default settings of the xmlWriter
$xmlWriterSettings = New-Object System.Xml.XmlWriterSettings
$xmlWriterSettings.Encoding = [System.Text.UTF8Encoding]::new()
# Prevent the declaration at the beginning like: <?xml version="1.0" encoding="utf-8"?>
$xmlWriterSettings.OmitXmlDeclaration = $true
# Creating an XmlWriter object to write the XML structure
$writer = [System.Xml.XmlWriter]::Create($memoryStream, $xmlWriterSettings)
# Writing the XML structure with the XmlWriter object
$writer.WriteStartElement('ADRESSELISTE')
while (! $rs.eof) {
$writer.WriteStartElement('ADRESSE')
for ($i=0; $i -lt $rs.fields.count; $i++) {
[string]$nodeName = $rs.fields[$i].Name.ToUpper()
[string]$nodeValue = [string]$rs.fields[$i].value
$writer.WriteElementString($nodeName,$nodeValue)
}
$writer.WriteEndElement()
$rs.MoveNext()
}
$writer.WriteEndElement()
# Exiting the XmlWriter object
$writer.Flush()
$writer.Close()
# Read memoryStream with streamreader
$memoryStream.Position = 0
$streamReader = New-Object System.IO.StreamReader($memoryStream)
[string]$result = $streamReader.ReadToEnd()
}
}
end {
Get-CurrentVariables -InitialVariables $initialVariables -Debug:$DebugPreference
Return $result
}
# Test: Get-XmlEulandaAddress -filter "Match='EULANDA'" -select 'Match,Name1,Name2,Name3,Strasse,Plz,Ort' -alias 'addressMatch' -udl 'C:\temp\Eulanda_1 Eulanda.udl'
}