Powershell script that executes another powershell remotely
Solution 1
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.
Solution 2
-command
is not a valid parameter for Invoke-Command
. Use -FilePath
instead to trigger your second script.
Also, ensure you have the proper credentials to execute these scripts.
Related videos on Youtube
Horaceman
Updated on September 18, 2022Comments
-
Horaceman almost 2 years
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
-
Get-HomeByFiveOClock over 9 yearsRight off the top of my head, service names are case sensitive. Is this service actually named "service"? or maybe "Service"? In fact, are you sure it's even named "service"?
-
Horaceman over 9 yearsThanks for trying ehhe. But the name it's ok. The first script (those two lines), which it's restartServer2Service.ps1 works fine. It's only when i use the invoke-command when it does not.
-
-
Horaceman over 9 yearsI finally ended up doing something to avoid it, because my first hop could call itself and the server/client thing was chaotic. But you were right. Thanks.