This article will provide an overview and introduction to DBAtools, a powerful open source library of automation scripts.

本文将概述和介绍DBAtools,DBAtools是一个强大的自动化脚本开源库。

It is an essential task for DBA to automate routine activities. DBAs should always be exploring opportunities for automation in their environment. It gives you enough room to focus on other critical tasks. We can use the combination of t-SQL, SSIS and SSRS to fetch required data and present in various format automatically. We can use PowerShell scripts to automate things without much complexity. SQL Server provides two PowerShell modules SQLPS and SQLServer. We can use SQLServer PowerShell module to use new cmdlets supporting various SQL Server features and latest features.

DBA自动化例行活动是一项基本任务。 DBA应该始终在其环境中探索自动化机会。 它为您提供了足够的空间来专注于其他关键任务。 我们可以结合使用t-SQL,SSIS和SSRS来获取所需的数据并自动以各种格式显示。 我们可以使用PowerShell脚本来自动化事情,而无需太多复杂性。 SQL Server提供了两个PowerShell模块SQLPS和SQLServer。 我们可以使用SQLServer PowerShell模块来使用支持各种SQL Server功能和最新功能的新cmdlet。

In this article, we will discuss automation of DBA tasks with the open-source and community driven PowerShell module DBATools.

在本文中,我们将讨论使用开源和社区驱动的PowerShell模块DBATools自动化DBA任务。

PowerShell integrates deeply and seamlessly with SQL Server and OS and helps us perform various tasks programmatically with a single language. DBATools is a useful repository for the PowerShell commands for SQL Server tasks, which contains more than 500 commands to do tasks such as backup/restore, monitoring, administration tasks with these simple to use commands.

PowerShell与SQL Server和OS进行了无缝无缝集成,可帮助我们使用一种语言以编程方式执行各种任务。 DBATools是用于SQL Server任务的PowerShell命令的有用存储库,其中包含500多个命令,可使用这些简单易用的命令来执行诸如备份/还原,监视,管理任务之类的任务。

DBATools supports almost all features, editions in SQL Servers such as followings.

DBATools支持SQL Server中的几乎所有功能,例如以下版本。

  • SQL Server versions SQL Server 2000 to SQL Server 2017 SQL Server版本SQL Server 2000到SQL Server 2017
  • Clustered and AG instances 集群实例和AG实例
  • All SQL Server editions 所有SQL Server版本
  • Windows and SQL authentication Windows和SQL身份验证

You can visit the URL for the documentation.

您可以访问文档的URL 。

安装 (Installation)

We can install DBATools using various methods. If you have an active internet connection, open the Windows PowerShell with administrative rights. You will receive the following error message without administrative rights.

我们可以使用多种方法安装DBATools。 如果您有活动的Internet连接,请使用管理权限打开Windows PowerShell。 您将收到以下错误消息,没有管理权限。

Right click on Windows PowerShell and Run as administrator.

右键单击Windows PowerShell,然后以管理员身份运行

Run the following command to install DBATools module. In PowerShell.

运行以下命令以安装DBATools模块。 在PowerShell中。

>Install-Module -Name DBATools

DBATools automatically downloads the dependencies and installs them. We need to give permission by entering Y to import the NuGet provider as shown here.

DBATools自动下载并安装依赖项。 我们需要输入Y来授予许可,以导入NuGet提供程序,如下所示。

We also need to accept the module from an untrusted repository. Enter Y to accept and go ahead with the installation.

我们还需要从不受信任的存储库接受模块。 输入Y以接受 并继续安装。

We get the warning in the following screenshot because DBATools is already installed in my system. In my environment, I have version 0.8.693 however latest version is 0.9.781.

由于我的系统中已经安装了DBATools,因此我们在以下屏幕截图中得到警告。 在我的环境中,我的版本为0.8.693,但最新版本为0.9.781。

Run the following command to upgrade the version.

运行以下命令以升级版本。

>Install-Module -Name DBATools -Force

It downloads the required package and installs it in PowerShell module.

它下载所需的软件包并将其安装在PowerShell模块中。

Once the DBATools PowerShell Module is installed, run the following command to enable remote execution of PowerShell scripts.

