接続の確立
CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でQuickBase Cmdlets を使用する例を示します。
インストールおよび接続
PSGet がある場合は、PowerShell Gallery から次のコマンドを使ってcmdlet をインストールできます。CData サイトからセットアップを取得することもできます。
Install-Module QuickBaseCmdlets
プロファイルに以下を追加すると、次のセッションでcmdlet がロードされます。
Import-Module QuickBaseCmdlets;
Connect-QuickBase cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。
$conn = Connect-QuickBase -User "[email protected]" -Password "test_1234" -Domain "test.quickbase.com" -ApplicationToken "bwkxrb5da2wn57bzfh9xn24"
Authenticating to Quickbase
User Authentication
Set the AuthScheme to Basic to authenticate with this method.
To authenthenticate with user credentials, specify the following connection properties:
- Set the Quickbase User and Password.
- If your application requires an ApplicationToken, you must provide it otherwise an error will be thrown. You can find the ApplicationToken under MyAppName > Settings > App properties > Advanced settings > Security options > Require Application Tokens > Manage Application Token.
User Token
Set the AuthScheme to Token to authenticate with this method.
To authenthenticate with a user token, specify the following connection properties:
- Set UserToken and you are ready to connect. You can find the UserToken under Quick Base > My Preferences > My User Information > Manage User Tokens.
データの取得
Select-QuickBase cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。
$results = Select-QuickBase -Connection $conn -Table "[CData].[QuickBase].SampleTable_1" -Columns @("Id, Column1") -Where "Column2='Bob'"Invoke-QuickBase cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。
cmdlet 出力のパイプ処理
cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。
Select-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where "Column2 = 'Bob'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\my[CData].[QuickBase].SampleTable_1Data.csv -NoTypeInformation
Select-QuickBase からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。
ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。
PS C:\> $conn = Connect-QuickBase -User "[email protected]" -Password "test_1234" -Domain "test.quickbase.com" -ApplicationToken "bwkxrb5da2wn57bzfh9xn24" PS C:\> $row = Select-QuickBase -Connection $conn -Table "[CData].[QuickBase].SampleTable_1" -Columns (Id, Column1) -Where "Column2 = 'Bob'" | select -first 1 PS C:\> $row | ConvertTo-Json { "Connection": { }, "Table": "[CData].[QuickBase].SampleTable_1", "Columns": [ ], "Id": "MyId", "Column1": "MyColumn1" }
データの削除
以下は、抽出条件に合うあらゆるレコードを削除します。
Select-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where "Column2 = 'Bob'" | Remove-QuickBase
データの変更
cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをQuickbase にロードします。
Import-Csv -Path C:\My[CData].[QuickBase].SampleTable_1Updates.csv | %{ $record = Select-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Where ("Id = `'"+$_.Id+"`'") if($record){ Update-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) -Where "Id = `'$_.Id`'" }else{ Add-QuickBase -Connection $conn -Table [CData].[QuickBase].SampleTable_1 -Columns @("Id","Column1") -Values @($_.Id, $_.Column1) } }