Cmdlets for Azure Table Storage

Build 25.0.9434

接続の確立

CData Cmdlets ユーザーは、データモジュールをインストールし、接続プロパティを設定してスクリプトを開始できます。このセクションでは、CSV インポートおよびエクスポートcmdlet などのネイティブPowerShell cmdlet でAzureTables Cmdlets を使用する例を示します。

Connecting to Azure Table Storage APIs

The driver will connect to the Azure Table Storage account specified by Account. By default, connections to the Azure Table Storage are secured via SSL, though this can be controlled through UseSSL. The authentication method to the Azure Table Storage is determined by the AuthScheme property.

Authenticating to Azure Table Storage

The following mechanisms may be used to authenticate.

アクセスキー

Account プロパティをストレージアカウント名に設定し、ストレージアカウントのAccessKey を設定して接続します。これらの値を取得する方法は次のとおりです。

ストレージをBackend として使用している場合(デフォルト):

  1. [Azure]ポータルにログインし、左側の[サービス]メニューで[ストレージアカウント]を選択します。
  2. ストレージアカウントがない場合は、[追加]ボタンをクリックしてアカウントを作成します。
  3. 使用するストレージアカウントのリンクをクリックし、[設定]の[アクセスキー]を選択します。 [アクセスキー]ウィンドウには、ストレージアカウント名と本製品 で使用するキー(接続にはkey1またはkey2のどちらかを使用できます)が含まれています。これらのプロパティは、それぞれAccountAccessKey本製品 接続プロパティにマッピングされます。

CosmosDB をBackend として使用している場合:

  1. [Azure]ポータルにログインし、左側の[サービス]メニューで[Azure Cosmos DB]を選択します。
  2. 使用するCosmos DB アカウントのリンクをクリックし、[設定]の[接続文字列]を選択します。 [接続文字列]ウィンドウには、Cosmos DB アカウント名と本製品 で使用する主キーが含まれています。これらのプロパティは、それぞれAccountAccessKey本製品 接続プロパティにマッピングされます。

Shared Access Signature

Set Account to the storage account name and set the SharedAccessSignature to a valid signature of a resource to connect to. The SharedAccessSignature may be generated with a tool such as Azure Storage Explorer.

Typically when SharedAccessSignature is used, the specific table to work with must also be specified via the Tables connection property. If no table is specified, a table listing will be attemped, but may fail due to a lack of permissions.

接続オブジェクトの作成

Connect-Azure cmdlet を使って、別のcmdlet に渡すことができる接続オブジェクトを作成します。

$conn = Connect-Azure -AccessKey 'myAccessKey' -Account 'myAccountName'

データの取得

Select-Azure cmdlet はデータを取得するためのネイティブなPowerShell インターフェースを提供します。

$results = Select-Azure -Connection $conn -Table "NorthwindProducts" -Columns @("PartitionKey, Name") -Where "Industry='Floppy Disks'"
Invoke-Azure cmdlet はSQL インターフェースを提供します。このcmdlet を使うと、Query パラメータを介してSQL クエリを実行できます。

cmdlet 出力のパイプ処理

cmdlet は行オブジェクトをパイプラインに一度に一行ずつ返します。以下は、結果をCSV ファイルにエクスポートします。

Select-Azure -Connection $conn -Table NorthwindProducts -Where "Industry = 'Floppy Disks'" | Select -Property * -ExcludeProperty Connection,Table,Columns | Export-Csv -Path c:\myNorthwindProductsData.csv -NoTypeInformation

Select-Azure からの結果をSelect-Object cmdlet にパイプして、Export-CSV cmdlet にパイプする前にいくつかのプロパティを実行していることがわかるでしょう。これをする理由は、CData Cmdlets は接続、テーブル、およびカラムの情報を結果セットの各行オブジェクトに追加しますが、必ずしもその情報がCSV ファイルに必要ではないからです。

ただし、これによってcmdlet の出力を別のcmdlet にパイプすることが容易になります。以下に、結果セットをJSON に変換する例を示します。

 
PS C:\> $conn  = Connect-Azure -AccessKey 'myAccessKey' -Account 'myAccountName'
PS C:\> $row = Select-Azure -Connection $conn -Table "NorthwindProducts" -Columns (PartitionKey, Name) -Where "Industry = 'Floppy Disks'" | select -first 1
PS C:\> $row | ConvertTo-Json
{
  "Connection":  {

  },
  "Table":  "NorthwindProducts",
  "Columns":  [

  ],
  "PartitionKey":  "MyPartitionKey",
  "Name":  "MyName"
} 

データの削除

以下は、抽出条件に合うあらゆるレコードを削除します。

Select-Azure -Connection $conn -Table NorthwindProducts -Where "Industry = 'Floppy Disks'" | Remove-Azure

データの変更

cmdlet はデータクレンジング同様、データの変換を容易にします。次の例は、レコードがすでに存在するかどうか、挿入する前に更新が必要かどうかをチェックしてから、CSV ファイルのデータをAzure Table Storage にロードします。

Import-Csv -Path C:\MyNorthwindProductsUpdates.csv | %{
  $record = Select-Azure -Connection $conn -Table NorthwindProducts -Where ("Id = `'"+$_.Id+"`'")
  if($record){
    Update-Azure -Connection $conn -Table NorthwindProducts -Columns @("PartitionKey","Name") -Values @($_.PartitionKey, $_.Name) -Where "Id  = `'$_.Id`'"
  }else{
    Add-Azure -Connection $conn -Table NorthwindProducts -Columns @("PartitionKey","Name") -Values @($_.PartitionKey, $_.Name)
  }
}

Copyright (c) 2025 CData Software, Inc. - All rights reserved.
Build 25.0.9434