How to use Windows Management Instrumentation in SQL Server 2005 to change the account of the SQL Server service or the password of the SQL Server service programmatically

Article translations Article translations
Article ID: 936492 - View products that this article applies to.
Expand all | Collapse all

INTRODUCTION

This article describes how to use Microsoft Windows Management Instrumentation (WMI) in Microsoft SQL Server 2005 to change either of the following items:
  • The account of the SQL Server service
  • The password of the SQL Server service

MORE INFORMATION

To use SQL Server 2005 to change the account of the SQL Server service or the password of the SQL Server service programmatically, you can use either of the following methods:
  • Use SQL Server Management Objects (SMO).
  • Use Windows Management Instrumentation (WMI). This article describes how to use WMI to perform this task.
Before you use WMI to change the account or the password, consider the following information:
  • After you change only the password, you do not have to restart the SQL Server service. You can avoid any unnecessary downtime when you have to change the password regularly.
  • When you change the account or when you change the account and the password, you must restart the SQL Server service. However, the WMI provider automatically restarts the SQL Server service. If the SQL Server service is not started, the WMI provider starts the service. If the SQL Server service is running, the WMI provider restarts the service. The SQL Server service must be restarted because the following conditions are true:
    • The service master key must be reencrypted.
    • Some other security settings must be set.
  • Microsoft does not support using the following methods to change the account of the SQL Server service or the password of the SQL Server service:
    • The sc command
    • Using the Services Microsoft Management Console (MMC) snap-in
    You cannot use these methods because the WMI provider also performs additional operations when it changes the service account. These operations are not performed if you use these unsupported methods.
To change the account of the SQL Server service or the password of the SQL Server service, use one of the following scripts.

Note Paste one of the Visual Basic scripts into a .vbs file. For example, you name the file SetAccountPassword.vbs. Then, run the .vbs file.
  • Use the following script to change the account and the password of the SQL Server service:
    'Set the account and the password.
    set svr = GetObject("WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement:SqlService.ServiceName='<MSSQL$InstanceName>',SQLServiceType=1")
    
    svr.SetServiceAccount "<Account>", "<Password>"
  • Use the following script to change only the password of the SQL Server service:
    'Set the password.
    set svr = GetObject("WINMGMTS:\\.\root\Microsoft\SqlServer\ComputerManagement:SqlService.ServiceName='<MSSQL$InstanceName>',SQLServiceType=1")
    svr.SetServiceAccountPassword "", "<New_Password>"
Note In these scripts, you must change <MSSQL$InstanceName> to a value that corresponds to your configuration. If you run a default instance of SQL Server 2005, change <MSSQL$InstanceName> to MSSQLSERVER. If you run a named instance of SQL Server 2005, change <MSSQL$InstanceName> to MSSQL$YourInstanceName.

We recommend that you test the script on a test server before you run the script on a production server.

Properties

Article ID: 936492 - Last Review: November 20, 2007 - Revision: 1.3
APPLIES TO
  • Microsoft SQL Server 2005 Developer Edition
  • Microsoft SQL Server 2005 Enterprise Edition
  • Microsoft SQL Server 2005 Standard Edition
  • Microsoft SQL Server 2005 Standard X64 Edition
  • Microsoft SQL Server 2005 Standard Edition for Itanium-based Systems
  • Microsoft SQL Server 2005 Enterprise X64 Edition
  • Microsoft SQL Server 2005 Enterprise Edition for Itanium-based Systems
  • Microsoft SQL Server 2005 Workgroup Edition
  • Microsoft SQL Server 2005 Express Edition
  • Microsoft SQL Server 2005 Express Edition with Advanced Services
Keywords: 
kbexpertiseadvanced kbsql2005engine kbhowto kbinfo KB936492

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com