0
votes

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

2
Does the PowerShell line return an error? If so you should post it here, If not, you should check what $PublisherRule contains after it runs and post the results here - Bassie
Edited and added the error - Nido
Awesome - you should post the answer to this question if you get a chance as that may help others who are having this issue! - Bassie

2 Answers

0
votes

You can use the array sub-expression operator @() to pass an array as an argument to -ArgumentList:

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

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;