![]() |
VOOZH | about |
The CData Cmdlets for GitHub 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 GitHub.
The Cmdlets are not only a PowerShell interface to GitHub, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete GitHub data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for GitHub. To access GitHub data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for GitHub.
Once you have acquired the necessary connection properties, accessing GitHub data in PowerShell can be enabled in three steps.
GitHub uses the OAuth 2 authentication standard. To authenticate using OAuth, create an app to obtain the OAuthClientId, OAuthClientSecret, and CallbackURL connection properties. See the Getting Started chapter of the CData help documentation for an authentication guide.
Install the module:
Install-Module GitHubCmdlets
Connect:
$github = Connect-GitHub -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL" -InitiateOAuth "$InitiateOAuth"
Search for and retrieve data:
$userlogin = "mojombo" $users = Select-GitHub -Connection $github -Table "Users" -Where "UserLogin = `'$UserLogin`'" $users
You can also use the Invoke-GitHub cmdlet to execute SQL commands:
$users = Invoke-GitHub -Connection $github -Query 'SELECT * FROM Users WHERE UserLogin = @UserLogin' -Params @{'@UserLogin'='mojombo'}
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for GitHub\lib\System.Data.CData.GitHub.dll")
Connect to GitHub:
$conn= New-Object System.Data.CData.GitHub.GitHubConnection("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost:portNumber;InitiateOAuth=GETANDREFRESH;")
$conn.Open()
Instantiate the GitHubDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Name, Email from Users"
$da= New-Object System.Data.CData.GitHub.GitHubDataAdapter($sql, $conn)
$dt= New-Object System.Data.DataTable
$da.Fill($dt)
$dt.Rows | foreach {
Write-Host $_.name $_.email
}
Update-GitHub -Connection $GitHub -Columns @('Name','Email') -Values @('MyName', 'MyEmail') -Table Users -Id "MyId"
$cmd = New-Object System.Data.CData.GitHub.GitHubCommand("UPDATE Users SET UserLogin='mojombo' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GitHub.GitHubParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Add-GitHub -Connection $GitHub -Table Users -Columns @("Name", "Email") -Values @("MyName", "MyEmail")
$cmd = New-Object System.Data.CData.GitHub.GitHubCommand("INSERT INTO Users (UserLogin) VALUES (@myUserLogin)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GitHub.GitHubParameter("@myUserLogin","mojombo")))
$cmd.ExecuteNonQuery()
Remove-GitHub -Connection $GitHub -Table "Users" -Id "MyId"
$cmd = New-Object System.Data.CData.GitHub.GitHubCommand("DELETE FROM Users WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GitHub.GitHubParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject
Download a free trial of the GitHub Data Provider to get started:
Download NowLearn more:
👁 GitHub IconRapidly create and deploy powerful .NET applications that integrate with GitHub.