|
| 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