我有一个Powershell脚本,我正致力于迁移后SSRS报告管理任务。
在这个特定的场景中,我们有一个DEV环境(我主要在那里测试),它承载一个SSRS实例,以及一个Prod环境,它是一个跨4个节点的扩展部署。
我是Powershell的新手(刚刚在2天前发现了......),我的脚本非常简单:
Clear-Host
$Username = "domain\myUsername"
$Password = "myPassword"
$Cred = New-Object System.Management.Automation.PSCredential -ArgumentList @($Username,(ConvertTo-SecureString -String $Password -AsPlainText -Force))
# Dev Connection String
$webServiceUrl = 'http://DEVwebServer.domain.com/reportserver/reportservice2010.asmx?WSDL'
# Prod Connection String
# $webServiceUrl = 'http://PRODwebServerNode1.domain.com/reportserver/reportservice2010.asmx?WSDL'
$rs = New-WebServiceProxy -Uri $webServiceUrl -Credential $Cred
$reports = $rs.ListChildren("/Some Folder Under Root", $true) | Where-Object { $_.TypeName -eq "Report" }
$type = $ssrsProxy.GetType().Namespace;
$schedDefType = "{0}.ScheduleDefinition" -f $type;
$schedDef = New-Object ($schedDefType)
$warning = @();
foreach ($report in $reports) {
$sched = $rs.GetExecutionOptions($report.Path, [ref]$schedDef);
$snapShotExists = $rs.ListItemHistory($report.Path);
if($sched -eq "Snapshot") {
Write-Host "Following report is configured to run from Snapshot:" -ForegroundColor Yellow
Write-Host ("Report Name: {0}`nReport Path: {1}`nExecution Type: {2}`n" -f $report.Name, $report.Path, $sched)
if ($snapShotExists) {
Write-Host "Does Snapshot Exist..?`n" -ForegroundColor Yellow
Write-Host "Yes!`tNumber of Snapshots: " $snapShotExists.Count -ForegroundColor Green
$snapShotExists.CreationDate
Write-Host "`n------------------------------------------------------------"
}
elseif (!$snapShotExists) {
Write-Host "Does Snapshot Exist..?`n" -ForegroundColor Yellow
Write-Host ("No!`n") -ForegroundColor Red
Write-Host "Creating Snapshot.......`n" -ForegroundColor Yellow
$rs.CreateItemHistorySnapshot($report.Path, [ref]$warning);
Write-Host "Snapshot Created!`n" -ForegroundColor Green
$snapShotExists.CreationDate
Write-Host "`n------------------------------------------------------------"
}
}
}
脚本的目的只是递归迭代$ reports变量中给定文件夹的所有报告,检查执行类型是否设置为" Snapshot",如果是检查一下"历史快照"存在,如果不存在,则创建一个。
当我在Dev中运行它时效果很好,但是当我在PROD中运行时,我在foreach循环中为每个$ report重复了以下错误:
关于为什么会在一个而不是另一个中工作的任何想法以及如何克服这个错误?
答案 0 :(得分:0)
通过使用this answer作为指导进行一些调整,我能够在Prod实例上工作:
通过更新我对New-WebServiceProxy的调用来添加Class和Namespace标志,我能够通过以下方式更新脚本:
...
# Add Class and Namespace flags to New-WebServiceProxy call
$rs = New-WebServiceProxy -Class 'RS' -Namespace 'RS' -Uri $webServiceUrl -Credential $Cred
$reports = $rs.ListChildren("/Business and Technology Solutions", $true) | Where-Object { $_.TypeName -eq "Report" }
# Declare new "ScheduleDefintion object using the Class declared in the New-WebServiceProxy call
$schedDef = New-Object RS.ScheduleDefinition
$warning = @();
foreach ($report in $reports) {
# Referencing the "Item" property from the ScheduleDefinition
$execType = $rs.GetExecutionOptions($report.Path, [ref]$schedDef.Item)
...
我不认为在New-WebServiceProxy调用中添加Class和Namespace标志正是它所做的,因为我认为它只是一种更清晰的方法来确保从WebService获取正确的命名空间。也许只是一点糖。
我认为关键的变化是确保计划定义对象中的“Item”属性,虽然我不确定为什么它在Dev中没有这样做...