0

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

4
  • 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 Commented Jun 16, 2016 at 19:46
  • 1
    Edited and added the error Commented Jun 16, 2016 at 19:50
  • 1
    This Stackoverflow Post answered my question Commented Jun 16, 2016 at 20:37
  • Awesome - you should post the answer to this question if you get a chance as that may help others who are having this issue! Commented Jun 17, 2016 at 8:37

2 Answers 2

0

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Tried it but it gives following error: New-Object : Cannot find an overload for "SharedAccessAuthorizationRule" and the argument count: "3". At line:1 char:18 + $PublisherRule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAcces ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [New-Object], MethodException + FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand
0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.