We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →Automate Oracle Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Oracle data from PowerShell? This article demonstrates how to utilize the Oracle Cmdlets for tasks like connecting to Oracle data, automating operations, downloading data, and more.
The CData Cmdlets for Oracle are standard PowerShell cmdlets that make it easy to accomplish data cleansing, normalization, backup, and other integration tasks by enabling real-time and bidirectional access to Oracle.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Oracle, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Oracle data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Oracle. To access Oracle data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Oracle.
Once you have acquired the necessary connection properties, accessing Oracle data in PowerShell can be enabled in three steps.
To connect to Oracle, you'll first need to update your PATH variable and ensure it contains a folder location that includes the native DLLs. The native DLLs can be found in the lib folder inside the installation directory. Once you've done this, set the following to connect:
- Port: The port used to connect to the server hosting the Oracle database.
- User: The user Id provided for authentication with the Oracle database.
- Password: The password provided for authentication with the Oracle database.
- Service Name: The service name of the Oracle database.
PowerShell
-
Install the module:
Install-Module OracleOCICmdlets
-
Connect:
$oracleoci = Connect-OracleOCI -User "$User" -Password "$Password" -Server "$Server" -Port "$Port"
-
Search for and retrieve data:
$country = "US" $customers = Select-OracleOCI -Connection $oracleoci -Table "Customers" -Where "Country = `'$Country`'" $customers
You can also use the Invoke-OracleOCI cmdlet to execute SQL commands:
$customers = Invoke-OracleOCI -Connection $oracleoci -Query 'SELECT * FROM Customers WHERE Country = @Country' -Params @{'@Country'='US'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Oracle\lib\System.Data.CData.OracleOCI.dll")
-
Connect to Oracle:
$conn= New-Object System.Data.CData.OracleOCI.OracleOCIConnection("User=myuser;Password=mypassword;Server=localhost;Port=1521;") $conn.Open()
-
Instantiate the OracleOCIDataAdapter, execute an SQL query, and output the results:
$sql="SELECT CompanyName, City from Customers" $da= New-Object System.Data.CData.OracleOCI.OracleOCIDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.companyname $_.city }
Update Oracle Data
PowerShell
Update-OracleOCI -Connection $OracleOCI -Columns @('CompanyName','City') -Values @('MyCompanyName', 'MyCity') -Table Customers -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("UPDATE Customers SET Country='US' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Oracle Data
PowerShell
Add-OracleOCI -Connection $OracleOCI -Table Customers -Columns @("CompanyName", "City") -Values @("MyCompanyName", "MyCity")
ADO.NET
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("INSERT INTO Customers (Country) VALUES (@myCountry)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myCountry","US")))
$cmd.ExecuteNonQuery()
Delete Oracle Data
PowerShell
Remove-OracleOCI -Connection $OracleOCI -Table "Customers" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.OracleOCI.OracleOCICommand("DELETE FROM Customers WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.OracleOCI.OracleOCIParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject