Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oracle update #4

Merged
merged 3 commits into from
Feb 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions PSTK.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
RootModule = 'PSTK.psm1'

# Version number of this module.s
ModuleVersion = '1.2.2'
ModuleVersion = '1.2.3'

# Supported PSEditions
# CompatiblePSEditions = @()
Expand Down Expand Up @@ -94,6 +94,7 @@ FunctionsToExport = @(
"Import-CSVProperties",
"Import-Function",
"Import-Properties",
"Invoke-OracleCmd",
"New-DynamicParameter",
"Out-Hashtable",
"Protect-WindowsCmdValue",
Expand All @@ -114,6 +115,7 @@ FunctionsToExport = @(
"Test-EnvironmentVariable",
"Test-HTTPStatus",
"Test-Object",
"Test-OracleConnection",
"Test-Service",
"Test-SQLConnection",
"Update-File",
Expand Down Expand Up @@ -161,10 +163,8 @@ PrivateData = @{

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

} # End of PSData hashtable
Expand Down
56 changes: 27 additions & 29 deletions Public/Compare-Version.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ function Compare-Version {
File name: Compare-Version.ps1
Author: Florian Carrier
Creation date: 19/10/2019
Last modified: 19/10/2019
Last modified: 10/02/2020
WARNING In case of modified formatting, Compare-Version only checks the semantic versionned part
#>
[CmdletBinding (
SupportsShouldProcess = $true
Expand Down Expand Up @@ -80,13 +81,13 @@ function Compare-Version {
"semantic" {
# Prepare version numbers for comparison
try {
$VersionNumber = [System.Version]::Parse($Version)
$VersionNumber = [System.Version]::Parse($Version)
} catch [FormatException] {
Write-Log -Type "ERROR" -Object "The version number ""$Version"" does not match $Format numbering"
return $false
}
try {
$ReferenceNumber = [System.Version]::Parse($Reference)
$ReferenceNumber = [System.Version]::Parse($Reference)
} catch [FormatException] {
Write-Log -Type "ERROR" -Object "The version number ""$Reference"" does not match $Format numbering"
return $false
Expand All @@ -101,36 +102,33 @@ function Compare-Version {
}
"modified" {
if ($Operator -in ("eq", "ne")) {
# Build comparison command
$Command = """$Version"" -$Operator ""$Reference"""
Write-Log -Type "DEBUG" -Object $Command
# Execute comparison
$Result = Invoke-Expression -Command $Command
# Return comparison result
return $Result
# Compare strings as-is
$VersionNumber = $Version
$ReferenceNumber = $Reference
} else {
# Parse version numbers
$VersionNumbers = $Version.Split(".")
$ReferenceNumbers = $Reference.Split(".")
# Check comparison operator
if ($Operator -in ("gt", "ge")) {
# TODO implement
# for ($i = 0; $i -lt $Count; $i++) {
# if ($i -lt ($Count - 1)) {
# $Command = """$($VersionNumbers[$i])"" -ge ""$($ReferenceNumbers[$i])"""
# } else {
# $Command = """$($VersionNumbers[$i])"" -Operator ""$($ReferenceNumbers[$i])"""
# }
# Write-Log -Type "DEBUG" -Object $Command
# $Result = Invoke-Expression -Command $Command
# if ($Result -eq $false) {
# return $false
# }
# }
} elseif ($Operator -in ("lt", "le")) {
# TODO implement
$SemanticVersion = Select-String -InputObject $Version -Pattern '(\d+.\d+.\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
try {
$VersionNumber = [System.Version]::Parse($SemanticVersion)
} catch [FormatException] {
Write-Log -Type "ERROR" -Object "The version number ""$Version"" does not match semantic numbering"
return $false
}
$SemanticReference = Select-String -InputObject $Reference -Pattern '(\d+.\d+.\d+)(?=\D*)' | ForEach-Object { $_.Matches.Value }
try {
$ReferenceNumber = [System.Version]::Parse($SemanticReference)
} catch [FormatException] {
Write-Log -Type "ERROR" -Object "The version number ""$Reference"" does not match semantic numbering"
return $false
}
}
# Build comparison command
$Command = """$VersionNumber"" -$Operator ""$ReferenceNumber"""
Write-Log -Type "DEBUG" -Object $Command
# Execute comparison
$Result = Invoke-Expression -Command $Command
# Return comparison result
return $Result
}
default {
Write-Log -Type "ERROR" -Object "The $Format versionning format is not yet supported"
Expand Down
192 changes: 192 additions & 0 deletions Public/Invoke-OracleCmd.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
function Invoke-OracleCmd {
<#
.SYNOPSIS
Invoke Oracle SQL command

.DESCRIPTION
Run a SQL command on an Oracle database

.NOTES
File name: Invoke-OracleCmd.ps1
Author: Florian Carrier
Creation date: 04/02/2020
Last modified: 06/02/2020
Dependencies: Invoke-OracleCmd requires Oracle Data Provider for .NET

.LINK
https://www.powershellgallery.com/packages/PSTK

.LINK
Invoke-SqlCmd

.LINK
https://www.oracle.com/database/technologies/appdev/dotnet/odp.html

.LINK
https://www.nuget.org/packages/Oracle.ManagedDataAccess.Core
#>
[CmdletBinding (
SupportsShouldProcess = $true
)]
Param (
[Parameter (
Position = 1,
Mandatory = $true,
HelpMessage = "Name of the database host"
)]
[ValidateNotNullOrEmpty ()]
[Alias ("Server")]
[System.String]
$Hostname,
[Parameter (
Position = 2,
Mandatory = $true,
HelpMessage = "Database server port number"
)]
[ValidateNotNullOrEmpty ()]
[System.String]
$PortNumber,
[Parameter (
Position = 3,
Mandatory = $true,
HelpMessage = "Name of the Oracle service"
)]
[ValidateNotNullOrEmpty ()]
[System.String]
$ServiceName,
[Parameter (
Position = 4,
Mandatory = $true,
HelpMessage = "SQL query"
)]
[ValidateNotNullOrEmpty ()]
[System.String]
$Query,
[Parameter (
Position = 5,
Mandatory = $false,
HelpMessage = "Database user credentials",
ParameterSetName = "Credentials"
)]
[ValidateNotNullOrEmpty ()]
[System.Management.Automation.PSCredential]
$Credentials,
[Parameter (
Position = 5,
Mandatory = $false,
HelpMessage = "User name",
ParameterSetName = "UserPassword"
)]
[ValidateNotNullOrEmpty ()]
[Alias ("Name")]
[System.String]
$Username,
[Parameter (
Position = 6,
Mandatory = $false,
HelpMessage = "Password",
ParameterSetName = "UserPassword"
)]
[ValidateNotNullOrEmpty ()]
[Alias ("Pw")]
[System.String]
$Password,
[Parameter (
Mandatory = $false,
HelpMessage = "Connection timeout (in seconds)"
)]
[ValidateNotNullOrEmpty ()]
[System.Int32]
$ConnectionTimeOut,
[Parameter (
Mandatory = $false,
HelpMessage = "Query timeout (in seconds)"
)]
[ValidateNotNullOrEmpty ()]
[System.Int32]
$QueryTimeOut,
[Parameter (
HelpMessage = "Abort on error"
)]
[Switch]
$AbortOnError,
[Parameter (
HelpMessage = "Encrypt connection"
)]
[Switch]
$EncryptConnection,
[Parameter (
HelpMessage = "Include SQL user errors"
)]
[Switch]
$IncludeSqlUserErrors,
[Parameter (
HelpMessage = "Out SQL errors"
)]
[Switch]
$OutputSqlErrors
)
Begin {
# Get global preference variables
Get-CallerPreference -Cmdlet $PSCmdlet -SessionState $ExecutionContext.SessionState
}
Process {
# Define connection string
$ConnectionString = "Data Source='(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=$Hostname)(PORT=$PortNumber))(CONNECT_DATA=(SERVICE_NAME=$ServiceName)))';"
# Check authentication mode
if ($PSBoundParameters.ContainsKey("Credentials")) {
# If "secured" credentials are provided
$ConnectionString = $ConnectionString + "User ID=$($Credentials.Username);Password=$($Credentials.GetNetworkCredential().Password);"
$SensitiveData = $Credentials.GetNetworkCredential().Password
} elseif ($PSBoundParameters.ContainsKey("Username") -And $PSBoundParameters.ContainsKey("Password")) {
# If plain text credentials are provided
if ($Username) {
$ConnectionString = $ConnectionString + "User ID=$Username;Password=$Password;"
$SensitiveData = $Password
} else {
Write-Log -Type "ERROR" -Message "Invalid username ""$Username""" -ExitCode 1
}
} else {
# Else default to integrated security (Windows authentication)
Write-Log -Type "DEBUG" -Message "Using Integrated Security"
$ConnectionString = $ConnectionString + "Integrated Security=True;"
}
# Technical parameters (Min Pool Size=10;Connection Lifetime=120;Connection Timeout=60;Incr Pool Size=5;Decr Pool Size=2;)
if ($PSBoundParameters.ContainsKey("ConnectionTimeOut") -And $ConnectionTimeOut -ne $null) {
$ConnectionString = $ConnectionString + "Connection Timeout=$ConnectionTimeOut;"
}
# Create connection object
Write-Log -Type "DEBUG" -Object $ConnectionString -Obfuscate $SensitiveData
$Connection = New-Object -TypeName "Oracle.ManagedDataAccess.Client.OracleConnection" -ArgumentList $ConnectionString
# Try to open the connection
try {
$Connection.Open()
} catch {
Write-Log -Type "ERROR" -Object "Unable to reach database $($Hostname):$PortNumber/$ServiceName"
return $Error
}
# Create SQL command
$Command = $Connection.CreateCommand()
# TODO sanitize query
$Command.CommandText = $Query
Write-Log -Type "DEBUG" -Object $Command
# Execute command
try {
$Reader = $Command.ExecuteReader()
} catch {
Write-Log -Type "ERROR" -Object "Could not execute statement`n$Query"
return $Error
}
# Get result
$Result = $Reader.Read()
# Close connection
$Connection.Close()
# Check outcome
if ($Result) {
# TODO return actual result
return $Result
} else {
return $null
}
}
}
Loading