Unable to update log on credential of service via powershell

Deepa Annie 20 Reputation points
2023-08-25T13:51:25.48+00:00

Powershell verision is 5.1

i am able to update the username and password manually of a server.

I am unable to achieve that through powershell.

tried the below. am getting return value 15 when i start
$account = "<the account name>" $password = "<the account password>" $servicename = "name='<the service name>'" $svc = gwmi win32_service -filter #--returnvalue is 0

$servicename $svc.StopService() #--returnvalue is 5

$svc.change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null) $svc.StartService() #--returnvalue is 15

i am unable to start it manually when the credential is set through powershell

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,879 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,602 questions
0 comments No comments
{count} votes

Accepted answer
  1. MotoX80 35,131 Reputation points
    2023-08-30T13:56:01.8666667+00:00

    The account that you use needs to have the "logon as service" right. If you want to do this strictly from a command line, then my preferred solution would be the ntrights program from the Windows 2003 Resourse Kit. Unfortunatly Microsoft has chosen to remove that download, so you will need another solution.

    Here's one.

    https://blakedrumm.com/blog/set-and-check-user-rights-assignment/

    https://github.com/blakedrumm/SCOM-Scripts-and-SQL/blob/master/Powershell/General%20Functions/Set-UserRights.ps1

    Download Get-UserRights and Set-UserRights.

    $user = "YourDomain\YourUser"
    $pswd = "password" 
    $svc = "YourServiceName" 
    
    Set-UserRights.ps1 -AddRight -UserRight SeServiceLogonRight -Username $user 
    sc.exe config $svc obj= $user password= $pswd
    Start-Service -Name $svc 
    
    
    
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Ian Xue 39,021 Reputation points Microsoft Vendor
    2023-08-28T06:38:08.8233333+00:00

    Hi,

    The return value 15 indicates Service Logon Failed. Did you provide the correct account and password?

    https://learn.microsoft.com/en-us/windows/win32/cimwin32prov/change-method-in-class-win32-service

    Or you can give a try to sc.exe.

    https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/sc-create

    Best Regards,

    Ian Xue


    If the Answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.