SQL Query to Start and or Stop Service on remote computer

I have need to schedule a job in SQL to stop a service on a remote computer, run another job and then to start the service again.
Using sysinternals psservice I can do this from my SQL server from a batch file but have not succeeded in getting this to run from the query.
In my batch file to successfully stop the service I have
c:\_Sysinternals\PsService.exe \\server.xxx.com -u username -p password stop ServiceName

I have tried
EXEC master..xp_CMDShell '"c:\_Sysinternals\PsService.exe" \\server.xxx.com -u username -p password stop ServiceName'

but this runs indefinitely and does nothing.

Please advise the correct syntax.
Paul GAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ste5anSenior DeveloperCommented:
Just use a batch. Stop the remote service, run your local job (using sqlcmd), start the remote service.
Mark WillsTopic AdvisorCommented:
When using xp_cmdshell it takes on the permissions / account settings of the service used to start sql server.

You can control and change that account by using : sp_xp_cmdshell_proxy_account

Even still, the permissions from the Sql Server Service account may not have sufficient permissions to execute some functions.

Ideally, you have a separate service account in the OS, and set it up with all the required privs to run whatever. It is also easier to replicate that account across remote computer as well.

Look at "SQL Server services" in Books On Line. You can also check the setting up of the proxy account.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Marten RuneSQL Expert/Infrastructure ArchitectCommented:
Permisson issue may be the issue.

and you are using " AND ', only use "

Try: exec master..xp_cmdshell "dir c:\"
and se that you get result, then

exec master..xp_cmdshell "dir c:\_Sysinternals"

And to se if you may execute it by:
EXEC master..xp_CMDShell "c:\_Sysinternals\PsService.exe /?"

then go for:
EXEC master..xp_CMDShell "c:\_Sysinternals\PsService.exe \\server.xxx.com -u username -p password stop ServiceName"

Regards Marten
Paul GAuthor Commented:
Thanks for all the input.
I added  -accepteula into the string and then I received an access denied message proving it was a permissions issue.
The clue to the problem was Mark's comment "When using xp_cmdshell it takes on the permissions / account settings of the service used to start sql server."
SQL Server was running under the Local System account and I didn't want to complicate things by adding a proxy account.
Last night I changed this to run under an Admin account.
As soon as I did this it ran fine.
Thanks again.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.