Sunteți pe pagina 1din 30

Module 11

Introduction to Managing SQL


Server Using PowerShell
Module Overview

• Getting Started with Windows PowerShell


• Configure SQL Server Using PowerShell
• Administer and Maintain SQL Server with
PowerShell
• Managing Azure SQL Databases Using PowerShell
Lesson 1: Getting Started with Windows
PowerShell

• What Is Windows PowerShell?


• PowerShell Help
• Getting Started with PowerShell
• SQL Server Management Objects
• PowerShell Providers
• Demonstration: Exploring SQL Server
Management Objects
What Is Windows PowerShell?

• Used to automate Windows tasks and


configuration settings, including SQL Server
• Uses commands, or cmdlets, in the format
verb+prefixnoun (noun is always singular)
• Is case insensitive
• Includes aliases, or shortcuts, such as cd, cls, and
dir
• Accessed either from the taskbar or Object
Explorer
PowerShell Help

• Get-Help is a cmdlet that displays help


• Get-Help <cmdlet>, such as Get-Help Get-Item

• Get-Help displays help about help


• Get-Alias
• Wildcards
• Get-Help *Azure*

• Tab completion
• Start a cmdlet and then press TAB repeatedly

• Get-verb
• A cmdlet that displays all PowerShell verbs
Getting Started with PowerShell

• PowerShell version 5.0 is current


• PowerShell cmdlets are made available through
modules and snap-ins
• SQL Server cmdlets have the prefix sqlps
• Run cmdlets individually or in a script
• Use the Windows PowerShell Console to run
cmdlets
• Use the Windows PowerShell ISE to develop and
debug scripts
• Denote variables with $—can be strongly or
loosely typed
SQL Server Management Objects

• Expose SQL Server objects to .NET programming


languages
• Classes are grouped into namespaces
• SMO namespace includes the Database class
• Other namespaces include SMO.Mail and SMO.Agent

• Use Database class to retrieve and change


settings
• Can be used to create a new database
PowerShell Providers

• Provide a way of navigating to a location and


getting access to objects and data
• Use Get-psProvider to list all the PowerShell
providers
• Use Get-PSDrive to see the PowerShell drives
• Use Set-Location or cd to change to the
PowerShell drives
Demonstration: Exploring SQL Server
Management Objects

In this demonstration, you will see how to:


• Use PowerShell to explore the help system
• Use a PowerShell provider to explore SMOs
Lesson 2: Configure SQL Server Using PowerShell

• Reading Database Settings


• Amending SQL Server Database Settings
• SQL Server Settings
• PowerShell ISE
Reading Database Settings

• To display database properties:


1. Import the SQLPS module
2. Check the SQL Server provider is available using:
1. Get-PSProvider
2. Get-PSDrive
3. Navigate to the position in the SMO hierarchy you
require
4. Declare variables to retrieve the SMO object
5. Use Write-Host to display database properties on the
screen
Amending SQL Server Database Settings

• You can amend a database setting by using


PowerShell and SMO
• Use the Alter method to make the change
• PowerShell works with objects
• Objects have properties, methods, and events
• Get-Member enables you to find information
about objects
• $database | Get-Member
SQL Server Settings

• To amend SQL Server instance settings


• Use the SQL PowerShell module
• Use SQL provider
• Access SMO objects
PowerShell ISE

• PowerShell ISE is used to develop PowerShell


scripts
• The console window is used just like the stand-
alone console
• The scripting pane includes IntelliSense and
colored script
• Use Options to customize the ISE
• Note Run and Run Selection on the toolbar
Lesson 3: Administer and Maintain SQL Server
with PowerShell

• Managing Users and Roles


• Display Information About SQL Server Instances
• Managing Backups and Restores
• Using PowerShell to Report Windows Update
• Demonstration: PowerShell for Troubleshooting
Managing Users and Roles

• To maintain users and roles, use the SQL provider


to access SMO objects
• List database users with the Database.Users
property
• Within a database
• Across database instances

• Add a database role with Smo.DatabaseRole


object and the create method
• Create permissions with the
Smo.ObjectPermissionSet
• Add a user with the Smo.User object and the
Create method
Display Information About SQL Server Instances

• PowerShell is a cross-product tool


• Windows operating systems
• Other Microsoft products
• SQL Server

• Ability to report across SQL Server instances


• A PowerShell pipe (|) channels data from one
cmdlet to another
• Get-ChildItem is used with the SQL Server
provider to access the SMO
Managing Backups and Restores

• Backup-SqlDatabase is the cmdlet for backing up


databases
• Get-help Backup-SqlDatabase for the full syntax
• Flexible

• Use Restore-SqlDatabase to restore databases


Using PowerShell to Report Windows Update

• PowerShell is flexible in that is crosses


boundaries of the operating system, other
Microsoft products, and SQL Server
• Windows Updates are often one of the first areas
to be checked when problems occur
• A PowerShell script can report on Windows
Updates, and filter by keywords
• Get-HotFix cmdlet displays information about
hotfixes
• Windows Update Agent API is based on COM;
not the .NET Framework
Demonstration: PowerShell for Troubleshooting

In this demonstration, you will see how to:


• Retrieve information about SQL Server instances
• Output information in different formats
Lesson 4: Managing Azure SQL Databases Using
PowerShell

• Connect to Your Subscription


• Create a Server
• Create an Azure SQL Database
• Azure SQL Database cmdlets
• Demonstration: Creating an Azure SQL Database
with PowerShell
Connect to Your Subscription

• Azure PowerShell contains a number of cmdlets


specific to working with Azure
• Add your Azure account using:
• Add-AzureRMAccount
• Select an Azure subscription using:
• Select-AzureRmSubscription –SubscriptionID <your
subscriptionID>
Create a Server

• List available data centers using:


• Get-AzureRmResourceProvide and filter to show only
Microsoft.Sql locations
• Create a resource group
• At the same location you want your Azure SQL
Database
• New-AzureRmResourceGroup

• Create a new server


• New-AzureRmSqlServer

• Create firewall rules


• New-AzureRmSqlServerFirewallRule
• Specify StartIPAddress and EndIPAddress
Create an Azure SQL Database

To create a new Azure SQL Database


• Use New-AzureRmSqlDatabase cmdlet
Azure SQL Database cmdlets

• A number of Azure PowerShell cmdlets to


automate tasks associated with Azure SQL
Database
• See MSDN for a list of cmdlets
Demonstration: Creating an Azure SQL Database
with PowerShell

In this demonstration, you will see how to use


Azure PowerShell to create an Azure SQL
Database
Lab: Using PowerShell to Manage SQL Server

• Exercise 1: Getting Started with PowerShell


• Exercise 2: Using PowerShell to Change SQL Server
Settings

Logon Information
Virtual machine: 20764C-MIA-SQL
User name: AdventureWorks\Student
Password: Pa55w.rd

Estimated Time: 30 minutes


Lab Scenario

You are a DBA working at the quickly expanding


Adventure Works Bicycle company. The IT director
has just agreed to add another server and is
getting concerned about how long it will take to
administer all the servers. The IT director has
asked you to evaluate PowerShell as a way of
automating some of the tasks, and reducing the
number of problems with SQL Server.
Lab Review

• What is an SMO object?


• Can you name three ways of getting information
about a cmdlet?
• When would you use a PowerShell provider?
Module Review and Takeaways

• Review Question(s)
• Best Practice

S-ar putea să vă placă și