简体   繁体   English

Powershell Dynamics CRM,如何向用户添加两个队列

[英]Powershell Dynamics CRM, how to add two queues to a user

I have created a user and I am trying to assign him two queues n°1 et n°2. 我已经创建了一个用户,我正在尝试为他分配两个队列n°1和n°2。 The problem is that the code I used only replaces the last queue. 问题是我使用的代码仅替换了最后一个队列。

Import-Module C:\Powershell\CRMBuzz\CRMBuzzPowerTools_Module_2_0_0_15_Setup\WindowsPowerShell\Modules\CRMBuzzPowerTools\CRMBuzz.PowerTools.PSSnapin.dll -Force -WarningAction SilentlyContinue -DisableNameCheckin
$connString="Url=https://crmlab:5555/CRMLab;Username=test@crm.lab;password=******;"
$CRMConn = New-OrganizationConnection -ConnectionString $connString -Verbose
$queue_ref0=Get-EntityReferenceByName -OrganizationService $CRMConn -EntityName "queue" -FindFieldName "name" -ReferenceValue "TEST1"
$queue_ref1=Get-EntityReferenceByName -OrganizationService $CRMConn -EntityName "queue" -FindFieldName "name" -ReferenceValue "TEST2"
$userent=Search-EntityFull -OrganizationService $CRMConn  -EntityObject systemuser -FieldName domainname -SearchValue "TEST\P_TEST"
[Microsoft.Xrm.Sdk.EntityReference] $userent.Attributes["queueid"]=$queue_ref0
[Microsoft.Xrm.Sdk.EntityReference] $userent.Attributes["queueid"]=$queue_ref1
Update-Record -OrganizationService $CRMConn -EntityObject $userent –verbose

I tried another method I found online but it's returning incompatibility errors. 我尝试了另一种在网上找到的方法,但是返回了不兼容错误。

Move-CrmRecordToQueue -EntityLogicalName account -Id 5ff140ea-95ed-e811-80e9-005056bd633b -QueueName "TEST1" -WorkingUserId 5bf140ea-95ed-e811-80e9-005056bd633b

I have also tried this but don't know the parameters to use. 我也尝试过,但是不知道要使用的参数。

Import-Module C:\Powershell\Handy.Crm.Extensions.Powershell.Cmdlets
$cred = Get-Credential
$CRMConn = Connect-CrmOnPremDiscovery -Credential $cred -ServerUrl https://crmlab:5555/CRMLab
Set-CRMQueueForUser -Connection $CRMConn -UserId 5bf140ea-95ed-e811-80e9-005056bd633b -QueueId 023963ca-08a5-e611-80c6-00155d011760

EDIT 编辑

James' answer cleared a few things for me. 詹姆斯的回答为我清除了几件事。 Above I was trying to add two queues to the default queue of user, which is impossible. 在上面,我试图将两个队列添加到用户的默认队列,这是不可能的。 I think I should add them in the field that's below the default queue like in this picture (don't know this field's name): 我想我应该将它们添加到默认队列下方的字段中,如下图所示(不知道该字段的名称):

在此处输入图片说明

It looks like you are trying to add the user into a queue. 您似乎正在尝试将用户添加到队列中。

Pretty sure that section is "Queues I'm a member of", which is the queuemembership_association many to many relationship between user and queue. 可以肯定的是,该部分是“我是队列的成员”,它是用户和队列之间的多对多关系queuemembership_association

You will need to issue an AssociateRequest . 您将需要发出一个AssociateRequest

Yes, you can do it with PowerShell . 是的,您可以使用PowerShell进行操作

For example, queuemembership_association link can be created using the below cmdlet. 例如,可以使用以下cmdlet创建queuemembership_association链接。 (Pls test it as I have not tested this yet) (请对其进行测试,因为我尚未对此进行测试)

PS C:\>$systemuser = Get-CrmRecord systemuser 00005a70-6317-e511-80da-c4346bc43d94 name

 PS C:\>$queue = Get-CrmRecord queue 66005a70-6317-e511-80da-c4346bc43d94 fullname

 PS C:\>Add-CrmRecordAssociation $systemuser $queue queuemembership_association

Two functions are available for associating single entity record and associating on bulk namely, Add-CrmRecordAssociation and Add-CrmMultiRecordAssociation . 有两个函数可用于关联单个实体记录和批量关联,即Add-CrmRecordAssociationAdd-CrmMultiRecordAssociation Pasting the related cmdlets along with examples: 粘贴相关的cmdlet以及示例:

