将 IStorageContext 作为 Cmdlet 的参数传递 - 无法找到类型 [IStorageContext]

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

我将创建一个接受 IStorageContext 作为参数的 Cmdlet。但是当 运行 cmdlet 时,它抛出一个 TypeNotFound 异常说明:

Unable to find type [IStorageContext]

这是 Cmdlet:

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

事实上,我已经使用 New-AzureRmStorageAccount and I want to pass value of its Context property to my method and in my method, using New-AzureStorageContainer 创建了一个存储帐户,我想创建一个容器。这是 Context 参数的文档:

-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

我发现IStorageContext的全名是:

但即使使用上面的类型名称作为参数类型,我也收到了同样的错误。

您可以使用以下任一类型代替 [IStorageContext]

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

所以方法是:

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

Import-Module Azure.Storage 应将所有相关类型与模块一起加载到当前 Powershell 会话中。

在特定的脚本中,您应该使用 #Requires -Modules Azure.Storage 来加载模块而无需显式 Import-Module 调用。

如果您需要来自特定库的特定类型,请使用 Add-Type cmdlet。如果您在默认位置安装了 Azure SDK,则加载类型如下:

Add-Type -LiteralPath "C:\Program Files (x86)\Microsoft SDKs\Azure\PowerShell\Storage\Azure.Storage\Microsoft.Azure.Commands.Common.Authentication.Abstractions.dll"