将IStorageContext传递为Cmdlet的参数 - 无法找到类型[IStorageContext]
问题描述:
我将创建一个Cmdlet,它接受IStorageContext
作为参数。但在运行cmdlet时,它抛出一个异常TypeNotFound
声明:将IStorageContext传递为Cmdlet的参数 - 无法找到类型[IStorageContext]
找不到类型[IStorageContext]
下面是该cmdlet:
Function SomeCmdlet {
param(
[parameter(Mandatory=$true)]
[IStorageContext]$storageContext
)
New-AzureStorageContainer -Name "ContainerName" -Context $storageContext -Permission Off
}
事实上,我已经使用New-AzureRmStorageAccount
创建了一个存储帐户,我想将其Context
属性的值传递给我的方法,并在我的方法中使用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
全名是:
Microsoft.Azure.Commands.Common.Authentication.Abstractions.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"
感谢Alex。我共享的代码可以简单地用于重现问题。你测试了你提出的解决方案吗? –
您正在使用哪个版本的'Azure.Storage'模块? –