Powershell script that executes another powershell remotely

powershellremotescriptingservice

It's a long story, but I need to create a SQL trigger that executes a script remotely which executes another script remotely (yeah, I know). I'm using PowerShell.

I have started at the end:

Get-Service –Name “service” –ComputerName “Server2” | Set-service –Status Stopped

Get-Service –Name “service” –ComputerName “Server2” | Set-service –Status Running

This works just fine.
Then, the script that will be executed on the trigger:

Invoke-Command –computername “cluster” –command {d:\adminscripts\RestartServer2Service.ps1}    

(yes, I use a NLB cluster, and it's reading the script)
But, I get this:

Cannot find any service with service name ‘service’.

Any thoughts? Any help will be appreciated.
Thanks

Best Answer

This is almost certainly a two-hop issue. If I use Enter-PSSession to connect to ServerA, and then run Get-Service spooler -ComputerName ServerB, I get the exact same "cannot find any service with service name" error.

However, if I run Enter-PSSession ServerA -Authentication CredSSP -Credential (Get-Credential), then the Get-Service command runs fine.

The problem is the second hop -- going from the first remote computer to the second (and third); it's a "bridge too far" for the default authentication protocol used by PS remoting. The solution that I use for this sort of problem is CredSSP.

You can find more details about the problem and how to enable/use CredSSP in this Scripting Guy article.