使用Powershell备份SSRS加密密钥

时间:2015-04-27 12:21:56

标签: powershell-v2.0 ssrs-2008-r2

我已经从谷歌制作了以下脚本来备份SSRS加密密钥:

cls
$pwd = "sa@123@123"
$SSRSClass = Get-Wmiobject -namespace "root\microsoft\sqlserver\reportserver\rs_BPSSRS\v10\admin" -class "MSReportServer_ConfigurationSetting"

$key = $SSRSClass.BackupEncryptionKey($pwd)
$stream = [System.IO.File]::Create("c:\\SSRS.snk", $key.KeyFile.Length)
$stream.Write($key.KeyFile, 0, $key.KeyFile.Length)
$stream.Close()

但我收到以下错误:

Method invocation failed because [System.Object[]] doesn't contain a method named 'BackupEn
cryptionKey'.
At line:5 char:38
+ $key = $SSRSClass.BackupEncryptionKey <<<< ($results)
    + CategoryInfo          : InvalidOperation: (BackupEncryptionKey:String) [], RuntimeEx 
   ception
    + FullyQualifiedErrorId : MethodNotFound

Exception calling "Create" with "2" argument(s): "Positive number required.
Parameter name: bufferSize"
At line:6 char:35
+ $stream = [System.IO.File]::Create <<<< ("c:\\SSRS.snk", $key.KeyFile.Length)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

You cannot call a method on a null-valued expression.
At line:7 char:14
+ $stream.Write <<<< ($key.KeyFile, 0, $key.KeyFile.Length)
    + CategoryInfo          : InvalidOperation: (Write:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

You cannot call a method on a null-valued expression.
At line:8 char:14
+ $stream.Close <<<< ()
    + CategoryInfo          : InvalidOperation: (Close:String) [], RuntimeException
    + FullyQualifiedErrorId : InvokeMethodOnNull

我正在使用PowerShell v2。我试着找到这个,但没有运气。我们的环境中有大约50多台SSRS服务器,手动备份是令人厌倦的。因此,我们想出了这种自动化。请提供您的意见。

由于

1 个答案:

答案 0 :(得分:0)

这段代码应该可以解决问题:

$ComputerName = "."
$KeyFolder = "C:\Temp"
$KeyPassword = "sa@123@123"
$TimeStamp = Get-Date -Format "-yyyyMMdd-HHmmss"

Get-WmiObject -Namespace "Root\Microsoft\SqlServer\ReportServer" -Class "__Namespace" -ComputerName $ComputerName |
    Select-Object -ExpandProperty Name |
    % {
        $NameSpaceRS = $_
        $InstanceName = $NameSpaceRS.SubString(3)
        $KeyFileName = Join-Path -Path $KeyFolder -ChildPath ($InstanceName + $Timestamp + ".snk")
        "Found Reporting Services in instance '$($InstanceName)' on $($ComputerName); will save key to '$($KeyFileName)' ..."
        $SQLVersion = (Get-WmiObject -Namespace "Root\Microsoft\SqlServer\ReportServer\$($NameSpaceRS)" -Class "__Namespace" -ComputerName $ComputerName).Name
        $SSRSClass = Get-WmiObject -Namespace "Root\Microsoft\SqlServer\ReportServer\$($NameSpaceRS)\$($SQLVersion)\Admin" -Query "SELECT * FROM MSReportServer_ConfigurationSetting WHERE InstanceName='$($InstanceName)'" -ComputerName $ComputerName
        $Key = $SSRSClass.BackupEncryptionKey($KeyPassword)
        If ($Key.HRESULT -ne 0) {
            $Key.ExtendedErrors -join "`r`n" | Write-Error
        } Else {
            $Stream = [System.IO.File]::Create($KeyFileName, $Key.KeyFile.Length)
            $Stream.Write($Key.KeyFile, 0, $Key.KeyFile.Length)
            $Stream.Close()
        }
    }

您提到的错误是因为您的代码试图从重复条目中获取信息。这意味着,如果您在服务器上尝试上面的代码只运行一个命名的SSRS实例,那么我想它会成功。试试这篇文章并发表您的意见。 欢呼声。