How to deploy SQL Server via PowerShell DSC

Today, TipsMake will guide you on how to deploy SQL Server through PowerShell DSC, enabling you to use the new PowerShell feature to manage your infrastructure on-premises and in the cloud efficiently and easily.

DSC is integrated into PowerShell 4.0 and is part of the Windows Management Framework . PowerShell DSC is included in Windows 2012 R2, but is available for Windows 2008 R2 and Windows 2012 users to download and install.

DSC is a declaration. A DSC script doesn't contain logic about how to perform specific installations or uninstallations. Instead, it defines specific server or application settings.

Imagine you've just bought a new apartment, complete with walls, doors, and windows. You don't need to worry about the walls or window placement, as that's the builder's job. All you need to do is describe the apartment you want, and the builder will construct it for you based on that description.

DSC works in a similar way. The specifications are contained in the configuration, and the DSC runtime acts as a builder, ensuring that the resources on the server are set up according to the user's wishes.

DSC is a relatively new tool. Other configuration management tools like Chef and Puppet have been on the market for many years, and these tools manage both Windows and non-Windows systems. However, using these tools requires users to learn another language. DSC scripts utilize newer extensions for PowerShell.

Advantages of DSC

- Standardization : Create scripts containing definitions of different service types within your architecture (such as IIS, database, file server) and then use them for all new deployments. You can be sure that all servers will be set up identically.

- Accelerated Deployment : Quickly and easily apply DSC configuration to servers via the PowerShell runtime.

- Configuration Detection : DSC provides a solution to identify the active server configuration specified in scripts and can report or automatically correct discrepancies.

- Simple: DSC is developed using PowerShell, so users can fine-tune DSC through PowerShell. However, the scripts lack logic and error handling capabilities, but they are easy to read.

- Idempotency: You can apply the same DSC configuration without any problems. And if the configuration is updated, only the different settings change.

Components of DSC

- WinRM (Windows Remote Management): Microsoft's implementations of the standard WS-Management protocol for managing servers using SOAP.

- CIM (Common Information Model): a standard for describing the structure and behavior of managed resources (such as storage, networks, and software components). WMI is implemented from CIM on Windows.

- MOF (Managed Object Format) files: Contain the configuration applied to the target node.

- Resources: These are the building blocks for configuring DSC. DSC includes several built-in resources, such as File and Windows Features, or users can create their own resources.

Function of DSC resources

Each DSC resource includes 3 functions.

- Test-TargetResource: This is the first function called when the DSC configuration is applied. It returns True/False based on whether the source is correct. If true, the DSC runtime does not need to perform any further operations.

- Set-TargetResource: Called when Test-TargetResource returns False. Responsible for ensuring the resource is set according to the specifications stored in the configuration.

- Get-TargetResource: Returns all properties of the resource. Not used in the configuration step but used for reporting.

DSC configuration

The configuration below specifies that a directory named DSC_Demo exists in C:temp .

When the PowerShell script above is executed, a MOF file named localhost.mof is created by the DSC runtime in the CreateFolderDemo directory. If you open the file, you will see a file that looks like this:

To apply the configuration -and and " make it so ", we use the command Start-DscConfiguration:

Start-DscConfiguration -Path .CreateFolderDemo -Wait -Verbose

The directory does not exist before the script is run, so the Test-TargetResource function will return False . Next, the Set-TargetResource function will be called and the directory will be created.

To display idempotency, if the script runs a second time, Test-TargetResource will return True and no further configuration will take place.

Deploying SQL Server via PowerShell DSC

If you use the xSqlPs PowerShell module , you can implement SQL Server through PowerShell DSC.

First, download the module and extract it into the $env:ProgramFilesWindowsPowerShell folder.

Download and extract the xSqlPs PowerShell module from here: Download xSqlPs PowerShell (insert link)

Open PowerShell and verify the modules are present by running the command Get-DSCResource:

Next, create the configuration. In this example, we're installing a SQL 2014 instance named DSCInstance on the local computer:

When you run the script, it will create a localhost.mof file in the InstallSQLDemo directory .

Configure using commands:

Start-DscConfiguration -Path .InstallSqlDemo -Wait -Verbose

The command above will run the SQL Server installation, creating a new DSCInstance database.

Similar to the FileDemo above, if you try running Start-DscConfiguration a second time, it will attempt to complete without any errors:

Note : xSqlServerResource is designed for SQL 2012 and other versions, and will report an installation failure error even after the user has completed the installation. This is because the resource searches for the SQL Setup log file in directory 110, but SQL Server versions store the log file in a different location.

The quickest way to fix this is to edit " C:Program FilesWindowsPowerShellModulesxSqlPsDSCResourcesMSFT_xSqlServerInstallMSFT_xSqlServerInstall.psm1" and change the directory on line 154 to one of the lines related to your SQL version (100 for Server 2008 and 120 for 2014).

DSC is a great tool for easier server management and deployment in the future. However, at this time, DSC is still very new and only supports basic operations. The SQL Server module does not display many of the command-line utilities available in SQL Setup.