安装DBATools PowerShell模块后,运行以下命令以启用PowerShell脚本的远程执行。

> set-executionpolicy remotesigned

Azure Data Studio中的PowerShell模块 (PowerShell Module in Azure Data Studio )

Azure Data Studio is a cross-platform integrated tool for SQL Server. It also has an in-built terminal for PowerShell, SSH. We can use this to run the SQL queries as well as PowerShell Command. We will use Azure Data Studio to run the commands using DBATools PowerShell Module.

Azure Data Studio是用于SQL Server的跨平台集成工具。 它还具有用于PowerShell,SSH的内置终端。 我们可以使用它来运行SQL查询以及PowerShell命令。 我们将使用Azure Data Studio通过DBATools PowerShell模块运行命令。

Launch Azure Data Studio. Go to View -> Command Palette and Terminal: Create new Integrated Terminal.

启动Azure Data Studio。 转到查看 -> 命令面板终端:创建新的集成终端

In the PowerShell terminal, run the following command to get a list of commands containing word Login.

在PowerShell终端中,运行以下命令以获取包含单词Login的命令列表。

>Get-Command -Name *login*

Similarly, in the following image, you can see the filtered results for keyword *Copy-SQLServe*

同样,在下图中,您可以查看关键字* Copy-SQLServe *的过滤结果。

In this next section, we will look on a few commands in DBATools PowerShell module.

在下一部分中,我们将研究DBATools PowerShell模块中的一些命令。

Test-DbaDatabaseOwner (Test-DbaDatabaseOwner)

:我们可以检查数据库所有者的数据库。

We can get information about this command using the following query.

我们可以使用以下查询获取有关此命令的信息。

> get-help Test-DbaDatabaseOwner

It gives information about this command along with brief information, description, and related links.

它提供有关此命令的信息以及简要信息,描述和相关链接。

Let us run the command on SQL named instance SQL2019CTP.

让我们在名为实例SQL2019CTPSQL上运行命令。

> Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP

It gives information about each database in the instance. We did not specify any login name; therefore, it checks for SA by default.

它提供有关实例中每个数据库的信息。 我们没有指定任何登录名; 因此,默认情况下会检查SA。

>> Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP

In the output, we get the row for each property in the database. If we have many databases, it is difficult to read details. We can format the result in a tabular format using the following query. It represents the output in an excellent tabular representation.

在输出中,我们获得数据库中每个属性的行。 如果我们有许多数据库,则很难阅读详细信息。 我们可以使用以下查询将结果格式化为表格格式。 它以出色的表格形式表示输出。

In this output, we can see that OwnerMatch is False for highlighted databases as their owner is not SA. You can check this in column TargetOwner.

在此输出中,我们可以看到突出显示的数据库的OwnerMatchFalse ,因为它们的所有者不是SA。 您可以在TargetOwner列中进行检查。

>> Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP | Format-Table

Let us change database owner for SQLShackDemo_ADR to Rajendra using following command.

让我们使用以下命令将SQLShackDemo_ADR的数据库所有者更改Rajendra

sp_changedbowner 'rajendra'

Let us rerun the command and check for the login ‘rajendra’. In the output, we can see OwnerMatch is True for database SQLShackDemo_ADR.

让我们重新运行命令并检查登录名“ rajendra”。 在输出中,我们可以看到OwnerMatch对于数据库SQLShackDemo_ADRTrue

>Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP -TargetLogin 'rajendra' | Format-Table

We can use -SqlCredential parameter to login with alternate credentials in SQL Server instance. We can use both Windows and SQL authentication.

我们可以使用-SqlCredential参数在SQL Server实例中使用备用凭据登录。 我们可以同时使用Windows和SQL身份验证。

Suppose we want to login to localhost\SQL2019CTP using SQL authentication user rajendra and want to check database where targetlogin does not match ‘kashish\Test’.

假设我们想登录到本地主机\ SQL2019CTP使用SQL身份验证的用户拉金德拉并希望检查数据库,在那里targetlogin不匹配“kashish \测试”。

Run the below command in terminal.

在终端中运行以下命令。

>Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP -SqlCredential rajendra -TargetLogin 'kashish\Test' | Format-Table

It opens a pop-up window to provide a password for the user specified in -SqlCredential.

