Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By : Donabel Santos
Book Image

SQL Server 2014 with PowerShell v5 Cookbook

By: Donabel Santos

Overview of this book

Table of Contents (21 chapters)
SQL Server 2014 with PowerShell v5 Cookbook
Credits
About the Author
Acknowledgments
About the Reviewers
www.PacktPub.com
Preface
Index

Changing SQL Server service account


We will see how to change SQL Server accounts in this recipe.

Getting ready

For this recipe, you will need to create another Windows/Domain account that you can use to change the service account to.

In this recipe, we will change the service account for SQLSERVERAGENT from QUERYWORKS\sqlservice to QUERYWORKS\sqlagent, which are available in the Virtual Machine (VM) created in Appendix B, Creating a SQL Server VM if you've created the same VM. If you are using a different environment, you must substitute these with accounts that already exist in your system.

How to do it...

Let's explore the code required to change a SQL Server service account:

  1. Open PowerShell ISE as an administrator.

  2. Import the SQLPS module and create a new Wmi.ManagedComputer object as follows:

    #import SQL Server module
    Import-Module SQLPS -DisableNameChecking
    
    #default SQL Server instance
    $instanceName = "localhost"
    
    $managedComputer = New-Object -TypeName Microsoft.SqlServer.Management.Smo...