Establishing a Connection
With the CData Cmdlets users can install a data module, set the connection properties, and start scripting. This section provides examples of using our IBMCloudObjectStorage Cmdlets with native PowerShell cmdlets, like the CSV import and export cmdlets.
Installing and Connecting
If you have PSGet, installing the cmdlets can be accomplished from the PowerShell Gallery with the following command. You can also obtain a setup from the CData site.
Install-Module IBMCloudObjectStorageCmdlets
The following line is then added to your profile, loading the cmdlets on the next session:
Import-Module IBMCloudObjectStorageCmdlets;
You can then use the Connect-IBMCloudObjectStorage cmdlet to create a connection object that can be passed to other cmdlets:
$conn = Connect-IBMCloudObjectStorage -ApiKey "MyAPIKey" -CloudObjectStorageCRN "myInstanceCRN" -Region "myRegion" -OAuthClientId "myOAuthClientId" -OAuthClientSecret "myOAuthClientSecret"
Connecting to IBM Cloud Object Storage
To connect to IBM Cloud Object Storage data, set these parameters:- ApiKey: The API key which was noted during setup (see Before You Connect).
- CloudObjectStorageCRN (Optional): The cloud object storage CRN established during setup (see Before You Connect). The cmdlet attempts to retrieve this automatically, but if you have more than one Cloud Object Storage account, we recommend that you specify this explicitly.
When you connect, the cmdlet completes the OAuth process.
Retrieving Data
The Select-IBMCloudObjectStorage cmdlet provides a native PowerShell interface for retrieving data:
$results = Select-IBMCloudObjectStorage -Connection $conn -Table "Objects" -Columns @("Key, Etag") -Where "Bucket='testBucket'"The Invoke-IBMCloudObjectStorage cmdlet provides an SQL interface. This cmdlet can be used to execute an SQL query via the Query parameter.
Piping Cmdlet Output
The cmdlets return row objects to the pipeline one row at a time. The following line exports results to a CSV file:
Select-IBMCloudObjectStorage -Connection $conn -Table Objects -Where "Bucket = 'testBucket'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myObjectsData.csv -NoTypeInformation
You will notice that we piped the results from Select-IBMCloudObjectStorage into a Select-Object cmdlet and excluded some properties before piping them into an Export-CSV cmdlet. We do this because the CData Cmdlets append Connection, Table, and Columns information onto each row object in the result set, and we do not necessarily want that information in our CSV file.
However, this makes it easy to pipe the output of one cmdlet to another. The following is an example of converting a result set to JSON:
PS C:\> $conn = Connect-IBMCloudObjectStorage -ApiKey "MyAPIKey" -CloudObjectStorageCRN "myInstanceCRN" -Region "myRegion" -OAuthClientId "myOAuthClientId" -OAuthClientSecret "myOAuthClientSecret" PS C:\> $row = Select-IBMCloudObjectStorage -Connection $conn -Table "Objects" -Columns (Key, Etag) -Where "Bucket = 'testBucket'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "Objects", "Columns": [ ], "Key": "MyKey", "Etag": "MyEtag" }
Modifying Data
The cmdlets make data transformation easy as well as data cleansing. The following example loads data from a CSV file into IBM Cloud Object Storage, checking first whether a record already exists and needs to be updated instead of inserted.
Import-Csv -Path C:\MyObjectsUpdates.csv | %{ $record = Select-IBMCloudObjectStorage -Connection $conn -Table Objects -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-IBMCloudObjectStorage -Connection $conn -Table Objects -Columns @("Key","Etag") -Values @($_.Key, $_.Etag) -Where "Id = `'$_.Id`'" }else{ Add-IBMCloudObjectStorage -Connection $conn -Table Objects -Columns @("Key","Etag") -Values @($_.Key, $_.Etag) } }