Skip to content

Commit a01570b

Browse files
authored
Merge pull request #4 from Akaizoku/develop
Oracle update
2 parents 2a591ce + df8deb2 commit a01570b

5 files changed

+492
-71
lines changed

PSTK.psd1

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
RootModule = 'PSTK.psm1'
1313

1414
# Version number of this module.s
15-
ModuleVersion = '1.2.2'
15+
ModuleVersion = '1.2.3'
1616

1717
# Supported PSEditions
1818
# CompatiblePSEditions = @()
@@ -94,6 +94,7 @@ FunctionsToExport = @(
9494
"Import-CSVProperties",
9595
"Import-Function",
9696
"Import-Properties",
97+
"Invoke-OracleCmd",
9798
"New-DynamicParameter",
9899
"Out-Hashtable",
99100
"Protect-WindowsCmdValue",
@@ -114,6 +115,7 @@ FunctionsToExport = @(
114115
"Test-EnvironmentVariable",
115116
"Test-HTTPStatus",
116117
"Test-Object",
118+
"Test-OracleConnection",
117119
"Test-Service",
118120
"Test-SQLConnection",
119121
"Update-File",
@@ -161,10 +163,8 @@ PrivateData = @{
161163

162164
# ReleaseNotes of this module
163165
ReleaseNotes = @'
164-
[1.2.2]
165-
- Added new features
166-
- Redesign Resolve-Boolean
167-
- Fixed an issue with Resolve-URI causing it to only resolve the last restricted character of the list
166+
[1.2.3]
167+
- Added Oracle database utility functions
168168
'@
169169

170170
} # End of PSData hashtable

Public/Compare-Version.ps1

+27-29
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ function Compare-Version {
2222
File name: Compare-Version.ps1
2323
Author: Florian Carrier
2424
Creation date: 19/10/2019
25-
Last modified: 19/10/2019
25+
Last modified: 10/02/2020
26+
WARNING In case of modified formatting, Compare-Version only checks the semantic versionned part
2627
#>
2728
[CmdletBinding (
2829
SupportsShouldProcess = $true
@@ -80,13 +81,13 @@ function Compare-Version {
8081
"semantic" {
8182
# Prepare version numbers for comparison
8283
try {
83-
$VersionNumber = [System.Version]::Parse($Version)
84+
$VersionNumber = [System.Version]::Parse($Version)
8485
} catch [FormatException] {
8586
Write-Log -Type "ERROR" -Object "The version number ""$Version"" does not match $Format numbering"
8687
return $false
8788
}
8889
try {
89-
$ReferenceNumber = [System.Version]::Parse($Reference)
90+
$ReferenceNumber = [System.Version]::Parse($Reference)
9091
} catch [FormatException] {
9192
Write-Log -Type "ERROR" -Object "The version number ""$Reference"" does not match $Format numbering"
9293
return $false
@@ -101,36 +102,33 @@ function Compare-Version {
101102
}
102103
"modified" {
103104
if ($Operator -in ("eq", "ne")) {
104-
# Build comparison command
105-
$Command = """$Version"" -$Operator ""$Reference"""
106-
Write-Log -Type "DEBUG" -Object $Command
107-
# Execute comparison
108-
$Result = Invoke-Expression -Command $Command
109-
# Return comparison result
110-
return $Result
105+
# Compare strings as-is
106+
$VersionNumber = $Version
107+
$ReferenceNumber = $Reference
111108
} else {
112109
# Parse version numbers
113-
$VersionNumbers = $Version.Split(".")
114-
$ReferenceNumbers = $Reference.Split(".")
115-
# Check comparison operator
116-
if ($Operator -in ("gt", "ge")) {
117-
# TODO implement
118-
# for ($i = 0; $i -lt $Count; $i++) {
119-
# if ($i -lt ($Count - 1)) {
120-
# $Command = """$($VersionNumbers[$i])"" -ge ""$($ReferenceNumbers[$i])"""
121-
# } else {
122-
# $Command = """$($VersionNumbers[$i])"" -Operator ""$($ReferenceNumbers[$i])"""
123-
# }
124-
# Write-Log -Type "DEBUG" -Object $Command
125-
# $Result = Invoke-Expression -Command $Command
126-
# if ($Result -eq $false) {
127-
# return $false
128-
# }
129-
# }
130-
} elseif ($Operator -in ("lt", "le")) {
131-
# TODO implement
110+
$SemanticVersion = Select-String -InputObject $Version -Pattern '(\d+.\d+.\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
111+
try {
112+
$VersionNumber = [System.Version]::Parse($SemanticVersion)
113+
} catch [FormatException] {
114+
Write-Log -Type "ERROR" -Object "The version number ""$Version"" does not match semantic numbering"
115+
return $false
116+
}
117+
$SemanticReference = Select-String -InputObject $Reference -Pattern '(\d+.\d+.\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
118+
try {
119+
$ReferenceNumber = [System.Version]::Parse($SemanticReference)
120+
} catch [FormatException] {
121+
Write-Log -Type "ERROR" -Object "The version number ""$Reference"" does not match semantic numbering"
122+
return $false
132123
}
133124
}
125+
# Build comparison command
126+
$Command = """$VersionNumber"" -$Operator ""$ReferenceNumber"""
127+
Write-Log -Type "DEBUG" -Object $Command
128+
# Execute comparison
129+
$Result = Invoke-Expression -Command $Command
130+
# Return comparison result
131+
return $Result
134132
}
135133
default {
136134
Write-Log -Type "ERROR" -Object "The $Format versionning format is not yet supported"

Public/Invoke-OracleCmd.ps1

+192
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
function Invoke-OracleCmd {
2+
<#
3+
.SYNOPSIS
4+
Invoke Oracle SQL command
5+
6+
.DESCRIPTION
7+
Run a SQL command on an Oracle database
8+
9+
.NOTES
10+
File name: Invoke-OracleCmd.ps1
11+
Author: Florian Carrier
12+
Creation date: 04/02/2020
13+
Last modified: 06/02/2020
14+
Dependencies: Invoke-OracleCmd requires Oracle Data Provider for .NET
15+
16+
.LINK
17+
https://www.powershellgallery.com/packages/PSTK
18+
19+
.LINK
20+
Invoke-SqlCmd
21+
22+
.LINK
23+
https://www.oracle.com/database/technologies/appdev/dotnet/odp.html
24+
25+
.LINK
26+
https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core
27+
#>
28+
[CmdletBinding (
29+
SupportsShouldProcess = $true
30+
)]
31+
Param (
32+
[Parameter (
33+
Position = 1,
34+
Mandatory = $true,
35+
HelpMessage = "Name of the database host"
36+
)]
37+
[ValidateNotNullOrEmpty ()]
38+
[Alias ("Server")]
39+
[System.String]
40+
$Hostname,
41+
[Parameter (
42+
Position = 2,
43+
Mandatory = $true,
44+
HelpMessage = "Database server port number"
45+
)]
46+
[ValidateNotNullOrEmpty ()]
47+
[System.String]
48+
$PortNumber,
49+
[Parameter (
50+
Position = 3,
51+
Mandatory = $true,
52+
HelpMessage = "Name of the Oracle service"
53+
)]
54+
[ValidateNotNullOrEmpty ()]
55+
[System.String]
56+
$ServiceName,
57+
[Parameter (
58+
Position = 4,
59+
Mandatory = $true,
60+
HelpMessage = "SQL query"
61+
)]
62+
[ValidateNotNullOrEmpty ()]
63+
[System.String]
64+
$Query,
65+
[Parameter (
66+
Position = 5,
67+
Mandatory = $false,
68+
HelpMessage = "Database user credentials",
69+
ParameterSetName = "Credentials"
70+
)]
71+
[ValidateNotNullOrEmpty ()]
72+
[System.Management.Automation.PSCredential]
73+
$Credentials,
74+
[Parameter (
75+
Position = 5,
76+
Mandatory = $false,
77+
HelpMessage = "User name",
78+
ParameterSetName = "UserPassword"
79+
)]
80+
[ValidateNotNullOrEmpty ()]
81+
[Alias ("Name")]
82+
[System.String]
83+
$Username,
84+
[Parameter (
85+
Position = 6,
86+
Mandatory = $false,
87+
HelpMessage = "Password",
88+
ParameterSetName = "UserPassword"
89+
)]
90+
[ValidateNotNullOrEmpty ()]
91+
[Alias ("Pw")]
92+
[System.String]
93+
$Password,
94+
[Parameter (
95+
Mandatory = $false,
96+
HelpMessage = "Connection timeout (in seconds)"
97+
)]
98+
[ValidateNotNullOrEmpty ()]
99+
[System.Int32]
100+
$ConnectionTimeOut,
101+
[Parameter (
102+
Mandatory = $false,
103+
HelpMessage = "Query timeout (in seconds)"
104+
)]
105+
[ValidateNotNullOrEmpty ()]
106+
[System.Int32]
107+
$QueryTimeOut,
108+
[Parameter (
109+
HelpMessage = "Abort on error"
110+
)]
111+
[Switch]
112+
$AbortOnError,
113+
[Parameter (
114+
HelpMessage = "Encrypt connection"
115+
)]
116+
[Switch]
117+
$EncryptConnection,
118+
[Parameter (
119+
HelpMessage = "Include SQL user errors"
120+
)]
121+
[Switch]
122+
$IncludeSqlUserErrors,
123+
[Parameter (
124+
HelpMessage = "Out SQL errors"
125+
)]
126+
[Switch]
127+
$OutputSqlErrors
128+
)
129+
Begin {
130+
# Get global preference variables
131+
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
132+
}
133+
Process {
134+
# Define connection string
135+
$ConnectionString = "Data Source='(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$Hostname)(PORT=$PortNumber))(CONNECT_DATA=(SERVICE_NAME=$ServiceName)))';"
136+
# Check authentication mode
137+
if ($PSBoundParameters.ContainsKey("Credentials")) {
138+
# If "secured" credentials are provided
139+
$ConnectionString = $ConnectionString + "User ID=$($Credentials.Username);Password=$($Credentials.GetNetworkCredential().Password);"
140+
$SensitiveData = $Credentials.GetNetworkCredential().Password
141+
} elseif ($PSBoundParameters.ContainsKey("Username") -And $PSBoundParameters.ContainsKey("Password")) {
142+
# If plain text credentials are provided
143+
if ($Username) {
144+
$ConnectionString = $ConnectionString + "User ID=$Username;Password=$Password;"
145+
$SensitiveData = $Password
146+
} else {
147+
Write-Log -Type "ERROR" -Message "Invalid username ""$Username""" -ExitCode 1
148+
}
149+
} else {
150+
# Else default to integrated security (Windows authentication)
151+
Write-Log -Type "DEBUG" -Message "Using Integrated Security"
152+
$ConnectionString = $ConnectionString + "Integrated Security=True;"
153+
}
154+
# Technical parameters (Min Pool Size=10;Connection Lifetime=120;Connection Timeout=60;Incr Pool Size=5;Decr Pool Size=2;)
155+
if ($PSBoundParameters.ContainsKey("ConnectionTimeOut") -And $ConnectionTimeOut -ne $null) {
156+
$ConnectionString = $ConnectionString + "Connection Timeout=$ConnectionTimeOut;"
157+
}
158+
# Create connection object
159+
Write-Log -Type "DEBUG" -Object $ConnectionString -Obfuscate $SensitiveData
160+
$Connection = New-Object -TypeName "Oracle.ManagedDataAccess.Client.OracleConnection" -ArgumentList $ConnectionString
161+
# Try to open the connection
162+
try {
163+
$Connection.Open()
164+
} catch {
165+
Write-Log -Type "ERROR" -Object "Unable to reach database $($Hostname):$PortNumber/$ServiceName"
166+
return $Error
167+
}
168+
# Create SQL command
169+
$Command = $Connection.CreateCommand()
170+
# TODO sanitize query
171+
$Command.CommandText = $Query
172+
Write-Log -Type "DEBUG" -Object $Command
173+
# Execute command
174+
try {
175+
$Reader = $Command.ExecuteReader()
176+
} catch {
177+
Write-Log -Type "ERROR" -Object "Could not execute statement`n$Query"
178+
return $Error
179+
}
180+
# Get result
181+
$Result = $Reader.Read()
182+
# Close connection
183+
$Connection.Close()
184+
# Check outcome
185+
if ($Result) {
186+
# TODO return actual result
187+
return $Result
188+
} else {
189+
return $null
190+
}
191+
}
192+
}

0 commit comments

Comments
 (0)