簡體   English   中英

使用Powershell打開SSRS項目

[英]Opening a SSRS project using Powershell

我有一個報告被復制到許多不同的服務器。 它是手動導入的,並且數據源屬性被更改以匹配當前服務器的規范。 我希望能夠通過允許用戶打開SSRS報告並通過PowerShell動態更改其共享數據源屬性來自動化該過程。 我希望你能幫忙。 您可能會在下面看到參考。

該報告位於以下位置:

在此輸入圖像描述

該腳本將接受servername,username和password的輸入參數。 此外,必須勾選保存我的密碼。

我簡直不敢相信我設法為此創建了一個腳本。 您可以使用下面的腳本作為將來的參考。 每個部分都有注釋,任何需要更改的內容都有一個“here”關鍵字,例如。 Your_database_name_here。

Import-Module SqlPs

#Input parameter to get Server\Instancename of your Datasource
$Servername = Read-Host "Please enter your Servername"
$Instancename = Read-Host "Please enter your Instancename. For default instance please press enter"
Write-host ""

if ($Instancename -eq ""){
   $ServerInstance = $Servername
   }
Else {
   $ServerInstance = $Servername +"\"+ $InstanceName
}

#Setting up SSRS Target URL. This is the location where your reports would be deployed.
if ($Instancename -eq ""){
   $ReportServerUri = "http://$Servername/ReportServer//ReportService2010.asmx?wsdl"
   $TargetURL = "http://$Servername/Reports"
   }
Else {
   $ReportServerUri = "http://$Servername/ReportServer_$Instancename//ReportService2010.asmx?wsdl"
   $TargetURL = "http://$Servername/Reports_$Instancename"
}

$global:proxy = New-WebServiceProxy -Uri $ReportServerUri -UseDefaultCreden



#We would make use of SQL Server Authentication for the reports shared datasource so you need to supply a username and password.
Write-Host "     SQL Server Authentication:"
$Username = Read-Host "     Username"
$Password = Read-Host -AsSecureString "Password" 


$type = $Proxy.GetType().Namespace
$datatype = ($type + '.Property')

$property =New-Object ($datatype);
$property.Name = “NewFolder”
$property.Value = “NewFolder”

$numproperties = 1
$properties = New-Object ($datatype + '[]')$numproperties 
$properties[0] = $property;

$newFolder = $proxy.CreateFolder("Reports”, “/”, $properties);
$newFolder = $proxy.CreateFolder("Data Sources”, “/”, $properties);

$Children =$proxy.ListChildren("/",$false)
$DBname = 'Your_Database_Name_Here'


# Creating Datasource through powershell
Write-Host "     Creating Datasource ..."
$Name = "Name_Your_Datasource_here"
$Parent = "/Data Sources"
$ConnectString = "data source=$Servername\$Instancename;initial catalog=$DBname"
$type = $Proxy.GetType().Namespace
$DSDdatatype = ($type + '.DataSourceDefinition')
$DSD = new-object ($DSDdatatype)
if($DSD -eq $null){
      Write-Error Failed to create data source definition object
}
$CredentialDataType = ($type + '.CredentialRetrievalEnum')
$Cred = new-object ($CredentialDataType)
$CredEnum = ($CredentialDataType).Integrated
$Cred.value__=1 

$DSD.CredentialRetrieval =$Cred
$DSD.ConnectString = $ConnectString
$DSD.Enabled = $true
$DSD.EnabledSpecified = $false
$DSD.Extension = "SQL"
$DSD.ImpersonateUserSpecified = $false
$DSD.Prompt = $null
$DSD.WindowsCredentials = $false
$DSD.UserName = $Username
$DSD.Password = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($Password))

$newDSD = $proxy.CreateDataSource($Name,$Parent,$true,$DSD,$null)

#Deploying RLD files to Target URL
Write-Host "     Deploying RDL files ..."
$stream = Get-Content 'D:\Your_RDL_path_here.rdl' -Encoding byte
$warnings =@();
$proxy.CreateCatalogItem("Report","Report_Name_here","/Reports",$true,$stream,$null,[ref]$warnings)


#Let's make use of the datasource we just created for your RDL files.
$Items = $global:proxy.listchildren("/Data Sources", $true) 
foreach ($item in $items)
{

$DatasourceName = $item.Name
$DatasourcePath = $item.Path
}


$RDLS = $global:proxy.listchildren("/Reports", $true) 
foreach ($rdl in $rdls)
{
$report = $rdl.path


$rep = $global:proxy.GetItemDataSources($report)
$rep | ForEach-Object {
$proxyNamespace = $_.GetType().Namespace
    $constDatasource = New-Object ("$proxyNamespace.DataSource")
    $constDatasource.Name = $DataSourceName
    $constDatasource.Item = New-Object ("$proxyNamespace.DataSourceReference")
    $constDatasource.Item.Reference = $DataSourcePath

$_.item = $constDatasource.Item
$global:proxy.SetItemDataSources($report, $_)
Write-Host "Changing datasource `"$($_.Name)`" to $($_.Item.Reference)"
}
}

#Open a IE browser to view the report.
$IE=new-object -com internetexplorer.application
$IE.navigate2($TargetURL)
$IE.visible=$true
Write-Host ""     
Write-Host "You may now view the Reports through the open IE browser."
Write-Host -ForegroundColor Green "**STEP COMPLETED!"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM