2

I have a file "test.ps1" and its content is as below:

$getRSDBName =
{
    Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $rsdb = Get-SPRSDatabase

    return $rsdb
}


$rsdb = invoke-command  -scriptblock $getRSDBName 

$rsdb

It work if I call it as:

powershell -f c:\test.ps1

I get an error if I use WinRS:

winrs -r:xxxxx -u:xxxxxx -p:xxxxx powershell -f c:\test.ps1

I also tried it by inputting -Authentication CredSSP -Credential $creds as parameters of Invoke-Command, but I got the same result as before. In both instances, the error is:

The local farm is not accessible. Cmdlets with FeatureDependencyId are not registered. Get-SPRSDatabase : Cannot access the local farm. Verify that the local farm is properly configured, currently available, and that you have the appropriate per missions to access the database before trying again. At C:\clean.ps1:181 char:29 + $rsdb = Get-SPRSDatabase <<<< + CategoryInfo : InvalidData: (Microsoft.Repor...ServiceDatabase: GetReportingServiceDatabase) [Get-SPRSDatabase], SPCmdletException + FullyQualifiedErrorId : Microsoft.ReportingServices.SharePoint.PowerShell.GetReportingServiceDatabase

Could someone explain what is happening?

Anthony Neace
  • 25,013
  • 7
  • 114
  • 129
user1504146
  • 79
  • 2
  • 8
  • Is PowerShell Remoting [enabled](http://technet.microsoft.com/en-us/magazine/ff700227.aspx) on the remote host? Does the execution policy on all hosts allow execution of PowerShell scripts? – Ansgar Wiechers Mar 31 '13 at 11:33

2 Answers2

1

that's caused by the "second hop" issue described here when the commandlet on the remote server tries in turn to reach the sql database (it has to pass credential to it, but they cannot be delegated a second time)

to solve it run the following on the server

Enable-WSManCredSSP –Role server

and this on the client (requires elevated privileges)

Enable-WSManCredSSP –Role client –DelegateComputer *

then remember to use

$session = New-PSSession -ComputerName $computer -Credential $credential -Authentication CredSSP

when you open the remote session, and pass $session to invoke-command

Invoke-Command -ScriptBlock $doSomething -Session $session  

this solved for me :) in general i've found that any strange issue i find when using invoke-command is somehow related to the second hop problem. That actually is a security feature, therefore it is here for help us even if it is a little boring, but is not that hard to solve it

Mosè Bottacini
  • 4,016
  • 2
  • 24
  • 28
0

Are you able to create a PS-Session and then run the script within that remote shell? If so, why not add a create/destroy session to your current script?

$Session = New-PSSession -ComputerName "computerNameHere"
Import-PSSession -Session $Session

$getRSDBName = {
    Add-PSSnapIn Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
    $rsdb = Get-SPRSDatabase

    return $rsdb
}
$rsdb = invoke-command  -scriptblock $getRSDBName 
$rsdb

Remove-PSSession -ComputerName "computerNameHere"
Adam
  • 1