它会打开一个弹出窗口,为-SqlCredential中指定的用户提供密码。

We login to the instance using rajendra and then checks for database owner that does not match targetowner.

我们使用rajendra登录到实例,然后检查与targetowner不匹配的数据库所有者

We can further customize the output in a grid view using parameter Out-GridView. In a Grid view, we get an interactive result window. In this interactive result window, we can filter out the results as well.

我们可以使用参数Out-GridView在网格视图中进一步自定义输出。 在网格视图中,我们得到一个交互式结果窗口。 在此交互式结果窗口中,我们也可以过滤出结果。

Run the following code to check databases with target login ‘Kashish\Test’ in a grid output window.

运行以下代码以在网格输出窗口中以目标登录名“ Kashish \ Test”检查数据库。

>Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP -SqlCredential rajendra -TargetLogin 'kashish\Test' | Out-GridView

In the Add Criteria, you get a list of all columns present in the output. Suppose we want to filter based on the database name. Select Database from the drop-down and mention the database name for which we want to filter the results. It is an interactive filtering of results. We get the filtered result as soon as we start typing on the filter box.

添加条件中 ,您将获得输出中存在的所有列的列表。 假设我们要基于数据库名称进行过滤。 从下拉列表中选择数据库 ,并提及我们要为其过滤结果的数据库名称。 它是结果的交互式过滤。 一旦开始在过滤器框中键入内容,我们就会得到过滤的结果。

In the following image, you can see the filtered results for the database name contains SQLShackDemo.

在下图中,您可以看到包含SQLShackDemo数据库名称的过滤结果

We can filter the result set for a particular database using –Database parameter. In the following code, we filtered out the result for SQLShackDemo database.

我们可以使用–Database参数过滤特定数据库的结果集。 在以下代码中,我们过滤掉了SQLShackDemo数据库的结果。

>Test-DbaDbOwner -SqlInstance localhost\SQL2019CTP  -TargetLogin 'kashish\Test' -Database SQLShackDemo

We get only result for a particular database in this case.

在这种情况下,我们只能得到特定数据库的结果。

Get-DbaSpConfigure (Get-DbaSpConfigure)

DBA need to configure various parameters in SQL Server such as maximum memory, minimum memory, dedicated administration connection, allow remote access etc. In SQL Server, we d do most of the configurations using the sp_configure command. DBATools contains PowerShell command Get-DbaSpConfigure to configure these setting easily and effectively.

DBA需要在SQL Server中配置各种参数,例如最大内存,最小内存,专用管理连接,允许远程访问等。在SQL Server中,我们将使用sp_configure命令进行大多数配置。 DBATools包含PowerShell命令Get-DbaSpConfigure,可轻松有效地配置这些设置。

First, let us get the information about Get-DbaSpConfigure using the get-help command. It lists out the synopsis, description, syntax for this DBATool command.

首先,让我们使用get-help命令获取有关Get-DbaSpConfigure的信息。 它列出了此DBATool命令的简介,描述和语法。

> get-help Get-DbaSpConfigure

The Get-DbaSpConfigure command returns all server level configuration information in SQL Server. In the following query, we specified the SQL instance name to get the all supported configuration using this command. We want the result in tabular format, therefore, specified Format-Table.

Get-DbaSpConfigure命令返回SQL Server中的所有服务器级别的配置信息。 在以下查询中,我们指定了SQL实例名称,以使用此命令获取所有受支持的配置。 我们希望结果采用表格格式,因此,请指定Format-Table。

>Get-DbaSpConfigure -SqlInstance localhost\SQL2019CTP | Format-Table

We can see it return the default value, run value and description for each configuration.

我们可以看到它返回每个配置的默认值,运行值和描述。

It is quite a long list to go through. You can check the few configurations output in the following screenshot as well.

这是一个很长的清单。 您也可以在以下屏幕截图中查看一些配置输出。

We do not need information about all system configurations most of the time. Suppose we want to check whether default backup compression is enabled or not in our SQL instance. We need to pass particular configuration in –Name parameter. Execute following DBATool command to get detailed information about this configuration.

