![]() |
VOOZH | about |
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.
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:
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).
From your Bitbucket account:
Install the module:
Install-Module BitbucketCmdlets
Connect:
$bitbucket = Connect-Bitbucket -Workspace "$Workspace" -Schema "$Schema" -InitiateOAuth "$InitiateOAuth"
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'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Bitbucket\lib\System.Data.CData.Bitbucket.dll")
Connect to Bitbucket:
$conn= New-Object System.Data.CData.Bitbucket.BitbucketConnection("Workspace=myworkspaceslug;Schema=Information;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
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 -Connection $Bitbucket -Columns @('Title','ContentRaw') -Values @('MyTitle', 'MyContentRaw') -Table Issues -Id "MyId"
$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()
Add-Bitbucket -Connection $Bitbucket -Table Issues -Columns @("Title", "ContentRaw") -Values @("MyTitle", "MyContentRaw")
$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()
Remove-Bitbucket -Connection $Bitbucket -Table "Issues" -Id "MyId"
$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()
CodeProject
Download a free trial of the Bitbucket Data Provider to get started:
Download NowLearn more:
👁 Bitbucket IconRapidly create and deploy powerful .NET applications that integrate with Bitbucket.