Search code examples
powershellpowershell-3.0azure-powershell

How to create Powershell Object with other Objects as Argument


I am trying to write Powershell script for Azure Service Bus Topic Creation. I have similar code in C# which works but now I want to transform it to Powershell script. But right now I am stuck on how to convert following line to Powershell:

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

I am trying it like this, but it isn't working:

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

Here is the Error

New-Object : Cannot find type [Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule]: make sure the assembly containing this type is loaded. At line:1 char:28 + $PublisherRule = New-Object <<<< -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule - ArgumentList "PublisherOwner", $PublisherKey + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand


Solution

  • Third parameter the array needs to be a strongly typed array. The converted script is as below and it worked:

    [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;