大多数时候,我们不需要有关所有系统配置的信息。 假设我们要检查SQL实例中是否启用了默认备份压缩。 我们需要在–Name参数中传递特定的配置。 执行以下DBATool命令以获取有关此配置的详细信息。

>Get-DbaSpConfigure -SqlInstance localhost\SQL2019CTP -Name 'DefaultBackupCompression'

In the output, you can see we get excellent information such as MinValue, MaxValue, ConfiguredValue, and RunningValue. It also gives a column to show whether in the specified instance, we are running with the default value for this parameter or not.

在输出中,您可以看到我们获得了极好的信息,例如MinValue,MaxValue,ConfiguredValue和RunningValue。 它还提供了一个列来显示在指定实例中我们是否正在使用此参数的默认值运行。

Similarly, following DBATool command list the configuration details for Cost threshold of Parallelism.

类似地,以下DBATool命令列出了并行度成本阈值的配置详细信息

Get-DbaSpConfigure -SqlInstance localhost\SQL2019CTP -Name 'CostThresholdForParallelism'

In the following query, we are checking whether the dedicated administrator connection is enabled on SQL instance or not.

在以下查询中,我们正在检查是否在SQL实例上启用了专用管理员连接。

>Get-DbaSpConfigure -SqlInstance localhost\SQL2019CTP -Name 'RemoteDacConnectionsEnabled'

Once we get the information of any particular parameter, we can use DBATool command Set-DbaSpConfigure to change the configuration value. Suppose we want to enable the dedicated administrator connection in SQL instance. Previously, we checked that this configuration is not enabled for specified SQL instance. We can enable it using the following code by specifying configuration value 1 for ‘RemoteDacConnectionsEnabled.’

一旦获得任何特定参数的信息,就可以使用DBATool命令Set-DbaSpConfigure更改配置值。 假设我们要在SQL实例中启用专用管理员连接。 以前,我们检查了是否为指定SQL实例启用了此配置。 通过为“ RemoteDacConnectionsEnabled”指定配置值1,我们可以使用以下代码启用它。

>Set-DbaSpConfigure -SqlInstance localhost\SQL2019CTP -Name 'RemoteDacConnectionsEnabled' -Value 1

Similarly, in the following code, we are changing the Cost threshold of parallelism value to 100.

同样,在下面的代码中,我们将并行度Cost阈值更改为100。

Set-DbaSpConfigure -SqlInstance localhost\SQL2019CTP -Name 'CostThresholdForParallelism' -Value 100

In the output, we get the Previous and New value for the configuration setting.

在输出中,我们获得配置设置的上一个和下一个值。

Let us verify this change using SSMS. Connect to SQL instance. Open Server properties and go to Advanced.

让我们使用SSMS验证此更改。 连接到SQL实例。 打开服务器属性,然后转到“ 高级”

In the following screenshot, we can see the Cost Threshold for Parallelism value is 100. It is the value we changed using DBATools configuration.

在以下屏幕截图中,我们可以看到“并行度的成本阈值”值为100。这是我们使用DBATools配置更改的值。

结论 (Conclusion)

In this article, we explored the use of PowerShell module DBATools to perform various DBA tasks. It contains many such important commands. I will continue exploring useful command and share in future articles.

在本文中,我们探讨了使用PowerShell模块DBATools执行各种DBA任务。 它包含许多这样的重要命令。 我将继续探索有用的命令并在以后的文章中分享。

目录 (Table of contents)

DBATools PowerShell Module for SQL Server
PowerShell SQL Server Validation Utility – DBAChecks
SQL Database Backups using PowerShell Module – DBATools
IDENTITY columns threshold using PowerShell SQL Server DBATools
DBATools PowerShell SQL Server Database Backups commands
SQL Restore Database using DBATools
Validate backups with SQL restore database operations using DBATools
Fix Orphan users in SQL Server using DBATools PowerShell
Creating a SQL Server Database using DBATools
Get SQL Database details using DBATools
Get-DbaHelpIndex command in DBATools
适用于SQL Server的DBATools PowerShell模块
PowerShell SQL Server验证实用程序– DBAChecks
使用PowerShell模块SQL数据库备份– DBATools
使用PowerShell SQL Server DBATools的IDENTITY列阈值
DBATools PowerShell SQL Server数据库备份命令
使用DBAToolsSQL Restore Database
使用DBATools通过SQL恢复数据库操作验证备份
使用DBATools PowerShell修复SQL Server中的孤立用户
使用DBATools创建SQL Server数据库
使用DBATools获取SQL数据库详细信息
DBATools中的Get-DbaHelpIndex命令

