3

I want to use Powershell to insert entities into Azure Table Storage and therefore need to instantiate a class which derives from the .NET abstract class Microsoft.WindowsAzure.StorageClient.TableServiceEntity

I am thinking to instantiate (in a Powershell 2.0 script) an object using eg New-Object and then adding members using Add-Member but cannot achieve the instantiation of a compatible TableServiceEntity derived class. (I can instantiate other arbitrary PSObjects, but they won't do it seems)

Alternatively if someone could show me how to instantiate and populate ANY object class which could then be passed into the tableServiceContext.AddObject() method

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
t0rus
  • 197
  • 1
  • 3
  • 9

1 Answers1

2

What you can do is to create your class in a cs file and then reference it to your script. Here's example: Save this as a cs file:

using System.Collections;
    public  class MyList : System.Collections.Generic.List<string>
    {
        public static string Test(string input)
        {
            return string.Format("test input {0}", input);
        }
    }

And here's the code to run it:

cd c:\pst
Add-Type -Path "2.cs" #-ReferencedAssemblies $assembly
[MyList]::Test("aaa")

You might need to pass your assembly to ReferencedAssemblies . And you also might need to upload assemblies into your script

The way to create table is described here: Add or replace entity in Azure Table Storage

Community
  • 1
  • 1
Andrey Marchuk
  • 13,301
  • 2
  • 36
  • 52
  • Good idea, but is there no Powershell only way to do it? – t0rus Nov 29 '11 at 15:41
  • If you will find a C# code that is doing exactly what you want - it can be rewritten to PowerShell. Edit your main post and people will help you. – Andrey Marchuk Nov 30 '11 at 07:35
  • Thanks. This _has_ helped and proved to be the seed of the solution. What I did in the end was to use **Add-Type** with the **-TypeDefinition** parameter to specify a short class definition which simply inherits from the abstract .NET base class (in this case **TableServiceEntity**) and now have a standalone Powershell script working – t0rus Nov 30 '11 at 12:47
  • Yes, I accept it, bt dont have the points to vote it up. Many thanks for your quick response. – t0rus Nov 30 '11 at 12:52