Powershell无法创建对象列表

时间:2014-12-15 09:19:07

标签: c# powershell

我正在尝试在Powershell中创建一个类型为Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule的对象。构造函数接受一个键和规则列表。下面是代码。我收到以下错误。     错误:

New-Object : Cannot find an overload for "SharedAccessAuthorizationRule" and the argument count: "2".
At line:39 char:10
     $Rule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthori ...
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    CategoryInfo          : InvalidOperation: (:) [New-Object], MethodException
    FullyQualifiedErrorId : ConstructorInvokedThrowException,Microsoft.PowerShell.Commands.NewObjectCommand

脚本:

cls    
Add-Type -Path "C:\{Your Service Bus dll location}\Microsoft.ServiceBus.dll"
Add-Type -Path "C:\{Your WindowsAzure.configuration Location}\Microsoft.WindowsAzure.Configuration.dll"
$AccessRights =  New-Object -TypeName 'System.Collections.Generic.List[Microsoft.ServiceBus.Messaging.AccessRights]' ;

$CanManage = $true;
$CanListen = $true;
$CanSend = $true;

if ($CanManage)
{
    $AccessRights  = $AccessRights + [Microsoft.ServiceBus.Messaging.AccessRights]::Manage;
}

if ($CanListen)
{
    $AccessRights  = $AccessRights + [Microsoft.ServiceBus.Messaging.AccessRights]::Listen;
}

if ($CanSend)
{
    $AccessRights  = $AccessRights + [Microsoft.ServiceBus.Messaging.AccessRights]::Send;
}

$Rule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -ArgumentList "2", $AccessRights;

2 个答案:

答案 0 :(得分:2)

    The array needs to be a strongly typed array. 

    Script that worked:
    cls    
    Add-Type -Path "C:\{Your Service Bus dll location}\Microsoft.ServiceBus.dll"
    Add-Type -Path "C:\{Your WindowsAzure.configuration Location}\Microsoft.WindowsAzure.Configuration.dll"

    #Strongly Typed Array
    [Microsoft.ServiceBus.Messaging.AccessRights[]]$AccessRights =  
    New-Object -TypeName "System.Collections.Generic.List[Microsoft.ServiceBus.Messaging.AccessRights]" ;

    $CanManage = $true;
    $CanListen = $true;
    $CanSend = $true;

        if ($CanManage)
        {
            $AccessRights  +=  [Microsoft.ServiceBus.Messaging.AccessRights]::Manage;
        }

        if ($CanListen)
        {
            $AccessRights  += [Microsoft.ServiceBus.Messaging.AccessRights]::Listen;
        }

        if ($CanSend)
        {

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

    $AccessRights;    

        $Rule = New-Object -TypeName Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule -ArgumentList "2", $AccessRights;

        $Rule;

答案 1 :(得分:0)

我猜你们对参数的排序是错误的,因为类的构造函数确实接受了一个字符串和一个可枚举的。尝试以下代码,看看是否有任何区别

$Rule = New-Object Microsoft.ServiceBus.Messaging.SharedAccessAuthorizationRule("2", $AccessRights);