Important Notice: On February 29th, this community was put into read-only mode. All existing posts will remain but customers are unable to add new posts or comment on existing. Please feel to join our Community Discord for any questions and discussions.

Change a Service to a Network account after install

I am installing a program called Elastic Agent, the install works fine but after install, I need to stop the service, change the Log On As account and restart it to finalize. I have tried different methods to get this accomplished and have had various results and no consistency on whether it works or not.

How can I get this accomplished with a high success rate?

1

Comments

2 comments
Date Votes
  • Here's another option to try:

    $ServiceName = 'Elastic Agent'
    $UserName = 'Fancy Name'

    # Please use a safe way to handle passwords, such as one of the methods from Kris's blogs:
    # https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-1/
    # https://www.pdq.com/blog/secure-password-with-powershell-encrypting-credentials-part-2/
    $Password = 'Fancy Password'

    Stop-Service -Name $ServiceName

    # Change the 'Log on as' credential.
    # Inspired by: https://adamtheautomator.com/powershell-service-logon-account/
    $ServiceObject = Get-WmiObject -Query "SELECT * FROM Win32_Service WHERE Name = '$ServiceName'"
    $null = $ServiceObject.Change($null,$null,$null,$null,$null,$null,$UserName,$Password)

    Start-Service -Name $ServiceName
    1
  • You can use a Powershell step after the install is done, you can use powershell step and use set-service and specify the log on account and use restart service

    1