共用方式為


使用 PowerShell 來變更及列出 Reporting Services 訂用帳戶擁有者並執行訂閱

從 SQL Server 2008 R2Reporting Services 開始,您可以透過程式設計方式,將 Reporting Services 訂閱的擁有權從一位使用者傳輸到另一個使用者。 本主題提供數個 Windows PowerShell 腳本,您可以用來變更或只列出訂用帳戶擁有權。 每個範例都包含原生模式和 SharePoint 模式的範例語法。 變更訂閱擁有者後,訂閱將在新擁有者的安全性環境中執行,並且報告中的 User!UserID 欄位將顯示新擁有者的值。 如需 PowerShell 範例呼叫之物件模型的詳細資訊,請參閱 ChangeSubscriptionOwner

PowerShell 相關內容

適用於:Reporting Services 原生模式 | Reporting Services SharePoint 模式

本主題內容:

如何使用腳本

權限

本節摘要說明在原生模式和 SharePoint 模式下使用 Reporting Services 的每個方法所需的許可權等級。 本主題中的腳本會使用下列 Reporting Services 方法:

原生模式:

  • 列出訂閱:(HYPERLINK “https://technet.microsoft.com/library/microsoft.reportingservices.interfaces.reportoperation.aspx"報表上的 ReadSubscription 且使用者是訂閱擁有者) OR ReadAnySubscription

  • 變更訂閱:用戶必須是 BUILTIN\Administrators 群組的成員

  • 列出子項:Item 上的讀取屬性

  • 觸發事件:GenerateEvents(系統)

SharePoint 模式:

  • 列出訂閱項目:管理警示或(超連結 "https://technet.microsoft.com/library/microsoft.sharepoint.spbasepermissions.aspx""; 在報表上的創建警示,且使用者是訂閱擁有者且該訂閱是計時性訂閱)。

  • 變更訂閱:ManageWeb

  • 列出子項目:檢視列表項目

  • 火災事件:ManageWeb

如需詳細資訊,請參閱 比較 Reporting Services 中的角色和工作與 SharePoint 群組和許可權

腳本使用方式

建立文稿檔案 (.ps1)

  1. 建立名為 c:\scripts 的資料夾。 如果您選擇不同的資料夾,請修改範例命令行語法語句中使用的資料夾名稱。

  2. 為每個腳本建立文本檔,並將檔案儲存至 c:\scripts 資料夾。 當您建立 .ps1 檔案時,請使用每個範例命令行語法的名稱。

  3. 以系統管理權限開啟命令提示字元。

  4. 使用每個範例所提供的範例命令行語法,執行每個腳本檔案。

測試的環境

本主題中的腳本已在 PowerShell 第 3 版和下列 Reporting Services 版本中進行測試:

  • SQL Server 2014

  • SQL Server 2012

  • SQL Server 2008 R2

腳本:列出所有訂用帳戶的擁有權

此腳本會列出網站上的所有訂用帳戶。 您可以使用此腳本來測試連線,或驗證報表路徑和訂用帳戶標識碼,以用於其他腳本。 這也是一個實用的腳本,只需稽核哪些訂用帳戶存在,以及擁有這些訂用帳戶的人員。

原生模式語法

powershell c:\scripts\ListAll_SSRS_Subscriptions.ps1 "[server]/reportserver" "/"

SharePoint 模式語法

powershell c:\scripts\ListAll_SSRS_Subscriptions.ps1 "[server]/_vti_bin/reportserver" "http://[server]"

劇本

# Parameters
#    server   - server and instance name (e.g. myserver/reportserver or myserver/reportserver_db2)

Param(
    [string]$server,
    [string]$site
   )

$rs2010 += New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential ;
$subscriptions += $rs2010.ListSubscriptions($site); # use "/" for default native mode site

Write-Host " "
Write-Host "----- $server's Subscriptions: "
$subscriptions | select Path, report, Description, Owner, SubscriptionID, lastexecuted, Status

小提示

若要確認 SharePoint 模式中的網站 URL,請使用 SharePoint Cmdlet Get-SPSite。 如需詳細資訊,請參閱<Get-SPSite>。

腳本:列出特定使用者所擁有的所有訂用帳戶

此腳本會列出特定用戶所擁有的所有訂用帳戶。 您可以使用此腳本來測試連線,或驗證報表路徑和訂用帳戶標識碼,以用於其他腳本。 當您組織中的人員離開,而且您想要確認他們擁有的訂用帳戶,以便變更擁有者或刪除訂用帳戶時,此腳本很有用。

原生模式語法

powershell c:\scripts\ListAll_SSRS_Subscriptions4User.ps1 "[Domain]\[user]" "[server]/reportserver" "/"

SharePoint 模式語法

powershell c:\scripts\ListAll_SSRS_Subscriptions4User.ps1 "[Domain]\[user]"  "[server]/_vti_bin/reportserver" "http://[server]"

劇本

# Parameters:
#    currentOwner - DOMAIN\USER that owns the subscriptions you wish to change
#    server        - server and instance name (e.g. myserver/reportserver or myserver/reportserver_db2)
#    site        - use "/" for default native mode site
Param(
    [string]$currentOwner,
    [string]$server,
    [string]$site
)

$rs2010 = New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential ;
$subscriptions += $rs2010.ListSubscriptions($site);

Write-Host " "
Write-Host " "
Write-Host "----- $currentOwner's Subscriptions: "
$subscriptions | select Path, report, Description, Owner, SubscriptionID, lastexecuted,Status | where {$_.owner -eq $currentOwner}

腳本:變更特定用戶擁有之所有訂用帳戶的擁有權

此腳本會將特定用戶擁有的所有訂用帳戶擁有權變更為新的擁有者參數。

原生模式語法

powershell c:\scripts\ChangeALL_SSRS_SubscriptionOwner.ps1 "[Domain]\current owner]" "[Domain]\[new owner]" "[server]/reportserver"

SharePoint 模式語法

powershell c:\scripts\ChangeALL_SSRS_SubscriptionOwner.ps1 "[Domain]\{current owner]" "[Domain]\[new owner]" "[server]/_vti_bin/reportserver"

劇本

# Parameters:
#    currentOwner - DOMAIN\USER that owns the subscriptions you wish to change
#    newOwner      - DOMAIN\USER that will own the subscriptions you wish to change
#    server        - server and instance name (e.g. myserver/reportserver, myserver/reportserver_db2, myserver/_vti_bin/reportserver)

Param(
    [string]$currentOwner,
    [string]$newOwner,
    [string]$server
)

$rs2010 = New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential ;
$items = $rs2010.ListChildren("/", $true);

$subscriptions = @();

ForEach ($item in $items)
{
    if ($item.TypeName -eq "Report")
    {
        $curRepSubs = $rs2010.ListSubscriptions($item.Path);
        ForEach ($curRepSub in $curRepSubs)
        {
            if ($curRepSub.Owner -eq $currentOwner)
            {
                $subscriptions += $curRepSub;
            }
        }
    }
}

Write-Host " "
Write-Host " "
Write-Host -foregroundcolor "green" "-----  $currentOwner's Subscriptions changing ownership to $newOwner : "
$subscriptions | select SubscriptionID, Owner, Path, Description,  Status  | format-table -AutoSize

ForEach ($sub in $subscriptions)
{
    $rs2010.ChangeSubscriptionOwner($sub.SubscriptionID, $newOwner);
}

$subs2 = @();

ForEach ($item in $items)
{
    if ($item.TypeName -eq "Report")
    {
        $subs2 += $rs2010.ListSubscriptions($item.Path);
    }
}

腳本:列出與特定報表相關聯的所有訂閱

此腳本會列出與特定報表相關聯的所有訂閱。 SharePoint 模式中的報表路徑語法不同,需要完整的 URL。 在語法範例中,使用的報表名稱是 "title only",因為其中包含空格,所以需要在報表名稱周圍加上單引號。

原生模式語法

powershell c:\scripts\List_SSRS_One_Reports_Subscriptions.ps1 "[server]/reportserver" "'/reports/title only'" "/"

SharePoint 模式語法

powershell c:\scripts\List_SSRS_One_Reports_Subscriptions.ps1 "[server]/_vti_bin/reportserver"  "'http://[server]/shared documents/title only.rdl'" "http://[server]"

劇本

# Parameters:
#    server      - server and instance name (e.g. myserver/reportserver or myserver/reportserver_db2)
#    reportpath  - path to report in the report server, including report name e.g. /reports/test report >> pass in  "'/reports/title only'"
#    site        - use "/" for default native mode site
Param
(
      [string]$server,
      [string]$reportpath,
      [string]$site
)

$rs2010 = New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential ;
$subscriptions += $rs2010.ListSubscriptions($site);

Write-Host " "
Write-Host " "
Write-Host "----- $reportpath 's Subscriptions: "
$subscriptions | select Path, report, Description, Owner, SubscriptionID, lastexecuted,Status | where {$_.path -eq $reportpath}

腳本:變更特定訂用帳戶的擁有權

此腳本會變更特定訂用帳戶的擁有權。 訂用帳戶由您傳遞給腳本的 "SubscriptionID" 所識別。 您可以使用其中一個清單訂閱腳本來判斷正確的 SubscriptionID。

原生模式語法

powershell c:\scripts\Change_SSRS_Owner_One_Subscription.ps1 "[Domain]\[new owner]" "[server]/reportserver" "/" "ac5637a1-9982-4d89-9d69-a72a9c3b3150"

SharePoint 模式語法

powershell c:\scripts\Change_SSRS_Owner_One_Subscription.ps1 "[Domain]\[new owner]" "[server]/_vti_bin/reportserver" "http://[server]" "9660674b-f020-453f-b1e3-d9ba37624519"

劇本

# Parameters:
#    newOwner       - DOMAIN\USER that will own the subscriptions you wish to change
#    server         - server and instance name (e.g. myserver/reportserver or myserver/reportserver_db2)
#    site        - use "/" for default native mode site
#    subscriptionID - guid for the single subscription to change

Param(
    [string]$newOwner,
    [string]$server,
    [string]$site,
    [string]$subscriptionid
   )
$rs2010 = New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential;

$subscription += $rs2010.ListSubscriptions($site) | where {$_.SubscriptionID -eq $subscriptionid};

Write-Host " "
Write-Host "----- $subscriptionid's Subscription properties: "
$subscription | select Path, report, Description, SubscriptionID, Owner, Status

$rs2010.ChangeSubscriptionOwner($subscription.SubscriptionID, $newOwner)

#refresh the list
$subscription = $rs2010.ListSubscriptions($site) | where {$_.SubscriptionID -eq $subscriptionid}; # use "/" for default native mode site
Write-Host "----- $subscriptionid's Subscription properties: "
$subscription | select Path, report, Description, SubscriptionID, Owner, Status

腳本:執行(觸發)單一訂閱

此腳本會使用 FireEvent 方法執行特定訂閱。 無論針對訂用帳戶設定的排程為何,腳本都會立即執行訂用帳戶。 EventType 會與報表伺服器組態檔中定義的已知事件集進行比對 ,rsreportserver.config 腳本會針對標準訂用帳戶使用下列事件類型:

<Event>

<Type>TimedSubscription</Type>

</Event>

如需組態檔的詳細資訊,請參閱 RSReportServer 組態檔

腳本包含延遲邏輯“Start-Sleep -s 6”,因此在事件觸發後會有時間更新狀態,使其可供使用於 ListSubscription 方法。

原生模式語法

powershell c:\scripts\FireSubscription.ps1 "[server]/reportserver" $null "70366e82-2d3c-4edd-a216-b97e51e26de9"

SharePoint 模式語法

powershell c:\scripts\FireSubscription.ps1 "[server]/_vti_bin/reportserver" "http://[server]" "c3425c72-580d-423e-805a-41cf9799fd25"

劇本

# Parameters
#    server         - server and instance name (e.g. myserver/reportserver or myserver/reportserver_db2)
#    site           - use $null for a native mode server
#    subscriptionid - subscription guid

Param(
  [string]$server,
  [string]$site,
  [string]$subscriptionid
  )

$rs2010 = New-WebServiceProxy -Uri "http://$server/ReportService2010.asmx" -Namespace SSRS.ReportingService2010 -UseDefaultCredential ;
#event type is case sensative to what is in the rsreportserver.config
$rs2010.FireEvent("TimedSubscription",$subscriptionid,$site)

Write-Host " "
Write-Host "----- Subscription ($subscriptionid) status: "
#get list of subscriptions and filter to the specific ID to see the Status and LastExecuted
Start-Sleep -s 6 # slight delay in processing so ListSubscription returns the updated Status and LastExecuted
$subscriptions = $rs2010.ListSubscriptions($site); 
$subscriptions | select Status, Path, report, Description, Owner, SubscriptionID, EventType, lastexecuted | where {$_.SubscriptionID -eq $subscriptionid}

另請參閱

ListSubscriptions ChangeSubscriptionOwner ListChildren FireEvent