Backup SSRS Encryption Keys Using Powershell

I have made the following script from google for backing up the SSRS Encryption keys:

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()

Open in new window


But I'm getting the following errors:

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

The above code is working for the servers where there is only one named instance of SSRS is running. The above mentioned error is only thrown for servers where there are atleast two named instances are running. kindly look into this and suggest an edit asap.

Thanks
Eldo issacAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Rainer JeschorCommented:
Hi,
perhaps the following Powershell command can help to determine the encoded instance names:
[code]

PS C:\windows\system32> Get-WmiObject –namespace root\Microsoft\SqlServer\ReportServer  –class __Namespace –ComputerName hostname | select Name
[/code]
From
https://msdn.microsoft.com/en-us/library/ms152836(v=sql.110).aspx

HTH
Rainer
0
Eldo issacAuthor Commented:
Thanks Rainer for your reply. The code given by you returns the following output:

Name
--------------
RS_BPSSRS
RS_LNTSSRSDEV

These are the two installed named instanes on the server. Kindly tell now how should I proceed. Thanks again
0
oBdACommented:
Try this; it will automatically determine all RS instances running on the target computer and save the file in the folder specified. The name will be the instance name, followed by a time stamp.
$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 |
	% {
		$KeyFileName = Join-Path -Path $KeyFolder -ChildPath ($_ + $Timestamp + ".snk")
		"Found RS instance '$($_)' on $($ComputerName); will save key to '$($KeyFileName)' ..."
		$SQLVersion = Get-WmiObject -Namespace "Root\Microsoft\SqlServer\ReportServer\$($_)" -Class "__Namespace" -ComputerName $ComputerName |
			Select-Object -ExpandProperty Name 
		$SSRSClass = Get-WmiObject -Namespace "Root\Microsoft\SqlServer\ReportServer\$($_)\$($SQLVersion)\Admin" -Class "MSReportServer_ConfigurationSetting"
		$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()
		}
	}

Open in new window

0
Eldo issacAuthor Commented:
Hello oBdA,

Thanks for your reply. Your code is working perfectly fine for those servers where only one named instance is installed, but not working for those where there are more than one SSRS named instances present. I'm getting the following error:

Found RS instance 'RS_LNTSSRSDEV' on MC1-DQ820.AM.LILLY.COM; will save key to '
C:\Temp\RS_LNTSSRSDEV-20150512-014003.snk' ...
Method invocation failed because [System.Object[]] doesn't contain a method nam
ed 'BackupEncryptionKey'.
At line:15 char:40
+         $Key = $SSRSClass.BackupEncryptionKey <<<< ($KeyPassword)
    + CategoryInfo          : InvalidOperation: (BackupEncryptionKey:String) [
   ], RuntimeException
    + FullyQualifiedErrorId : MethodNotFound

Kindly look into this and give your suggestion.
0
oBdACommented:
Sorry:
$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()
		}
	}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.