Ready to get started?

Learn more about the CData ADO.NET Provider for Oracle or download a free trial:

Download Now

Automate Oracle Integration Tasks from PowerShell

Are you looking for a quick and easy way to access Oracle data from PowerShell? We show how to use the Cmdlets for Oracle and the CData ADO.NET Provider for Oracle to connect to Oracle data and synchronize, automate, download, 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.

Cmdlets or ADO.NET?

The cmdlets are not only a PowerShell interface to the Oracle API, 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.

After obtaining the needed connection properties, accessing Oracle data in PowerShell consists of three basic 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

  1. Install the module:

    Install-Module OracleOCICmdlets
  2. Connect:

    $oracleoci = Connect-OracleOCI -User "$User" -Password "$Password" -Server "$Server" -Port "$Port"
  3. 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

  1. Load the provider's assembly:

    [Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Oracle\lib\System.Data.CData.OracleOCI.dll")
  2. Connect to Oracle:

    $conn= New-Object System.Data.CData.OracleOCI.OracleOCIConnection("User=myuser;Password=mypassword;Server=localhost;Port=1521;") $conn.Open()
  3. 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 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 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 System.Data.CData.OracleOCI.OracleOCIParameter("@myId","001d000000YBRseAAH")) $cmd.ExecuteNonQuery()