如何使用PowerShell处理SQL Azure数据库

介绍 ( Introduction )

PowerShell is a shell used specially to automate administrative tasks.

PowerShell是专门用于自动执行管理任务的外壳。

It is an incredible tool used to programmatically automate tasks like SQL Server tasks, Exchange tasks, Windows tasks, etc. It is very useful to integrate different Microsoft and sometimes non-Microsoft programs.

这是一个令人难以置信的工具,可用于以编程方式自动执行SQL Server任务,Exchange任务,Windows任务等任务。集成不同的Microsoft程序(有时是非Microsoft程序)非常有用。

In this new tutorial, we will show how to install PowerShell for Azure and then how to use it. We will create some databases, edit database properties and retrieve database information using PowerShell.

在此新教程中,我们将展示如何安装PowerShell for Azure,然后如何使用它。 我们将使用PowerShell创建一些数据库,编辑数据库属性并检索数据库信息。

如何使用PowerShell处理SQL Azure数据库

Figure 1. PowerShell and Azure


图1. PowerShell和Azure

要求 ( Requirements )

  • An Azure subscription.

    Azure订阅。
  • A local machine with Windows installed.

    安装Windows的本地计算机。

入门 ( Getting started )

  1. In your local machine, we will first install Microsoft Azure PowerShell. You can download the installer here.

    如何使用PowerShell处理SQL Azure数据库

    Figure 2. Downloading the installer.

    在您的本地计算机上,我们将首先安装Microsoft Azure PowerShell。 您可以在此处下载安装程序


    2 下载安装程序。

  2. Once you have the installer, a configuration Windows will be displayed.

    如何使用PowerShell处理SQL Azure数据库

    Figure 3. Time remaining to configure the Microsoft Web Installer.

    安装完成后,将显示一个配置窗口。


    3 剩余时间来配置Microsoft Web Installer。

  3. The Web Platform Installer will initiate the Microsoft Azure PowerShell wizard.

    如何使用PowerShell处理SQL Azure数据库

    Figure 4. The PowerShell installer for Azure SDK.

    Web平台安装程序将启动Microsoft Azure PowerShell向导。


    图4 适用于Azure SDK的PowerShell安装程序。

  4. As any installer, it requires you to accept the terms and conditions.

    如何使用PowerShell处理SQL Azure数据库

    Figure 5. Microsoft Agreement.

    作为任何安装程序,它都要求您接受条款和条件。


    5 Microsoft协议。
  5. Once it is installed, it will ask to restart the machine. You can restart immediately or later.

    如何使用PowerShell处理SQL Azure数据库

    Figure 6. The restart message.

    一旦安装,它将要求重新启动计算机。 您可以立即或稍后重新启动。


    图6.重新启动消息。

  6. On the Windows Start Menu, open the Microsoft Azure PowerShell. This is the command line used to automate some tasks.

    如何使用PowerShell处理SQL Azure数据库

    Figure 7. The Microsoft Azure PowerShell

    在Windows“开始”菜单上,打开Microsoft Azure PowerShell。 这是用于自动执行某些任务的命令行。


    图7. Microsoft Azure PowerShell

  7. The Microsoft Azure PowerShell will be similar to this screenshot.

    如何使用PowerShell处理SQL Azure数据库

    Figure 8. The PowerShell Command line

    The PowerShell for Azure is different from the PowerShell installed by default in Windows 7, 8 or 2008 or 2012. It has exclusive commands (cmdlets) for Azure activities.

    Microsoft Azure PowerShell将类似于此屏幕截图。


    图8. PowerShell命令行

    PowerShell for Azure与Windows 7、8或2008或2012中默认安装的PowerShell不同。它具有用于Azure活动的独占命令(cmdlet)。

  8. Once the Microsoft Azure PowerShell is installed, you can connect to your Azure Subscription using the Get-AzurePublishSettingsFile.
    Get-AzurePublishSettingsFile

    This command is used to create a file to connect to your Azure Portal and automate tasks there.

    如何使用PowerShell处理SQL Azure数据库

    Figure 9. The Get-AzurePublishSettingsFile command.

    一旦安装了Microsoft Azure PowerShell,就可以使用Get-AzurePublishSettingsFile连接到Azure订阅。
    ➜Get-AzurePublishSettingsFile

    此命令用于创建文件以连接到您的Azure门户并在那里自动执行任务。


    图9. Get- AzurePublishSettingsFile 命令。

  9. The command will prompt your browser in the Azure Portal. Choose the account than you want to use.

    如何使用PowerShell处理SQL Azure数据库

    Figure 10. The Windows to the Azure Portal section to select an account

    该命令将在Azure门户中提示您的浏览器。 选择您要使用的帐户。


    图10. Windows到Azure Portal 区域以选择一个帐户

  10. Specify your login and password and Sign in.

    如何使用PowerShell处理SQL Azure数据库

    Figure 11. The Login and Password to Sign in

    指定您的登录名和密码,然后登录。


    图11. 登录名和登录密码

  11. A subscription file will be downloaded. This file will be used to connect to Azure.

    如何使用PowerShell处理SQL Azure数据库

    Figure 12. The downloading window of the bublish profile.

    订阅文件将被下载。 此文件将用于连接到Azure。


    图12.冒泡配置文件的下载窗口。

  12. The subscription file generated is an xml file similar to this one.

    如何使用PowerShell处理SQL Azure数据库

    Figure 13. The Published File.

    生成的订阅文件是一个与此类似的xml文件。


    图13.发布的文件。

  13. Once the file is generated, you can import the file using the Import-AzurePublishSettingsFile command like this:

    >Import-AzurePublishSettingsFile –PublishSettingsFile
    c:\yourpath\yourpublishfilename.publishsettings

    如何使用PowerShell处理SQL Azure数据库

    Figure 14. The message after importing the file.

    With the import command you can now connect to your Azure Subscription.
    You just need to specify the path of the subscription file.

    生成文件后,可以使用Import-AzurePublishSettingsFile命令导入文件,如下所示:

    > Import-AzurePublishSettingsFile –PublishSettingsFile
    c:\ yourpath \ yourpublishfilename.publishsettings


    图14.导入文件后的消息。

    现在,可以使用import命令连接到Azure订阅。
    您只需要指定订阅文件的路径即可。

  14. One good command to start it the get-azuresqldatabaseserver

    >get-azuresqldatabaseserver

    With this command you can obtain the SQL Server Name.

    如何使用PowerShell处理SQL Azure数据库

    Figure 15. The Database Servers.

    In this example, you have 2 Server names, the location and the administrator login names.

    启动get-azuresqldatabaseserver的一个好命令

    > get-azuresqldatabaseserver

    使用此命令可以获取SQL Server名称。


    图15.数据库服务器。

    在此示例中,您有2个服务器名称,位置和管理员登录名。

  15. Another important command is the Get-AzureSqlDatabase. The command lists the databases of a specific server.

    Get-AzureSqlDatabase -ServerName "kadhym5ybh"

    如何使用PowerShell处理SQL Azure数据库

    Figure 16. The list of Azure Databases

    The command will display all of the databases of the Server named kadhym5ybh. It will also display the CollationName, Edition, MaxSize in Gb, Bytes, the creating date, Service Objective Name and RecoveryPeriodStartDate.

    另一个重要的命令是Get-AzureSqlDatabase。 该命令列出了特定服务器的数据库。

    Get-AzureSqlDatabase -ServerName“ kadhym5ybh”


    图16. Azure数据库列表

    该命令将显示名为kadhym5ybh的服务器的所有数据库。 它还将显示Collat​​ionName,版本,以Gb为单位的MaxSize,字节,创建日期,服务目标名称和RecoveryPeriodStartDate。

  16. The other command is the New-AzureSqlDatabase. This command is used to create a New Database. You can also especify the Maximum Size, the Edition of the Database and the Collation. The following example creates a Database named Database1 in the kadhym5ybh Server. The database has a maximum size of 5 GB and it is a Web edition with the SQL_Latin1_General collation. The editions can be Web, Business, Basic, Standard, or Premium. The default edition is the Web edition.

    New-AzureSqlDatabase -ServerName "kadhym5ybh" -DatabaseName "sqlshack" -Edition "Web" -MaxSizeGB 5 -Collation "SQL_Latin1_General_CP1_CI_AS"

    如何使用PowerShell处理SQL Azure数据库

    Figure 17. The new database created

    另一个命令是New-AzureSqlDatabase。 此命令用于创建新数据库。 您还可以指定最大大小,数据库版本和排序规则。 以下示例在kadhym5ybh服务器中创建一个名为Database1的数据库。 该数据库的最大大小为5 GB,它是具有SQL_Latin1_General排序规则的Web版本。 这些版本可以是Web,Business,Basic,Standard或Premium。 默认版本是Web版本。

    New-AzureSqlDatabase -ServerName“ kadhym5ybh” -DatabaseName“ sqlshack ”-版本“ Web ” -MaxSizeGB 5-整理​​“ SQL_Latin1_General_CP1_CI_AS”


    图17.创建的新数据库

  17. You can also verify if the database sqlshack was created with 5 GB in the Azure Portal in the Databases section.

    如何使用PowerShell处理SQL Azure数据库

    Figure 18. The Database in the Azure Portal

    您还可以在“数据库”部分的“ Azure门户”中验证是否使用5 GB创建了数据库sqlshack。


    图18. Azure门户中的数据库

  18. Another PowerShell command is the set-AzureSqlDatabase. With this command you can modify the database properties.

    To do this, first you need to store the database information in a variable. In this example the $db1 variable.

    PS C:\> $db1 = Get-AzureSqlDatabase -ServerName "kadhym5ybh" -DatabaseName "sqlshack”

    另一个PowerShell命令是set-AzureSqlDatabase。 使用此命令,您可以修改数据库属性。

    为此,首先需要将数据库信息存储在变量中。 在此示例中,$ db1变量。

    PS C:\> $ db1 = Get-AzureSqlDatabase -ServerName“ kadhym5ybh” -DatabaseName“ sqlshack”

  19. The next step is to use the Set-AzureSqlDatabase command to change a database property. In this example, we are modifying the Maximum size of the Database1 from 50 to 20.

    PS C:\> Set-AzureSqlDatabase -ServerName "kadhym5ybh" -Database $db1 -MaxSizeGB 1

    如何使用PowerShell处理SQL Azure数据库

    Figure 19. The Database size

    下一步是使用Set-AzureSqlDatabase命令更改数据库属性。 在此示例中,我们将Database1的最大大小从50修改为20。

    PS C:\> Set-AzureSqlDatabase -ServerName“ kadhym5 ybh”-数据库$ db1 -MaxSizeGB 1


    图19.数据库大小

  20. You can also store the information using the out-file cmdlet. The following example shows how to store the list of database servers in a text file named PS_Results.txt in the scripts folder:

    get-azuresqldatabaseserver | out-file c:\scripts\PS_Results.txt

    如何使用PowerShell处理SQL Azure数据库

    Figure 20. The text file with the Servers

    您还可以使用输出文件cmdlet存储信息。 以下示例显示如何将数据库服务器列表存储在脚本文件夹中名为PS_Results.txt的文本文件中:

    get-azuresql数据库服务器| 外档c:\ scripts \ PS_Results.txt


    图20.带有服务器的文本文件

  21. You can also export your values to a csv file using the export-csv cmdlet.

    get-azuresqldatabaseserver | export-csv -path C:\scripts\csvresults.csv

    如何使用PowerShell处理SQL Azure数据库

    Figure 21. The csv results in MS Excel

    您还可以使用export-csv cmdlet将值导出到csv文件。

    get-azuresql数据库服务器| export-csv-路径C:\ scripts \ csvresults.csv


    图21. MS Excel中的csv结果

  22. Or maybe in you will like the results in xml format using the Export-Clixml cmdlet:

    get-azuresqldatabaseserver | Export-Clixml c:\scripts\results.xml

    如何使用PowerShell处理SQL Azure数据库

    Figure 22. The results in xml format

    或者,也许您会喜欢使用Export-Clixml cmdlet以xml格式显示结果:

    get-azuresql数据库服务器| Export-Clixml c:\ scripts \ results.xml


    图22. xml格式的结果

  23. It is also possible to store the results in html format using the ConvertTo-HTML cmdlet combined with the Out-File.

    get-azuresqldatabaseserver | ConvertTo-HTML | Out-File C:\Scripts\results.html

    如何使用PowerShell处理SQL Azure数据库

    Figure 23. The results in html format

    也可以将ConvertTo-HTML cmdlet与Out-File结合使用,以html格式存储结果。

    get-azuresql数据库服务器| ConvertTo-HTML | 外档C:\ Scripts \ results.html


    图23. html格式的结果

  24. If you want to filter some columns, you can use the select command. The following example shows the use of the select and also set the format of the results as a list instead of a table.

    get-azuresqldatabaseserver | select ServerName, Location | Format-List

    如何使用PowerShell处理SQL Azure数据库

    Figure 24. The results of the select in a Format of a list.

    如果要过滤某些列,则可以使用select命令。 以下示例说明了select的用法,还将结果的格式设置为列表而不是表。

    get-azuresql数据库服务器| 选择ServerName,位置| 格式清单


    图24.以列表格式选择的结果。

  25. In order to get a list of cmdlets available, you can use the Get-Command

    Get-Command

    如何使用PowerShell处理SQL Azure数据库

    Figure 25. The list of cmdlets of Azure PowerShell

    为了获得可用的cmdlet列表,可以使用Get-Command

    获取命令


    图25. Azure PowerShell的cmdlet列表

  26. If you want an example of a specific command, you can use the get help cmdlet.

    The following example shows how to use the the Remove-AzureSqlDatabase cmdlet.

    get-help Remove-AzureSqlDatabase –detailed

    如何使用PowerShell处理SQL Azure数据库

    Figure 26. The help of the Remove-AzureSqlDatabase

    结论 ( Conclusion )

    In this article, we installed the Microsoft Azure PowerShell and then we used some PowerShell commands to create, modify Azure Databases from our local machine using PowerShell. We also stored the results in different formats like html, xml, csv and txt.

    如果需要特定命令的示例,则可以使用get help cmdlet。

    以下示例显示如何使用Remove-AzureSqlDatabase cmdlet。

    获取帮助Remove-AzureSqlDatabase –详细


    图26. Remove-AzureSqlDatabase的帮助

    在本文中,我们安装了Microsoft Azure PowerShell,然后使用一些PowerShell命令使用PowerShell从本地计算机创建和修改Azure数据库。 我们还将结果存储为html,xml,csv和txt等不同格式。

翻译自: https://www.sqlshack.com/how-to-work-handle-sql-azure-databases-with-powershell/