質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

Azure

Azureは、マイクロソフトのクラウド プラットフォームで、旧称は Windows Azureです。PaaSとIaaSを組み合わせることで、 コンピューティング・ストレージ・データ・ネットワーキング・アプリケーションなど多くの機能を持ちます。

Q&A

解決済

2回答

4574閲覧

PowershellでAzureTableStorageのEntityを削除したい

Takayukky

総合スコア10

PowerShell

Windows PowerShellはコマンドラインインターフェースであり、システム管理を含むWindowsタスク自動化のためのスクリプト言語です。

Azure

Azureは、マイクロソフトのクラウド プラットフォームで、旧称は Windows Azureです。PaaSとIaaSを組み合わせることで、 コンピューティング・ストレージ・データ・ネットワーキング・アプリケーションなど多くの機能を持ちます。

1グッド

0クリップ

投稿2016/07/28 05:46

編集2016/07/29 03:06

log4netでAzureログをAzureStorageのWADLogsTableに吐き出しております。
私は古いログ(Entity)を削除したく、以下のスクリプトを作成し実行しました。
ところがエラーが発生しエンティティを削除できませんでした。

# 指定したAzureサブスクリプションを選択 Select-AzureSubscription -SubscriptionName $SubscriptionName Get-AzureSubscription # ストレージアカウントへの接続コンテキスト生成 $context = New-AzureStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccessKey # テーブル名 $tableName = "WADLogsTable" $table = Get-AzureStorageTable –Name $tableName -Context $context # TimeStampを使用してフィルターする $query = New-Object Microsoft.WindowsAzure.Storage.Table.TableQuery # フィルター設定 $query.FilterString = "Timestamp le datetime'2016-07-17T15:00:00'" # フィルター結果 $entities = $table.CloudTable.ExecuteQuery($query) if ($entities -ne $null){ Write-Output "START DELETE..." # フィルターして取得したエンティティを削除 $table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Delete($entities)) Write-Output "COMPLETED DELETE" }else{ Write-Output "Retrieved Entity is empty" }

どうやらエラーの原因は、

$table.CloudTable.Execute([Microsoft.WindowsAzure.Storage.Table.TableOperation]::Delete($entities))

Delete()の引数の型がDynamicTableEntityになっていることでした。
Delete()の引数の型はITableEntityでなくてはいけないのですが、
今回のようにクエリでエンティティを絞り込み、その結果を削除したい場合は
どのようにすればいいのでしょうか?

qNeumann👍を押しています

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答2

0

4年前の質問であり、既にC++での代替をされてるとのことでしたが、Powershellで実装したサンプルをQiitaの記事に投稿しました。
Table Storageに溜まり続けるWindowsイベントログのクリーニング

実装にあたり、こちらの質問に記載されているコードがとても参考になったので、この場で御礼をさせていただきます。ありがとうございました。

投稿2020/05/10 04:28

編集2020/05/10 04:41
qNeumann

総合スコア2

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

0

自己解決

自己解決しました。

初期の意向とは異なってしまいますが、
不慣れなPowershellよりは慣れているC#でプログラムを作成することにしました。
(目的はテーブル内のエンティティを削除することでしたので...)

下記コードは、AzureのTableStorage内のエンティティをTimestampで絞り込み
その結果得られたエンティティを削除するものです。
10万を超すエンティティを全て削除するには多くの時間が掛かってしまいますが、
Azure Storage Explorerで地道に削除するよりは効率がいいです。

※ほとんどMicrosoftのDocumentationに載っているコードを参考にしました。

https://azure.microsoft.com/ja-jp/documentation/articles/storage-dotnet-how-to-use-tables/

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Microsoft.Azure; // Namespace for CloudConfigurationManager using Microsoft.WindowsAzure.Storage; // Namespace for CloudStorageAccount using Microsoft.WindowsAzure.Storage.Table; // Namespace for Table storage types namespace ConsoleApplication2 { class Program { static void Main(string[] args) { // Parse the connection string and return a reference to the storage account. CloudStorageAccount storageAccount = CloudStorageAccount.Parse( CloudConfigurationManager.GetSetting("StorageConnectionString")); // Create the table client. CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); // Create the CloudTable object that represents the target table. CloudTable table = tableClient.GetTableReference("TargetTable"); //引数:テーブル名 // TableQuery オブジェクトを使用しクエリを生成 TableQuery<TableEntity> query = new TableQuery<TableEntity>() .Where(TableQuery.GenerateFilterConditionForDate ( "Timestamp", //絞り込み対象のカラム名 QueryComparisons.LessThan, DateTime.Parse("yyyy-MM-dd") )); // Print the fields for each customer. foreach (TableEntity entity in table.ExecuteQuery(query)) { Console.WriteLine("{0}, {1}\t{2}", entity.PartitionKey, entity.RowKey, entity.Timestamp); TableOperation deleteOperation = TableOperation.Delete(entity); // Execute the operation. table.Execute(deleteOperation); Console.WriteLine("Entity deleted."); } } } }

投稿2016/07/29 03:23

Takayukky

総合スコア10

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問