【问题标题】:Opening a SSRS project using Powershell使用 Powershell 打开 SSRS 项目
【发布时间】:2016-06-28 08:39:16
【问题描述】:

我有一份报告被复制到多个不同的服务器。它是手动导入的,并且数据源属性被更改以匹配当前服务器的规格。我希望能够通过使用户能够打开 SSRS 报告并通过 PowerShell 动态更改其共享数据源属性来自动化该过程。我希望你能帮忙。您可能会在下面看到参考。

脚本将接受服务器名称、用户名和密码的输入参数。另外,保存我的密码必须打勾。

【问题讨论】:

    标签: powershell reporting-services ssms-2012


    【解决方案1】:

    我不敢相信我设法为此创建了一个脚本。您可以使用下面的脚本作为将来的参考。每个部分都有注释,任何需要更改的内容都有一个“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!"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-10
      • 2020-05-30
      • 1970-01-01
      • 2012-02-07
      • 2015-01-13
      相关资源
      最近更新 更多