Sitecore PowerShell Extensions (SPE) Remoting Baby Steps

Just took some baby steps towards SPE remoting today. Here are the steps I followed:

1. Started simple by installing a physical Sitecore 10.1 Instance using Sitecore Complete Install Assistant (SCIA), parked Docker for later!


The best part about SCIA is, it pre-installs SPE 6.2 installs for you. So, we are covered there.

Once installation is done, you can check the module installed here -


2. Next, the most important part! Since we want to execute SPE remoting commands, let's install the SPE remoting module like this through PowerShell window -

Install-Module -Name SPE

3. Based on this page, you should be able to use the following commands as part of SPE 6.2:


4. Its time to test one of those and Invoke-RemoteScript is one of the relevant ones. So, let's try executing the following command in PowerShell Window:

 Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id admin }

I get the following error:

Invoke-RemoteScript : Cannot validate argument on parameter 'Session'. The argument is null. Provide a valid value for

the argument, and then try running the command again.

At line:1 char:31

+  Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id ad ...

+                               ~~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Invoke-RemoteScript], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Invoke-RemoteScript


The good thing about the above error is that the command is recognised but there is no session to execute.

5. So, I created a session as the first step:

$session = New-ScriptSession -Username admin -Password b -ConnectionUri https://sc101spenewsc.dev.local/


6. If you execute $session, you should see the session details now:


7. So, we seem good to execute the original command now. So, let's try the same:

Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id admin }

Then, I received this error:

Invoke-RemoteScript : Server response: The request could not be completed because the remoting service is disabled.

At line:1 char:1

+ Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id adm ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ConnectionError: (https://sc101spenewsc.dev.local/:) [Write-Error], Forbidden

    + FullyQualifiedErrorId : System.Net.Http.HttpRequestException,Invoke-RemoteScript


8. I executed the following command 

Set-ExecutionPolicy RemoteSigned

and, retried the same cmdlet but received the same error.

9. Then, based on the SPE documentation, I checked my spe.config (in my case, under C:\inetpub\wwwroot\sc101spenewsc.dev.local\App_Config\Include\Spe\spe.config) and realized remoting was disabled and enabled the same. Ideally, this should be patched but since I was just testing, I made the change to spe.config itself -


10. Re-executed the Invoke-RemoteScript one more time to find a different error this time:

Invoke-RemoteScript : Server response: The specified user sitecore\admin is not authorized for the remoting service.

At line:1 char:1

+ Invoke-RemoteScript -Session $session -ScriptBlock { Get-User -id adm ...

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : ConnectionError: (https://sc101spenewsc.dev.local/:) [Write-Error], Unauthorized

    + FullyQualifiedErrorId : System.Net.Http.HttpRequestException,Invoke-RemoteScript


11. I reopened the spe.config to investigate more and found that the admin user is denied permission to remoting by default and so, added the same:


12. Back in the PS window and executed  Invoke-RemoteScript  to see it successfully executed this time:



Somehow, reminded of this Paw Patrol catch line!



Comments

  1. Hello,
    I did all this, but I am still getting an error while executing remote PS scripts in Sitecore. Any idea why?
    Error:
    Invoke-RemoteScript -Session $session -ScriptBlock {Get-User -Id devops_admin} | Get-Member
    Invoke-RemoteScript : Server response: The specified user extranet\Anonymous is not authorized for the remoting service.
    At line:5 char:1
    + Invoke-RemoteScript -Session $session -ScriptBlock {Get-User -Id devo ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : ConnectionError: (http://tst-cms.svccorp.com/:) [Write-Error], Unauthorized
    + FullyQualifiedErrorId : System.Net.Http.HttpRequestException,Invoke-RemoteScript
    Get-Member : You must specify an object for the Get-Member cmdlet.
    At line:5 char:82
    + ... ession $session -ScriptBlock {Get-User -Id devops_admin} | Get-Member
    + ~~~~~~~~~~
    + CategoryInfo : CloseError: (:) [Get-Member], InvalidOperationException
    + FullyQualifiedErrorId : NoObjectInGetMember,Microsoft.PowerShell.Commands.GetMemberCommand

    ReplyDelete
    Replies
    1. I have no idea why extranet\anonymous user is being used here.

      Delete
    2. Looks like you logged in with extranet\anonymous and then trying to get info about devops_admin user... so, first ensure that extranet\anonymous is allowed permission in spe.config by adding this line like I have done for sitecore\admin

      That should work. Also, remove Get-Member from your cmdlet if there is error thrown while you execute: Invoke-RemoteScript -Session $session -ScriptBlock {Get-User -Id devops_admin} | Get-Member

      Delete

Post a Comment