从 SQL Server 2008 R2Reporting Services 开始,你可以以编程方式将 Reporting Services 订阅的所有权从一个用户转移到另一个用户。 本主题提供了多个可用于更改或仅列出订阅所有权的 Windows PowerShell 脚本。 每个示例都包含本机模式和 SharePoint 模式的语法示例。 更换订阅所有者后,订阅将在新所有者的安全上下文中执行,报表中的 User!UserID 字段将显示为新所有者的身份。 有关 PowerShell 示例调用的对象模型的详细信息,请参阅 ChangeSubscriptionOwner
| 适用于:Reporting Services 本机模式 | Reporting Services SharePoint 模式 |
本主题内容:
如何使用脚本
权限
本节总结了使用各本机模式和 SharePoint 模式 Reporting Services 方法所需的权限级别。 本主题中的脚本使用以下 Reporting Services 方法:
ReportingService2010.FireEvent 方法仅在最后一个脚本中使用,以触发特定订阅运行。 如果不打算使用该脚本,则可以忽略 FireEvent 方法的权限要求。
本机模式:
列出订阅:(HYPERLINK “https://technet.microsoft.com/library/microsoft.reportingservices.interfaces.reportoperation.aspx" 用户具有报表上的 ReadSubscription 权限,且是订阅所有者)或具有 ReadAnySubscription 权限
更改订阅:用户必须是 BUILTIN\Administrators 组的成员
列出子级:项上的 ReadProperties
触发事件:GenerateEvents(系统)
SharePoint 模式:
列出订阅:ManageAlerts OR (HYPERLINK “https://technet.microsoft.com/library/microsoft.sharepoint.spbasepermissions.aspx"报表上的 CreateAlerts,并且用户是订阅所有者,订阅是计时订阅。
更改订阅:ManageWeb
列出子级:ViewListItems
触发事件:ManageWeb
有关详细信息,请参阅 “将 Reporting Services 中的角色和任务与 SharePoint 组和权限进行比较”。
脚本用法
创建脚本文件 (.ps1)
创建一个名为 c:\scripts的文件夹。 如果选择其他文件夹,则修改示例命令行语法语句中使用的文件夹名称。
为每个脚本创建文本文件并将文件保存至 c:\scripts 文件夹。 创建 .ps1 文件时,请使用每个示例命令行语法中的名称。
使用管理员权限打开命令提示符。
使用每个示例提供的示例命令行语法运行每个脚本文件。
经测试的环境
本主题中的脚本在 PowerShell 版本 3 和以下 Reporting Services 版本中进行了测试:
SQL Server 2014
SQL Server 2012
SQL Server 2008 R2
脚本:列出所有订阅的所有权
此脚本列出站点上的所有订阅。 可以使用此脚本测试连接或验证报表路径和订阅 ID,以便在其他脚本中使用。 这也是一个有用的脚本,用于审核存在哪些订阅及其所有者。
原生模式语法
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。
脚本:列出特定用户拥有的全部订阅
此脚本列出特定用户拥有的全部订阅。 可以使用此脚本测试连接或验证报表路径和订阅 ID,以便在其他脚本中使用。 当组织中的某人离开并想要验证他们拥有哪些订阅,以便更改所有者或删除订阅时,此脚本非常有用。
本机模式语法
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。 在语法示例中,使用的报表名称是“仅标题”,其中包含空格,因此需要报表名称周围的单引号。
本机模式语法
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