适用于SQL Server的DBATools PowerShell模块

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

DBATools documentation

安装 (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。 您将收到以下错误消息,没有管理权限。

DBATools installation

Right click on Windows PowerShell and Run as administrator.

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

Windows PowerShell with administrative rights

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提供程序,如下所示。

DBATools installation

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

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

DBATools installation

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。

DBATools installation and upgrade

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模块中。

DBATools installation and upgrade

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

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

> set-executionpolicy remotesigned

enable remote execution of PowerShell scripts

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。 转到查看 -> 命令面板终端:创建新的集成终端

PowerShell Module in 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*

Get-help command in DBATools

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

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

Showing filtered results for keyword *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

Get-help command ifor Test-DbaDatabaseOwner DBATools

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

Use cases of command Test-DbaDatabaseOwner in DBATools

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

Use cases of command Test-DbaDatabaseOwner in DBATools

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

Checking the database owner name in the output

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中指定的用户提供密码。

Use cases of command Test-DbaDatabaseOwner in DBATools

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

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

Use cases of command Test-DbaDatabaseOwner in DBATools

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

Use cases of command Test-DbaDatabaseOwner in DBATools

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数据库名称的过滤结果

Use cases of command Test-DbaDatabaseOwner in DBATools - Grid view

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.

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

Use cases of command Test-DbaDatabaseOwner in DBATools

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

Use cases of command Get-DbaSpConfigure in DBATools

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.

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

Use cases of command Get-DbaSpConfigure in DBATools

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

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

Use cases of command Get-DbaSpConfigure in DBATools

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。 它还提供了一个列来显示在指定实例中我们是否正在使用此参数的默认值运行。

Use cases of command Get-DbaSpConfigure in DBATools

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

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

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

Use cases of command Get-DbaSpConfigure in DBATools

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'

Use cases of command Get-DbaSpConfigure in DBATools

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

Use cases of command Get-DbaSpConfigure in DBATools

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.

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

Use cases of command Get-DbaSpConfigure in DBATools

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配置更改的值。

Verify configuration change from DBATools with SSMS

结论 (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/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PL/SQL Developer是Oracle数据库当前最流行的开发工具之一,它在ORACLE数据库开发设计方面功能强大,使用方便,但是数据库管理方面一直比较欠缺。 DBATools For PL/SQL Developer 是一款PL/SQL Developer的辅助插件,主要功能是提供许多DBA数据库管理的功能,使ORACLE工程师不仅可以使用PL/SQL Developer进行开发设计,同时也可以进行数据库管理及监控,大大提高工作效率。 功能列表: 1.表空间管理 2.初始化参数管理 3.重做日志管理 4.数据库监视 4.1.查看SGA统计信息 4.2.查看排序情况 4.3.查看日志切换情况 4.4.查看锁资源 4.11.查看库缓存命中率 4.6.查看数据缓存命中率 4.7.查看WorkArea情况 4.8.查看当前会话等待事件 4.9.查看数据库大小 4.10.查看等待事件统计信息 5.清空缓冲区 6.导出数据库表结构文档 7.快速打开TNSNAME.ORA文件 8.快速打开listener.ORA文件 9.快速打开Oracle Net Manager 10.自定义配置 10.1.语言配置,支持中英两种语言 10.2.菜单自定义配置 10.3.数据库监视SQL自定义配置 11.快捷菜单 11.1.表 11.1.1.分析表 11.1.2.取表记录数 11.1.3.创建物化视图日志 11.1.4.设置并行度 11.2.视图 11.2.1.取表记录数 11.3.物化视图 11.3.1.分析 11.3.2.取表记录数 11.3.3.刷新 11.3.4.设置并行度 11.4.数据库链接 11.4.1.测试 11.11.列 11.11.1.分析 11.6.索引 11.6.1.分析 11.6.2.重建 11.7.表空间 11.7.1.管理 11.7.2.新增 11.7.3.编辑 12.导出菜单 12.1.导出属性方式的XML文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值