Powershell script to change service account

前端 未结 10 2350
小蘑菇
小蘑菇 2020-12-15 03:07

Does anyone have a Powershell script to change the credentials used by a Windows service?

10条回答
  •  失恋的感觉
    2020-12-15 03:31

    I created a text file "changeserviceaccount.ps1" containing the following script:

    $account="domain\user"
    $password="passsword"
    $service="name='servicename'"
    
    $svc=gwmi win32_service -filter $service
    $svc.StopService()
    $svc.change($null,$null,$null,$null,$null,$null,$account,$password,$null,$null,$null)
    $svc.StartService()
    

    I used this as part of by post-build command line during the development of a windows service:

    Visual Studio: Project properties\Build Events

    Pre-build event command line:

    "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe" myservice.exe /u
    

    Post-build event command line:

    "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\installutil.exe" myservice.exe
    powershell -command - < c:\psscripts\changeserviceaccount.ps1
    

提交回复
热议问题