Powershell script to change service account

前端 未结 10 2412
小蘑菇
小蘑菇 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:41

    Considering that whithin this class:

    $class=[WMICLASS]'\\.\root\Microsoft\SqlServer\ComputerManagement:SqlService'
    

    there's a method named setserviceaccount(), may be this script will do what you want:

    # Copyright Buck Woody, 2007
    # All scripts provided AS-IS. No functionality is guaranteed in any way.
    # Change Service Account name and password using PowerShell and WMI
    $class = Get-WmiObject -computername "SQLVM03-QF59YPW" -namespace
    root\Microsoft\SqlServer\ComputerManagement -class SqlService
    
    #This remmed out part shows the services - I'll just go after number 6 (SQL
    #Server Agent in my case):
    # foreach ($classname in $class) {write-host $classname.DisplayName}
    # $class[6].DisplayName
    stop-service -displayName $class[6].DisplayName
    
    # Note: I recommend you make these parameters, so that you don't store
    # passwords. At your own risk here!
    $class[6].SetServiceAccount("account", "password")
    start-service -displayName $class[6].DisplayName
    

提交回复
热议问题