locked
SharePoint Remote Powershell Session - Can't find the local farm RRS feed

  • Question

  • Hi,

    I've been trying to configure a remote PS session to connect to a SharePoint Farm to create site objects.  I can get connected and load the SharePoint PS snap in successfully but when I try commands like Get-SPWeb all I get is an error "Cannot access the local farm".

    If I launch PS on the SharePoint server and add the snap in I can use the CMDLETs fine.  I've ensured PS remoting is enabled and granted access so that the client computer is authorised to connect to the SharePoint server.

    Here is the connection code I've used:

    $RunAsAccount = "domain\user"
    $RunAsPass = "password"
    $pass = $RunAsPass | ConvertTo-SecureString -AsPlainText -Force
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $RunAsAccount, $pass
    enter-pssession -computername spserver.domain.local -credential $credentials
    Add-PSSnapin Microsoft.SharePoint.Powershell

    The connection user is a local administrator on the sharepoint server and is a dbowner of the sharepoint config and content databases (for testing) and also has shell access on the sharepoint config DB.

    Any ideas?

    Thanks,

    Tim

    Tuesday, April 8, 2014 11:18 AM

Answers

  • Remoting isn't quite that nice.

    I believe you need to use an invoke-command to run the snapin on the destination server, or import the commandlets locally to slightly different names.

    To borrow the important section for your needs first you'd need to put the session into a variable for later use, then use the invoke-command cmdlet

    "$session = New-PSSession -computername "SPServer" -Authentication CredSSP -Credential $crd

    Start Remoting on you Client Computer
    Now your client computer is ready to actually create the session and import the SharePoint commands from the Server.

    The next command adds the SharePoint PowerShell commands to your session.
    Invoke-Command -Session $session -ScriptBlock{Add-PsSnapin Microsoft.SharePoint.PowerShell}

    Note, that at this point, you can run whatever administrative PowerShell commands you need to run by using the Invoke-Command -Session $session -ScriptBlock{} syntax."

    http://blogs.msdn.com/b/varun_malhotra/archive/2010/06/10/configure-power-shell-for-remote-use-of-sp-2010.aspx

    • Marked as answer by The_Ronin_69 Tuesday, April 8, 2014 1:09 PM
    Tuesday, April 8, 2014 11:52 AM

All replies

  • Remoting isn't quite that nice.

    I believe you need to use an invoke-command to run the snapin on the destination server, or import the commandlets locally to slightly different names.

    To borrow the important section for your needs first you'd need to put the session into a variable for later use, then use the invoke-command cmdlet

    "$session = New-PSSession -computername "SPServer" -Authentication CredSSP -Credential $crd

    Start Remoting on you Client Computer
    Now your client computer is ready to actually create the session and import the SharePoint commands from the Server.

    The next command adds the SharePoint PowerShell commands to your session.
    Invoke-Command -Session $session -ScriptBlock{Add-PsSnapin Microsoft.SharePoint.PowerShell}

    Note, that at this point, you can run whatever administrative PowerShell commands you need to run by using the Invoke-Command -Session $session -ScriptBlock{} syntax."

    http://blogs.msdn.com/b/varun_malhotra/archive/2010/06/10/configure-power-shell-for-remote-use-of-sp-2010.aspx

    • Marked as answer by The_Ronin_69 Tuesday, April 8, 2014 1:09 PM
    Tuesday, April 8, 2014 11:52 AM
  • Hi,

    I've been trying to configure a remote PS session to connect to a SharePoint Farm to create site objects.  I can get connected and load the SharePoint PS snap in successfully but when I try commands like Get-SPWeb all I get is an error "Cannot access the local farm".

    If I launch PS on the SharePoint server and add the snap in I can use the CMDLETs fine.  I've ensured PS remoting is enabled and granted access so that the client computer is authorised to connect to the SharePoint server.

    Here is the connection code I've used:

    $RunAsAccount = "domain\user"
    $RunAsPass = "password"
    $pass = $RunAsPass | ConvertTo-SecureString -AsPlainText -Force
    $credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $RunAsAccount, $pass
    enter-pssession -computername spserver.domain.local -credential $credentials
    Add-PSSnapin Microsoft.SharePoint.Powershell

    The connection user is a local administrator on the sharepoint server and is a dbowner of the sharepoint config and content databases (for testing) and also has shell access on the sharepoint config DB.

    Any ideas?

    Thanks,

    Tim

    a few heads up:

    1: Enter-PSSesion only works interactively. That means, it will not work using script, but only from prompt.

    2: Invoke-command seems to be the most stable method of using remote PS. Here is a few examples

    http://jesperarnecke.wordpress.com/2014/01/28/invoke-command-remote-powershell-variables/

    Tuesday, April 8, 2014 12:04 PM
  • Thanks for the help guys.  That's a great explanation Alex.  It works a treat.  Thank you!
    Tuesday, April 8, 2014 1:11 PM