Ready to get started?

Download a free trial of the AlloyDB Data Provider to get started:

 Download Now

Learn more:

AlloyDB Icon AlloyDB ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with AlloyDB.

Automate AlloyDB Integration Tasks from PowerShell



Are you in search of a quick and easy way to access AlloyDB data from PowerShell? This article demonstrates how to utilize the AlloyDB Cmdlets for tasks like connecting to AlloyDB data, automating operations, downloading data, and more.

The CData Cmdlets for AlloyDB 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 AlloyDB.

PowerShell Cmdlets or ADO.NET Provider?

The Cmdlets are not only a PowerShell interface to AlloyDB, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete AlloyDB data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for AlloyDB. To access AlloyDB data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for AlloyDB.

Once you have acquired the necessary connection properties, accessing AlloyDB data in PowerShell can be enabled in three steps.

The following connection properties are usually required in order to connect to AlloyDB.

  • Server: The host name or IP of the server hosting the AlloyDB database.
  • User: The user which will be used to authenticate with the AlloyDB server.
  • Password: The password which will be used to authenticate with the AlloyDB server.

You can also optionally set the following:

  • Database: The database to connect to when connecting to the AlloyDB Server. If this is not set, the user's default database will be used.
  • Port: The port of the server hosting the AlloyDB database. This property is set to 5432 by default.

Authenticating with Standard Authentication

Standard authentication (using the user/password combination supplied earlier) is the default form of authentication.

No further action is required to leverage Standard Authentication to connect.

Authenticating with pg_hba.conf Auth Schemes

There are additional methods of authentication available which must be enabled in the pg_hba.conf file on the AlloyDB server.

Find instructions about authentication setup on the AlloyDB Server here.

Authenticating with MD5 Authentication

This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to md5.

Authenticating with SASL Authentication

This authentication method must be enabled by setting the auth-method in the pg_hba.conf file to scram-sha-256.

Authenticating with Kerberos

The authentication with Kerberos is initiated by AlloyDB Server when the ∏ is trying to connect to it. You should set up Kerberos on the AlloyDB Server to activate this authentication method. Once you have Kerberos authentication set up on the AlloyDB Server, see the Kerberos section of the help documentation for details on how to authenticate with Kerberos.

PowerShell

  1. Install the module:

    Install-Module AlloyDBCmdlets
  2. Connect:

    $alloydb = Connect-AlloyDB -User "$User" -Password "$Password" -Database "$Database" -Server "$Server" -Port "$Port"
  3. Search for and retrieve data:

    $shipcountry = "USA" $orders = Select-AlloyDB -Connection $alloydb -Table "Orders" -Where "ShipCountry = `'$ShipCountry`'" $orders

    You can also use the Invoke-AlloyDB cmdlet to execute SQL commands:

    $orders = Invoke-AlloyDB -Connection $alloydb -Query 'SELECT * FROM Orders WHERE ShipCountry = @ShipCountry' -Params @{'@ShipCountry'='USA'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.AlloyDB.AlloyDBConnection("User=alloydb;Password=admin;Database=alloydb;Server=127.0.0.1;Port=5432") $conn.Open()
  3. Instantiate the AlloyDBDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT ShipName, ShipCity from Orders" $da= New-Object System.Data.CData.AlloyDB.AlloyDBDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.shipname $_.shipcity }

Update AlloyDB Data

PowerShell

Update-AlloyDB -Connection $AlloyDB -Columns @('ShipName','ShipCity') -Values @('MyShipName', 'MyShipCity') -Table Orders -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.AlloyDB.AlloyDBCommand("UPDATE Orders SET ShipCountry='USA' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.AlloyDB.AlloyDBParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert AlloyDB Data

PowerShell

Add-AlloyDB -Connection $AlloyDB -Table Orders -Columns @("ShipName", "ShipCity") -Values @("MyShipName", "MyShipCity")

ADO.NET

$cmd = New-Object System.Data.CData.AlloyDB.AlloyDBCommand("INSERT INTO Orders (ShipCountry) VALUES (@myShipCountry)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.AlloyDB.AlloyDBParameter("@myShipCountry","USA"))) $cmd.ExecuteNonQuery()

Delete AlloyDB Data

PowerShell

Remove-AlloyDB -Connection $AlloyDB -Table "Orders" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.AlloyDB.AlloyDBCommand("DELETE FROM Orders WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.AlloyDB.AlloyDBParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()