Search code examples
powershellazureazure-storageazure-powershellpowershell-cmdlet

Pass IStorageContext as parameter of a Cmdlet - Unable to find type [IStorageContext]


I'm going to create a Cmdlet which accepts IStorageContext as parameter. But when running the cmdlet, it throws a TypeNotFound exception stating:

Unable to find type [IStorageContext]

Here is the Cmdlet:

Function SomeCmdlet {
  param(
    [parameter(Mandatory=$true)]
    [IStorageContext]$storageContext
  )
  New-AzureStorageContainer -Name "ContainerName" -Context $storageContext -Permission Off
}

In fact, I've created a Storage Account using New-AzureRmStorageAccount and I want to pass value of its Context property to my method and in my method, using New-AzureStorageContainer i want to create a container. Here is the documentation for Context parameter:

-Context
Specifies a context for the new container.

Type:                       IStorageContext
Position:                   Named
Default value:              None
Accept pipeline input:      True (ByPropertyName, ByValue)
Accept wildcard characters: False

I found-out that the full name of IStorageContext is:

  • Microsoft.Azure.Commands.Common.Authentication.Abstractions.IStorageContext

But even with using above type name as parameter type I received the same error.


Solution

  • Instead of [IStorageContext] you can use either of following types:

    • [Microsoft.WindowsAzure.Commands.Common.Storage.AzureStorageContext]
    • [object]

    So the method would be:

    Function SomeCmdlet {
      param(
        [parameter(Mandatory=$true)]
        [object]$storageContext
      )
      New-AzureStorageContainer -Name "ContainerName" -Context $storageContext -Permission Off
    }