0
Add-Type -Path c:\AzureStorageFile\Microsoft.WindowsAzure.Storage.dll

$AzStorObject = New-Object -TypeName Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext

Gives me error

New-Object : A constructor was not found. Cannot find an appropriate constructor for type Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext.

Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68
  • According to [the documentation](https://learn.microsoft.com/dotnet/api/microsoft.windowsazure.commands.storage.azurestoragecontext.-ctor) the parameterless constructor for `AzureStorageContext` is not public. There is a [`New-AzureStorageContext` cmdlet](https://learn.microsoft.com/powershell/module/azure.storage/new-azurestoragecontext), however, that outputs `AzureStorageContext` instances. – Lance U. Matthews Aug 12 '20 at 20:55
  • can you please be a little more specific? I want to create an object of type Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext – 01notReally Aug 13 '20 at 12:00

1 Answers1

1

You are not passing the -ArgumentList parameter to New-Object, so when attempting to instantiate the specified type it will look for a constructor that takes no parameters. The parameterless constructor of the AzureStorageContext class is protected, not public, though...

protected AzureStorageContext ();

...so New-Object will not be able to invoke it.

That same Microsoft.WindowsAzure.Storage.dll assembly is used by the Azure.Storage package. Upon installing that...

Install-Module -Name Azure.Storage

...you can invoke the New-AzureStorageContext cmdlet to create AzureStorageContext instances...

$AzStorObject = New-AzureStorageContext # Additional parameters needed

Otherwise, there is a public constructor of the AzureStorageContext class...

public AzureStorageContext (Microsoft.WindowsAzure.Storage.CloudStorageAccount account);

...that you could use if you pass a CloudStorageAccount instance...

$AzStorObject = New-Object -TypeName Microsoft.WindowsAzure.Commands.Storage.AzureStorageContext -ArgumentList $myCloudStorageAccount
Lance U. Matthews
  • 15,725
  • 6
  • 48
  • 68