We are proud to share our inclusion in the 2024 Gartner Magic Quadrant for Data Integration Tools. We believe this recognition reflects the differentiated business outcomes CData delivers to our customers.
Get the Report →Automate Google Directory Integration Tasks from PowerShell
Are you in search of a quick and easy way to access Google Directory data from PowerShell? This article demonstrates how to utilize the Google Directory Cmdlets for tasks like connecting to Google Directory data, automating operations, downloading data, and more.
The CData Cmdlets for Google Directory 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 Directory.
PowerShell Cmdlets or ADO.NET Provider?
The Cmdlets are not only a PowerShell interface to Google Directory, but also an SQL interface; this tutorial shows how to use both to create, retrieve, update, and delete Google Directory data. We also show examples of the ADO.NET equivalent, which is possible with the CData ADO.NET Provider for Google Directory. To access Google Directory data from other .NET applications, like LINQPad, use the CData ADO.NET Provider for Google Directory.
Once you have acquired the necessary connection properties, accessing Google Directory data in PowerShell can be enabled in three steps.
Google uses the OAuth authentication standard. You can authorize the data provider to access Google Spreadsheets as an individual user or with a Google Apps Domain service account. See the Getting Started section of the data provider help documentation for an authentication guide.
PowerShell
-
Install the module:
Install-Module GoogleDirectoryCmdlets
-
Connect:
$googledirectory = Connect-GoogleDirectory -OAuthClientId "$OAuthClientId" -OAuthClientSecret "$OAuthClientSecret" -CallbackURL "$CallbackURL"
-
Search for and retrieve data:
$status = "confirmed" $mytable = Select-GoogleDirectory -Connection $googledirectory -Table "MyTable" -Where "Status = `'$Status`'" $mytable
You can also use the Invoke-GoogleDirectory cmdlet to execute SQL commands:
$mytable = Invoke-GoogleDirectory -Connection $googledirectory -Query 'SELECT * FROM MyTable WHERE Status = @Status' -Params @{'@Status'='confirmed'}
ADO.NET
-
Load the provider's assembly:
[Reflection.Assembly]::LoadFile("C:\Program Files\CData\CData ADO.NET Provider for Google Directory\lib\System.Data.CData.GoogleDirectory.dll")
-
Connect to Google Directory:
$conn= New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryConnection("OAuthClientId=MyOAuthClientId;OAuthClientSecret=MyOAuthClientSecret;CallbackURL=http://localhost;InitiateOAuth=GETANDREFRESH") $conn.Open()
-
Instantiate the GoogleDirectoryDataAdapter, execute an SQL query, and output the results:
$sql="SELECT Id, Description from MyTable" $da= New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryDataAdapter($sql, $conn) $dt= New-Object System.Data.DataTable $da.Fill($dt) $dt.Rows | foreach { Write-Host $_.id $_.description }
Update Google Directory Data
PowerShell
Update-GoogleDirectory -Connection $GoogleDirectory -Columns @('Id','Description') -Values @('MyId', 'MyDescription') -Table MyTable -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryCommand("UPDATE MyTable SET Status='confirmed' WHERE Id = @myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryParameter("@myId","10456255-0015501366")))
$cmd.ExecuteNonQuery()
Insert Google Directory Data
PowerShell
Add-GoogleDirectory -Connection $GoogleDirectory -Table MyTable -Columns @("Id", "Description") -Values @("MyId", "MyDescription")
ADO.NET
$cmd = New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryCommand("INSERT INTO MyTable (Status) VALUES (@myStatus)", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryParameter("@myStatus","confirmed")))
$cmd.ExecuteNonQuery()
Delete Google Directory Data
PowerShell
Remove-GoogleDirectory -Connection $GoogleDirectory -Table "MyTable" -Id "MyId"
ADO.NET
$cmd = New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryCommand("DELETE FROM MyTable WHERE Id=@myId", $conn)
$cmd.Parameters.Add((New-Object System.Data.CData.GoogleDirectory.GoogleDirectoryParameter("@myId","001d000000YBRseAAH")))
$cmd.ExecuteNonQuery()
CodeProject