2015-04-27 100 views
0

我從谷歌做出瞭如下腳本備份SSRS加密密鑰:備份SSRS加密密鑰使用PowerShell

cls 
$pwd = "[email protected]@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服務器,手動備份令人厭煩。因此,我們想出了這種自動化。請提供您的意見。

感謝

回答

0

這段代碼應該做的伎倆:

$ComputerName = "." 
$KeyFolder = "C:\Temp" 
$KeyPassword = "[email protected]@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的只有一個命名實例的服務器上嘗試上面的代碼,那麼我認爲它會成功。試試這件作品並發表您的評論。 CHEERS。

+0

感謝您的輸入。有效 – user2068804