Ready to get started?

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

 Download Now

Learn more:

Google Cloud Spanner Icon Google Spanner ADO.NET Provider

Rapidly create and deploy powerful .NET applications that integrate with Google Cloud Spanner databases.

Automate Google Spanner Integration Tasks from PowerShell



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

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

Google Spanner uses the OAuth authentication standard. To authenticate using OAuth, you can use the embedded credentials or register an app with Google.

See the Getting Started guide in the CData driver documentation for more information.

PowerShell

  1. Install the module:

    Install-Module GoogleSpannerCmdlets
  2. Connect:

    $googlespanner = Connect-GoogleSpanner -ProjectId "$ProjectId" -InstanceId "$InstanceId" -Database "$Database"
  3. Search for and retrieve data:

    $id = "1" $customer = Select-GoogleSpanner -Connection $googlespanner -Table "Customer" -Where "Id = `'$Id`'" $customer

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

    $customer = Invoke-GoogleSpanner -Connection $googlespanner -Query 'SELECT * FROM Customer WHERE Id = @Id' -Params @{'@Id'='1'}

ADO.NET

  1. Load the provider's assembly:

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

    $conn= New-Object System.Data.CData.GoogleSpanner.GoogleSpannerConnection("ProjectId='project1';InstanceId='instance1';Database='db1';InitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the GoogleSpannerDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Name, TotalDue from Customer" $da= New-Object System.Data.CData.GoogleSpanner.GoogleSpannerDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.name $_.totaldue }

Update Google Spanner Data

PowerShell

Update-GoogleSpanner -Connection $GoogleSpanner -Columns @('Name','TotalDue') -Values @('MyName', 'MyTotalDue') -Table Customer -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("UPDATE Customer SET Id='1' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Google Spanner Data

PowerShell

Add-GoogleSpanner -Connection $GoogleSpanner -Table Customer -Columns @("Name", "TotalDue") -Values @("MyName", "MyTotalDue")

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("INSERT INTO Customer (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","1"))) $cmd.ExecuteNonQuery()

Delete Google Spanner Data

PowerShell

Remove-GoogleSpanner -Connection $GoogleSpanner -Table "Customer" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.GoogleSpanner.GoogleSpannerCommand("DELETE FROM Customer WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.GoogleSpanner.GoogleSpannerParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()