#CreateEntityAssociation
function Add-CrmRecordAssociation{

<#
 .SYNOPSIS
 Associates two records for N:N relationship.

 .DESCRIPTION
 The Add-CrmRecordAssociation cmdlet lets you associate two records for N:N relationship by specifying relatioship logical name. 

 There are two ways to specify records.
 
 1. Pass EntityLogicalName and record's Id for both records.
 2. Get a record object by using Get-CrmRecord/Get-CrmRecords cmdlets, and pass it for both records.

 You can specify relationship logical name for the association.

 .PARAMETER conn
 A connection to your CRM organizatoin. Use $conn = Get-CrmConnection <Parameters> to generate it.

 .PARAMETER CrmRecord1
 A first record object which is obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't use EntityLogicalName/Id.

 .PARAMETER CrmRecord2
 A second record object which is obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't use EntityLogicalName/Id.

 .PARAMETER EntityLogicalName1
 A logicalname for first Entity. i.e.)accout, contact, lead, etc..

 .PARAMETER Id1
 An Id (guid) of first record

 .PARAMETER EntityLogicalName2
 A logicalname for second Entity. i.e.)accout, contact, lead, etc..

 .PARAMETER Id2
 An Id (guid) of second record

 .PARAMETER RelationshipName
 A N:N relationship logical name.

 .EXAMPLE
 Add-CrmRecordAssociation -conn $conn -EntityLogicalName1 account -Id1 00005a70-6317-e511-80da-c4346bc43d94 -EntityLogicalName2 contact -Id2 66005a70-6317-e511-80da-c4346bc43d94 -RelationshipName new_accounts_contacts

 This example associates an account and a contact records through new_accounts_contacts custom N:N relationship.

 .EXAMPLE
 Add-CrmRecordAssociation account 00005a70-6317-e511-80da-c4346bc43d94 contact 66005a70-6317-e511-80da-c4346bc43d94 new_accounts_contacts
 
 This example associates an account and a contact records through new_accounts_contacts custom N:N relationship by ommiting parameters names.
 When ommiting parameter names, you do not provide $conn, cmdlets automatically finds it.

 .EXAMPLE
 PS C:\>$account = Get-CrmRecord account 00005a70-6317-e511-80da-c4346bc43d94 name

 PS C:\>$contact = Get-CrmRecord contact 66005a70-6317-e511-80da-c4346bc43d94 fullname

 PS C:\>Add-CrmRecordAssociation -conn $conn -CrmRecord1 $account -CrmRecord2 $contact -RelationshipName new_accounts_contacts

 This example retrieves and stores an account and a contact records to variables, then pass them to Add-CrmRecordAssociation cmdlets.

 .EXAMPLE
 PS C:\>$account = Get-CrmRecord account 00005a70-6317-e511-80da-c4346bc43d94 name

 PS C:\>$contact = Get-CrmRecord contact 66005a70-6317-e511-80da-c4346bc43d94 fullname

 PS C:\>Add-CrmRecordAssociation $account $contact new_accounts_contacts

 This example retrieves and stores an account and a contact records to variables, then pass them to Add-CrmRecordAssociation cmdlets.

#>

    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$conn,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="CrmRecord")]
        [PSObject]$CrmRecord1,
        [parameter(Mandatory=$true, Position=2, ParameterSetName="CrmRecord")]
        [PSObject]$CrmRecord2,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="NameWithId")]
        [string]$EntityLogicalName1,
        [parameter(Mandatory=$true, Position=2, ParameterSetName="NameWithId")]
        [guid]$Id1,
        [parameter(Mandatory=$true, Position=3, ParameterSetName="NameWithId")]
        [string]$EntityLogicalName2,
        [parameter(Mandatory=$true, Position=4, ParameterSetName="NameWithId")]
        [guid]$Id2,
        [parameter(Mandatory=$true, Position=5)]
        [string]$RelationshipName
    )

    $conn = VerifyCrmConnectionParam $conn; 

    if($CrmRecord1 -ne $null)
    {
        $EntityLogicalName1 = $CrmRecord1.logicalname
        $Id1 = $CrmRecord1.($EntityLogicalName1 + "id")
    }

    if($CrmRecord2 -ne $null)
    {
        $EntityLogicalName2 = $CrmRecord2.logicalname
        $Id2 = $CrmRecord2.($EntityLogicalName2 + "id")
    }

    try
    {
        $result = $conn.CreateEntityAssociation($EntityLogicalName1, $Id1, $EntityLogicalName2, $Id2, $RelationshipName, [Guid]::Empty)
        if(!$result)
        {
            return $conn.LastCrmException
        }
    }
    catch
    {
        return $conn.LastCrmException
    }
}

