Ready to get started?

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

 Download Now

Learn more:

Bitbucket Icon Bitbucket ADO.NET Provider

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

Automate Bitbucket Integration Tasks from PowerShell



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

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

PowerShell Cmdlets or ADO.NET Provider?

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

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

For most queries, you must set the Workspace. The only exception to this is the Workspaces table, which does not require this property to be set, as querying it provides a list of workspace slugs that can be used to set Workspace. To query this table, you must set Schema to 'Information' and execute the query SELECT * FROM Workspaces>.

Setting Schema to 'Information' displays general information. To connect to Bitbucket, set these parameters:

  • Schema: To show general information about a workspace, such as its users, repositories, and projects, set this to Information. Otherwise, set this to the schema of the repository or project you are querying. To get a full set of available schemas, query the sys_schemas table.
  • Workspace: Required if you are not querying the Workspaces table. This property is not required for querying the Workspaces table, as that query only returns a list of workspace slugs that can be used to set Workspace.

Authenticating to Bitbucket

Bitbucket supports OAuth authentication only. To enable this authentication from all OAuth flows, you must create a custom OAuth application, and set AuthScheme to OAuth.

Be sure to review the Help documentation for the required connection properties for you specific authentication needs (desktop applications, web applications, and headless machines).

Creating a custom OAuth application

From your Bitbucket account:

  1. Go to Settings (the gear icon) and select Workspace Settings.
  2. In the Apps and Features section, select OAuth Consumers.
  3. Click Add Consumer.
  4. Enter a name and description for your custom application.
  5. Set the callback URL:
    • For desktop applications and headless machines, use http://localhost:33333 or another port number of your choice. The URI you set here becomes the CallbackURL property.
    • For web applications, set the callback URL to a trusted redirect URL. This URL is the web location the user returns to with the token that verifies that your application has been granted access.
  6. If you plan to use client credentials to authenticate, you must select This is a private consumer. In the driver, you must set AuthScheme to client.
  7. Select which permissions to give your OAuth application. These determine what data you can read and write with it.
  8. To save the new custom application, click Save.
  9. After the application has been saved, you can select it to view its settings. The application's Key and Secret are displayed. Record these for future use. You will use the Key to set the OAuthClientId and the Secret to set the OAuthClientSecret.

PowerShell

  1. Install the module:

    Install-Module BitbucketCmdlets
  2. Connect:

    $bitbucket = Connect-Bitbucket -Workspace "$Workspace" -Schema "$Schema"
  3. Search for and retrieve data:

    $id = "1" $issues = Select-Bitbucket -Connection $bitbucket -Table "Issues" -Where "Id = `'$Id`'" $issues

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

    $issues = Invoke-Bitbucket -Connection $bitbucket -Query 'SELECT * FROM Issues 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 Bitbucket\lib\System.Data.CData.Bitbucket.dll")
  2. Connect to Bitbucket:

    $conn= New-Object System.Data.CData.Bitbucket.BitbucketConnection("Workspace=myworkspaceslug;Schema=InformationInitiateOAuth=GETANDREFRESH") $conn.Open()
  3. Instantiate the BitbucketDataAdapter, execute an SQL query, and output the results:

    $sql="SELECT Title, ContentRaw from Issues" $da= New-Object System.Data.CData.Bitbucket.BitbucketDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.title $_.contentraw }

Update Bitbucket Data

PowerShell

Update-Bitbucket -Connection $Bitbucket -Columns @('Title','ContentRaw') -Values @('MyTitle', 'MyContentRaw') -Table Issues -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Bitbucket.BitbucketCommand("UPDATE Issues SET Id='1' WHERE Id = @myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Bitbucket.BitbucketParameter("@myId","10456255-0015501366"))) $cmd.ExecuteNonQuery()

Insert Bitbucket Data

PowerShell

Add-Bitbucket -Connection $Bitbucket -Table Issues -Columns @("Title", "ContentRaw") -Values @("MyTitle", "MyContentRaw")

ADO.NET

$cmd = New-Object System.Data.CData.Bitbucket.BitbucketCommand("INSERT INTO Issues (Id) VALUES (@myId)", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Bitbucket.BitbucketParameter("@myId","1"))) $cmd.ExecuteNonQuery()

Delete Bitbucket Data

PowerShell

Remove-Bitbucket -Connection $Bitbucket -Table "Issues" -Id "MyId"

ADO.NET

$cmd = New-Object System.Data.CData.Bitbucket.BitbucketCommand("DELETE FROM Issues WHERE Id=@myId", $conn) $cmd.Parameters.Add((New-Object System.Data.CData.Bitbucket.BitbucketParameter("@myId","001d000000YBRseAAH"))) $cmd.ExecuteNonQuery()