In summary, this article from TipsMake has guided you on how to deploy SQL Server using PowerShell DSC. Additionally, you can use DSC to deploy Service Packs or Cumulative Updates to a SQL Server instance. Then, if a new Service Pack is released, the DBA will update some configuration files and redeploy, and the SQL Server resource will detect that only one patch needs to be installed, not the entire deployment.

Before making any significant changes to SQL Server, to avoid risks, you should refer to the SQL Server backup and restore guide to understand how to recover SQL Server data.


Taimienphi.vn has also provided detailed instructions on how to install SQL Server 2019 in the article "How to install SQL Server 2019" . If you need to install it, please refer to that guide for easier operation. 

Other SQL articles
  • How to create, validate, and modify the Identity column in Microsoft SQL Server

    microsoft sql server được tích hợp sẵn cột identity, được sử dụng để tạo ra các giá trị quan trọng. nó có thể tham chiếu nhưng một trường autonumber trong microsoft access hoặc một dãy trong oracle. bài viết dưới đây tipsmake sẽ hướng dẫn bạn cách tạo, kiểm tra và thay đổi cột indentity trên microsoft sql server.
  • How to log in to SQL Server if the SA account is disabled.

    trong trường hợp nếu tài khoản sa bị vô hiệu hóa và bạn không thể đăng nhập sql server. bài viết này tipsmake sẽ hướng dẫn bạn cách đăng nhập sql server nếu tài khoản sa bị vô hiệu hóa.
  • Instructions on how to install Laravel, supporting web programming.

    với những ai đang học lập trình không thể nào không biết đến được laravel, vậy làm thế nào để cài đặt laravel trên máy tính của bạn, chạy trơn chu các ứng dụng web cũng như giúp bạn học tập và thực hành tốt.
  • How to install SQL Server 2019 on Windows

    phiên bản microsoft sql server 2019 mới nhất mang đến rất nhiều tính năng mới hấp dẫn đáp ứng được người dùng hiện nay, nhất là giúp tổ chức đánh giá, kiểm tra dữ liệu và tích hợp nhiều phần mềm khác nhau. Để cài đặt microsoft sql server 2019, các bạn làm theo hướng dẫn trong bài viết sau đây.
  • The AND, OR, and NOT operators in SQL

    toán tử and và or được sử dụng để kết hợp nhiều điều kiện nhằm thu hẹp dữ liệu trong câu lệnh sql. hai toán tử này được gọi là toán tử liên hợp trong sql. and và or cho phép tạo nhiều so sánh với các toán tử khác trong cùng một lệnh sql.
  • DROP DATABASE command in SQL

    drop database trong sql được dùng như thế nào? bài viết sẽ hướng dẫn bạn chi tiết cách xóa database trong sql.
Category

System

Windows XP

Windows Server 2012

Windows 8

Windows 7

Windows 10

Wifi tips

Virus Removal - Spyware

Speed ​​up the computer

Server

Security solution

Mail Server

LAN - WAN

Ghost - Install Win

Fix computer error

Configure Router Switch

Computer wallpaper

Computer security

Mac OS X

Mac OS System software

Mac OS Security

Mac OS Office application

Mac OS Email Management

Mac OS Data - File

Mac hardware

Hardware

USB - Flash Drive

Speaker headset

Printer

PC hardware

Network equipment

Laptop hardware

Computer components

Advice Computer

Game

PC game

Online game

Mobile Game

Pokemon GO

information

Technology story

Technology comments

Quiz technology

New technology

British talent technology

Attack the network

Artificial intelligence

Technology

Smart watches

Raspberry Pi

Linux

Camera

Basic knowledge

Banking services

SEO tips

Science

Strange story

Space Science

Scientific invention

Science Story

Science photo

Science and technology

Medicine

Health Care

Fun science

Environment

Discover science

Discover nature

Archeology

Life

Travel Experience

Tips

Raise up child

Make up

Life skills

Home Care

Entertainment

DIY Handmade

Cuisine

Christmas

Application

Web Email

Website - Blog

Web browser

Support Download - Upload

Software conversion

Social Network

Simulator software

Online payment

Office information

Music Software

Map and Positioning

Installation - Uninstall

Graphic design

Free - Discount

Email reader

Edit video

Edit photo

Compress and Decompress

Chat, Text, Call

Archive - Share

Electric

Water heater

Washing machine

Television

Machine tool

Fridge

Fans

Air conditioning

Program

Unix and Linux

SQL Server

SQL

Python

Programming C

PHP

NodeJS

MongoDB

jQuery

JavaScript

HTTP

HTML

Git

Database

Data structure and algorithm

CSS and CSS3

C ++

C #

AngularJS

Mobile

Wallpapers and Ringtones

Tricks application

Take and process photos

Storage - Sync

Security and Virus Removal

Personalized

Online Social Network

Map

Manage and edit Video

Data

Chat - Call - Text

Browser and Add-on

Basic setup