#CreateMultiEntityAssociation
function Add-CrmMultiRecordAssociation{

<#
 .SYNOPSIS
 Associates multiple records to single record for N:N relationship.

 .DESCRIPTION
 The Add-CrmMultiRecordAssociation cmdlet lets you associate multiple records to single record for N:N relationship by specifying relatioship logical name. 
 Use @('<object>','<object>') syntax to specify multiple ids or records.
 if the relationship is self-referencing, specify $True for -IsReflexiveRelationship Parameter.

 There are two ways to specify records.
 
 1. Pass EntityLogicalName and record's Id for both records.
 2. Get record object(s) by using Get-CrmRecord/Get-CrmRecords cmdlets, and pass them.

 You can specify relationship logical name for the association.

 .PARAMETER conn
 A connection to your CRM organizatoin. Use $conn = Get-CrmConnection <Parameters> to generate it.

 .PARAMETER CrmRecord1
 A first record object which is obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't use EntityLogicalName/Id.

 .PARAMETER CrmRecord2s
 An array of records object which are obtained via Get-CrmRecord/Get-CrmRecords. When you pass CrmRecord, then you don't use EntityLogicalName/Id.

 .PARAMETER EntityLogicalName1
 A logicalname for first Entity. i.e.)accout, contact, lead, etc..

 .PARAMETER Id1
 An Id (guid) of first record

 .PARAMETER EntityLogicalName2
 A logicalname for second Entity. i.e.)accout, contact, lead, etc..

 .PARAMETER Id2s
 An array of Ids (guid) of second records. Specify by using @('66005a70-6317-e511-80da-c4346bc43d94','62005a70-6317-e511-80da-c4346bc43d94') synctax.

 .PARAMETER RelationshipName
 A N:N relationship logical name.

 .PARAMETER IsReflexiveRelationship
 Specify $True if the N:N relationship is self-referencing.

 .EXAMPLE
 Add-CrmMultiRecordAssociation -conn $conn -EntityLogicalName1 account -Id1 00005a70-6317-e511-80da-c4346bc43d94 -EntityLogicalName2 contact -Id2s @('66005a70-6317-e511-80da-c4346bc43d94','62005a70-6317-e511-80da-c4346bc43d94') -RelationshipName new_accounts_contacts
 This example associates an account and two contact records through new_accounts_contacts custom N:N relationship.

 .EXAMPLE
 Add-CrmMultiRecordAssociation account 00005a70-6317-e511-80da-c4346bc43d94 contact @('66005a70-6317-e511-80da-c4346bc43d94','62005a70-6317-e511-80da-c4346bc43d94') new_accounts_contacts
 
 This example associates an account and two contact records through new_accounts_contacts custom N:N relationship by ommiting parameters names.
 When ommiting parameter names, you do not provide $conn, cmdlets automatically finds it.

 .EXAMPLE
 PS C:\>$account = Get-CrmRecord account 00005a70-6317-e511-80da-c4346bc43d94 name

 PS C:\>$fetch = @"
<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false" no-lock="true">
  <entity name="contact">
    <attribute name="fullname" />
    <filter type="and">
      <condition attribute="lastname" operator="like" value="%sample%" />
    </filter>
  </entity>
</fetch>
"@

 PS C:\>$contacts = Get-CrmRecordsByFetch $fetch

 PS C:\>Add-CrmMultiRecordAssociation $account $contacts.CrmRecords new_accounts_contacts

 This example retrieves contacts by using FetchXML and stores to a variable, then retrieves and store an account record to another variable.
 Then passes those variables Add-CrmMultiRecordAssociation.