翻译自: https://www.sqlshack.com/dbatools-powershell-module-for-sql-server/

适用于SQL Server的DBATools PowerShell模块相关推荐

  1. python cv2模块安装_python 连接sql server数据库,pymssql模块安装。

    python 连接sql server数据库,pymssql模块安装. python版本:python3.7 数据库版本:sql server 2016 连接sql server数据库,本菜鸟用的是p ...

  2. iotop iostat_适用于SQL Server DBA的有用的Linux命令– iotop和iostat

    iotop iostat In the article, we will learn how to use the 'iotop' and 'iostat' commands with various ...

  3. sql crud_使用适用于SQL Server的Python SQL库执行CRUD操作

    sql crud This article covers how to connect a Python application to Microsoft SQL Server using a 3rd ...

  4. intellisense_SQL Server IntelliSense的使用和故障排除–适用于SQL Server 2012或更高版本

    intellisense 什么是IntelliSense? (What is IntelliSense?) SQL Server Management Studio的IntelliSense是SQL ...

  5. jupyter 写sql_了解适用于SQL Server的Jupyter Notebooks

    jupyter 写sql 介绍 (Introduction) The Jupyter notebook is a powerful and interactive tool that supports ...

  6. 适用于SQL Server生产环境DBA的七大技巧

    摘自:http://database.ctocio.com.cn/452/8976452.shtml 1.使用forfiles命令删除陈旧的数据库备份文件 从Windows Server 2003开始 ...

  7. 使用PowerShell模块SQL数据库备份– DBATools

    This article will be first article of series for SQL database backup and restoration using DBAtools, ...

  8. DBATools PowerShell SQL Server数据库备份命令

    In my earlier PowerShell SQL Server article, SQL Database Backups using PowerShell Module – DBATools ...

  9. 使用DBATools PowerShell修复SQL Server中的孤立用户

    This article gives an overview of Orphan users and fixing them using DBATools PowerShell. 本文概述了Orpha ...

最新文章

  1. 法国科学家发布AI模型,阐释蛋白结构和功能及进化关系
  2. 阿里云域名备案时产品类型
  3. 类成员函数指针的语法
  4. Jetty:配置概览-怎么配置Jetty
  5. 360网络修复工具_Win10网络图标不见了解决方法
  6. 拉屎能赚钱?在马桶上月入过万?原来卫生间里还有这么多隐藏福利,超模君都惊了……
  7. 10835k1_行政组织学_21秋考试
  8. numpy.tile作用,语法,参数分析以及举例
  9. 操作系统 —— 课程概述
  10. SonarLint各种提示的意思
  11. Spring AOP 本质(1)
  12. 敲笨钟(古诗词押 ong 韵)
  13. Elasticsearch个人学习笔记
  14. 《mysql从删库到跑路》下载
  15. SCI、EI、IEEE、检索网站的区别
  16. 记我的 15 款 MacBook Pro 13.3 寸维修(开不了机)+换屏的经历
  17. ccf201809-2买菜
  18. 杨超越杯编程大赛上热搜:不懂技术真不敢追星
  19. vue的生命周期(详细)
  20. <artifactId>spring-boot-maven-plugin</artifactId> 这一行在POM.xml文件中一直爆红,如何解决.问题记录日期:2020-09-05

热门文章

  1. 服务器重装系统要注意什么_企业租用美国服务器,应该注意什么?
  2. redis 3.0 java 工具包_redis分布式锁工具包,提供纯Java方式调用
  3. cacti监控服务器
  4. MikroTik RouterOS电子克隆盘原理收集
  5. PHP基础2--基本语法
  6. 第七章部分例题最大乘积
  7. centos7.0 安装vsftp实录
  8. js 把字符串格式化成时间
  9. hdu 2035 人见人爱A^B (快速幂)
  10. JavaScript学习(十一)—selected属性、checked属性、class属性的操作