I'm trying to run the code below via a script in a SQL agent job on a drive which is failing. When i logon as the service account user and run it in an ISE shell it works fine which leads me to believe it's not access related.
I tried running it as a powershell job step but it wouldn't work so decided to run it as a cmdexec job type and call it like this: "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file "F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1"
$ComputerName = "servername"$KeyFolder = "\\servername\sharename\SSRSKEYS\"
$KeyPassword = "Password1"
$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")
$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()
}
}
Error I am getting:
Executed as user: domain\svc_account. Exception calling "Create" with "2" argument(s): "Access to the path '\\servername\sharename\SSRSKEYS\MSSQLSERVER-20150824-125254.snk' is denied." At F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:24
char:13 + $Stream = [System.IO.File]::Create($KeyFileName, $Key.KeyFile.Length ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId : UnauthorizedAccessException You cannot call a method on a null-valued
expression. At F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:25 char:13 + $Stream.Write($Key.KeyFile, 0, $Key.KeyFile.Length) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull You cannot call a method on a null-valued expression. At
F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:26 char:13 + $Stream.Close() + ~~~~~~~~~~~~~~~ + CategoryInfo
: InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNullException calling "Create" with "2" argument(s): "Access to the path '\\servername\share\SSRSKEYS\RSDYNAMICS-20150824-125254.snk'
is denied." At F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:24 char:13 + $Stream = [System.IO.File]::Create($KeyFileName, $Key.KeyFile.Length ... +
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [], MethodInvocationException + FullyQualifiedErrorId
: UnauthorizedAccessException You cannot call a method on a null-valued expression. At F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:25 char:13 + $Stream.Write($Key.KeyFile, 0, $Key.KeyFile.Length)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId
: InvokeMethodOnNull You cannot call a method on a null-valued expression. At F:\Powershell\ScriptOutSSRSEncryptionKeys.ps1:26 char:13 + $Stream.Close() +
~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull. Process Exit Code 0. The
step succeeded.