如何使用其他对象创建Powershell对象作为参数

时间:2016-06-16 19:39:50

标签: powershell powershell-v3.0 azure-powershell

我正在尝试为Azure Service Bus主题创建编写Powershell脚本。我在C#中有类似的代码,但现在我想将它转换为Powershell脚本。但是现在我被困在如何将以下行转换为Powershell:

AuthorizationRule Ar = new SharedAccessAuthorizationRule("PublisherOwner", "SASKEY++++++++++++++++++++++", new[] { AccessRights.Listen, AccessRights.Send });

我正在尝试这样,但它不起作用:

$PublisherRule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -ArgumentList "PublisherOwner", $PublisherKey

这是错误

  

New-Object:找不到类型[Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule]:确保   加载包含此类型的程序集。   在行:1字符:28   + $ PublisherRule = New-Object<<<< -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -   ArgumentList“PublisherOwner”,$ PublisherKey       + CategoryInfo:InvalidType:(:) [New-Object],PSArgumentException       + FullyQualifiedErrorId:TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand

2 个答案:

答案 0 :(得分:0)

您可以使用数组子表达式运算符@()将数组作为参数传递给-ArgumentList

$PublisherRule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -ArgumentList "PublisherOwner", $PublisherKey,@([Microsoft.ServiceBus.Messaging.AccessRights]::Listen,[Microsoft.ServiceBus.Messaging.AccessRights]::Send)

答案 1 :(得分:0)

第三个参数数组需要是一个强类型数组。转换后的脚本如下所示:

[Microsoft.ServiceBus.Messaging.AccessRights[]]$PublisherRights =  
New-Object -TypeName "System.Collections.Generic.List[Microsoft.ServiceBus.Messaging.AccessRights]" ;

$PublisherRights += [Microsoft.ServiceBus.Messaging.AccessRights]::Listen;
$PublisherRights += [Microsoft.ServiceBus.Messaging.AccessRights]::Send;

$Rule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -ArgumentList "PublisherRule", "SASKEY", $PublisherRights;