#>

    [CmdletBinding()]
    PARAM(
        [parameter(Mandatory=$false)]
        [Microsoft.Xrm.Tooling.Connector.CrmServiceClient]$conn,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="CrmRecord")]
        [PSObject]$CrmRecord1,
        [parameter(Mandatory=$true, Position=2, ParameterSetName="CrmRecord")]
        [PSObject[]]$CrmRecord2s,
        [parameter(Mandatory=$true, Position=1, ParameterSetName="NameWithId")]
        [string]$EntityLogicalName1,
        [parameter(Mandatory=$true, Position=2, ParameterSetName="NameWithId")]
        [guid]$Id1,
        [parameter(Mandatory=$true, Position=3, ParameterSetName="NameWithId")]
        [string]$EntityLogicalName2,
        [parameter(Mandatory=$true, Position=4, ParameterSetName="NameWithId")]
        [guid[]]$Id2s,
        [parameter(Mandatory=$true, Position=5)]
        [string]$RelationshipName,
        [parameter(Mandatory=$false, Position=6)]
        [bool]$IsReflexiveRelationship
    )

    $conn = VerifyCrmConnectionParam $conn;   

    if($CrmRecord1 -ne $null)
    {
        $EntityLogicalName1 = $CrmRecord1.logicalname
        $Id1 = $CrmRecord1.($EntityLogicalName1 + "id")
    }

    if($CrmRecord2s -ne $null)
    {
        if($CrmRecord2s.Count -ne 0)
        {
            $EntityLogicalName2 = $CrmRecord2s[0].logicalname
            $Ids = New-Object 'System.Collections.Generic.List[System.Guid]'
            foreach($CrmRecord2 in $CrmRecord2s)
            {
                $Ids.Add($CrmRecord2.($EntityLogicalName2 + "id"))
            }
            $Id2s = $Ids.ToArray()
        }
         else
        {
            Write-Warning 'CrmRecords2 does not include any records.'
            break;
        }
    }   

    try
    {
        $result = $conn.CreateMultiEntityAssociation($EntityLogicalName1, $Id1, $EntityLogicalName2, $Id2s, $RelationshipName, [Guid]::Empty, $IsReflexiveRelationship)
        if(!$result)
        {
            return $conn.LastCrmException
        }
    }
    catch
    {
        return $conn.LastCrmException
    }
}

It looks like you are trying to set the default queue field. 您似乎正在尝试设置默认队列字段。

在此处输入图片说明

That can only hold one value at a time so I don't think what you are trying to do will work. 一次只能保留一个值,所以我认为您尝试执行的操作不会起作用。

If its helpful you could issue the update call twice, this will save both values into CRM, but the last will overwrite the first. 如果有帮助,您可以发出两次更新调用,这会将两个值都保存到CRM中,但是最后一个将覆盖第一个。

[Microsoft.Xrm.Sdk.EntityReference] $userent.Attributes["queueid"]=$queue_ref0
Update-Record -OrganizationService $CRMConn -EntityObject $userent –verbose

[Microsoft.Xrm.Sdk.EntityReference] $userent.Attributes["queueid"]=$queue_ref1
Update-Record -OrganizationService $CRMConn -EntityObject $userent –verbose

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Powershell与应用程序用户在线连接到Dynamics CRM - Connect with Powershell to Dynamics CRM online with application user 是否可以使用Powershell添加和配置Dynamics CRM工作流程 - Is it possible to add and configure Dynamics CRM workflows with Powershell 我可以使用C#或Powershell添加/更新CRM Dynamics插件程序集吗? - Can I add/update CRM Dynamics plugin assemblies using C# or Powershell? 使用Powershell的Dynamics CRM Online V 9部署软件包-问题 - Dynamics CRM online V 9 Deploy packages using powershell - issues 禁用组织时 Microsoft CRM Dynamics 2011 PowerShell 错误 - Microsoft CRM Dynamics 2011 PowerShell Error when disabling an organization Microsoft Dynamics CRM的PowerShell cmdlet中的内部服务器错误 - Internal Server Error in PowerShell cmdlets for Microsoft Dynamics CRM 如何在线连接 PowerShell 与 CRM? - How to connect PowerShell with CRM online? 使用 PowerShell 从 Dynamics CRM(在线)检索帐户实体名称列表 - Retrieve list of account entity names from Dynamics CRM (Online) using PowerShell 如何在Powershell中将用户添加到本地管理员组? - How to add user to local admin group in Powershell? 使用 Azure Powershell(AZ 模块)为 Dynamics CRM Online 设置 Azure Ad Api 权限 - Setting Azure Ad Api permission for Dynamics CRM Online using Azure Powershell (AZ Module)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM