Show
Answers
Skip to main content This browser is no longer supported. Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support. Install SQL Server with PowerShell Desired State Configuration
In this articleHave you ever gone through the SQL Server installation interface just by selecting the same buttons, entering the same information, and not giving it a second thought? The installation finished, but you forgot to specify the DBA group in the sysadmin role. Then you had to do these things:
What's worse is now the confidence of the entire installation is shaken. "What else did I forget?" you might ask yourself. Read about PowerShell Desired State Configuration (DSC). By using DSC, you build one configuration template that you can reuse over hundreds and thousands of servers. Depending on the build, you might have to tweak a few of the setup parameters. But that's not a significant issue because you can keep all of the standard settings in place. It eliminates the possibility that you'll forget to enter an important parameter. This article explores the initial setup of a standalone instance of SQL Server 2017 on Windows Server 2016 by using the SqlServerDsc DSC resource. Some prior knowledge of DSC is helpful as we won't explore how DSC works. Note This article refers to PowerShell 5.1, which is the default version of PowerShell installed with Windows Server 2016, 2019, and 2022. The following items are required for this walkthrough:
PrerequisitesIn most cases, DSC is used to handle the prerequisite requirements. But for the purposes of this demo, we handle the prerequisites manually. Install the SqlServerDsc DSC resourceDownload the SqlServerDsc DSC resource from the PowerShell Gallery by using the Install-Module cmdlet. Note Make sure PowerShell is running As Administrator to install the module.
Get the SQL Server 2017 installation mediaDownload the SQL Server 2017 installation media to the server. We downloaded SQL Server 2017 Enterprise
from a Visual Studio subscription and copied the ISO to Now the ISO must be extracted to a directory:
Create the configurationConfigurationCreate the configuration function that will be called to generate the Managed Object Format (MOF) documents:
ModulesImport the modules into the current session. These modules tell the configuration document how to build the MOF documents. They also tell the DSC engine how to apply the MOF documents to the server:
Resources.NET FrameworkSQL Server relies on the .NET Framework. So we need to make sure it's installed before we install SQL Server. The WindowsFeature resource is used to install the Net-Framework-45-Core Windows feature:
SqlSetupThe SqlSetup resource is used to tell DSC how to install SQL Server. The parameters required for a basic installation are as follows:
Note We don't recommend this configuration in a high-security environment. A full list and description of the parameters available on SqlSetup are available on the SqlServerDsc GitHub repository. The SqlSetup resource installs only SQL Server and doesn't maintain the settings that are applied. An example is if the SQLSysAdminAccounts are specified at installation time. An admin might add or remove sign-ins to or from the sysadmin role. But the SqlSetup resource won't be affected. If you want DSC to enforce the membership of the sysadmin role, use the SqlServerRole resource. Finish configuration
Build and deployCompile the configurationDot source the configuration script:
Run the configuration function:
A directory called SQLInstall is created in the working directory. It contains a file called localhost.mof. Examine the contents of the MOF, which shows the compiled DSC configuration. Deploy the configurationTo start the DSC deployment of SQL Server, call the Start-DscConfiguration cmdlet. The following parameters are provided to the cmdlet:
As the configuration applies, the verbose output shows you what's happening. As long as no errors (red text) are thrown, when Operation 'Invoke CimMethod' complete appears on the screen, SQL Server should be installed. Validate installationDSCThe Test-DscConfiguration cmdlets can determine if the current state of the server meets the desired state. In this case, it's the SQL Server installation. The result of Test-DscConfiguration should be True:
ServicesThe services list now returns SQL Server services:
SQL Server
See alsoWindows PowerShell Desired State Configuration Overview Install SQL Server from the command prompt Install SQL Server by using a configuration file FeedbackSubmit and view feedback for How do I install SQL Server 2016 Service Pack?In Windows Explorer navigate to the location you have saved the SQL Server 2016 SP1 executable. Double Click on the executable to start applying SQL Server 2016 SP1. Accept the Terms and Conditions to proceed and click Next. Select the Instances that you wish to apply the Service Pack to and click Next.
How do I download a SQL Server module from PowerShell?To install the SqlServer module from the PowerShell Gallery, start a PowerShell session and run Install-Module SQLServer . If running on Windows PowerShell you can use Install-Module SQLServer -Scope CurrentUser to install the module for just the current user and avoid needing elevated permissions.
How do I install a SQL Server service?Create a SQL database for your website.. Start the Microsoft SQL Server Management Studio app.. In the Object Explorer panel, right-click on Databases, and choose New Database..... Enter the name of the new database. Click/Tap OK. Note: Remember the database name, because it will be required by the DNN Installation Wizard.. How do I automate a SQL patch in PowerShell?#Update the Build Reference so DBATOOLS is aware of the latest SP/CU versions.. #Set the versions to whatever version you're using.. #create a list of servers that you want to patch.. #create a credential to pass in to the Update-DbaInstance command; this will prompt for your password.. |