BodiDani
BodiDani

Reputation: 43

PowerShell remote restart service running from a TFS build error

I have a TFS build definition contains a PowerShell Script build step. I would like to run the following command on a remote computer(DEVWS45PC) which should restart the service named StartSeleniumGridHub:

winrm s winrm/config/client '@{TrustedHosts="DEVWS45PC"}'

Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSeleniumGridHub)

This gives me this error:

##[error]Get-Service : Cannot find any service with service name 'StartSeleniumGridHub'.
At E:\builds_2017\killBrowsersOnAllNodesAndRestartHub.ps1:43 
char:36
+     Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSe ...
+                                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (StartSeleniumGridHub:String) [Get-Service], ServiceCommandException
    + FullyQualifiedErrorId : NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand

Restart-Service : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an 
argument that is not null or empty, and then try the command again.
At E:\builds_2017\killBrowsersOnAllNodesAndRestartHub.ps1:43 
char:34
+     Restart-Service -InputObject $(Get-Service -Computer DEVWS45PC -Name StartSe ...
+                                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Restart-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.RestartServiceCommand

I can run this script without any problem from any of my computers. Note that 'DEVWS45PC' is truly set as a trusted host on the TFS build agent computer, so it's not the problem.

I have no clue what is the problem. Maybe authentication problems?

Upvotes: 0

Views: 377

Answers (1)

PatrickLu-MSFT
PatrickLu-MSFT

Reputation: 51143

You could manually RDP to the remote machine DEVWS45PC with your build service account and run the powershell script.

Most likely lacking of permissions of your build service account(Due to could not find the service). Suggest you add the service account to your local Administrator group on the remote machine DEVWS45PC and try again.

Also make sure you are using the PowerShell on Target Machines task instead of powershell task in the build definition.

Upvotes: 0

Related Questions