建立 SharePoint Server 的對象Create an audience for SharePoint Server

了解如何使用 Microsoft PowerShell 指令碼建立對象。Learn how to use a Microsoft PowerShell script to create an audience.

使用 Microsoft PowerShell 指令碼建立對象Create an audience by using a Microsoft PowerShell script

  1. 確認您是否符合下列基本需求:Verify that you meet the following minimum requirements:

  2. 複製下列變數宣告,並將它們貼到文字編輯器 (例如 [記事本])。請設定您組織特有的輸入值。您將在步驟 3 中使用這些值。請儲存檔案,並將它命名為 Audiences.ps1。Copy the following variable declarations, and paste them into a text editor such as Notepad. Set input values specific to your organization. You will use these values in step 3. Save the file, and name it Audiences.ps1.

    ## Settings you may want to change for Audience Name and Description ## 
    $mySiteHostUrl = https://www.my.contoso.com
    $audienceName = "<Input name of audience>"
    $audienceDescription = "<Input description for audience>"
    $audienceRules = @()
    $audienceRules += New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent("AccountName", "Contains", "jdoe")
    #Create an OR group operator between the two audience rules.
    $audienceRules += New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent("", "OR", "")
    $audienceRules += New-Object Microsoft.Office.Server.Audience.AudienceRuleComponent("AccountName", "Contains", "jlew")
    
  3. 複製下列程式碼,並將它貼到步驟 2 之變數宣告下方的 Audiences.ps1。Copy the following code, and paste it into Audiences.ps1 beneath the variable declarations from step 2.

    #Get the My Site Host's SPSite object
    $site = Get-SPSite $mySiteHostUrl
    $ctx = [Microsoft.Office.Server.ServerContext]::GetContext($site)
    $audMan = New-Object Microsoft.Office.Server.Audience.AudienceManager($ctx)
    #Create a new audience object for the given Audience Manager
    $aud = $audMan.Audiences.Create($audienceName, $audienceDescription)
    $aud.AudienceRules = New-Object System.Collections.ArrayList
    $audienceRules | ForEach-Object { $aud.AudienceRules.Add($_) }
    #Save the new Audience
    $aud.Commit()
    #Compile the new Audience
    $upa = Get-SPServiceApplication | Where-Object {$_.DisplayName -eq "User Profile Service Application"}
    $audJob = [Microsoft.Office.Server.Audience.AudienceJob]::RunAudienceJob(($upa.Id.Guid.ToString(), "1", "1", $aud.AudienceName))
    

    注意

    您可以使用不同的檔案名稱,但是必須將檔案儲存為副檔名為 .ps1 的 ANSI 編碼文字檔。You can use a different file name, but you must save the file as an ANSI-encoded text file with the extension .ps1.

  4. 按一下 SharePoint Management Shell。Click SharePoint Management Shell.

  5. 變更至您儲存檔案的目錄。Change to the directory to which you saved the file.

  6. 在 PowerShell 命令提示字元中,輸入下列命令:At the PowerShell command prompt, type the following command:

    ./Audiences.ps1 
    

如需 PowerShell 指令碼和 .ps1 檔案的其他資訊,請參閱<執行 Windows PowerShell 指令碼>。For additional information about PowerShell scripts and .ps1 files, see Running Windows PowerShell Scripts.

如需如何建立對象的其他資訊,請參閱 AudienceRuleComponent 類別For additional information about how to create audiences, see AudienceRuleComponent class.