如何使用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创建一些数据库,编辑数据库属性并检索数据库信息。


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.

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


    Figure 2. Downloading the installer.


    2 下载安装程序。

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

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


    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.

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


    Figure 4. The PowerShell installer for Azure SDK.


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

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


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


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

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


    Figure 6. The restart message.


    图6.重新启动消息。

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

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


    Figure 7. The Microsoft Azure PowerShell


    图7. Microsoft Azure PowerShell

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

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


    Figure 8. The PowerShell Command line


    图8. PowerShell命令行

    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.

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

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

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

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

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


    Figure 9. The Get-AzurePublishSettingsFile command.


    图9. Get- AzurePublishSettingsFile 命令。

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

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


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


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

  11. Specify your login and password and Sign in.

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


    Figure 11. The Login and Password to Sign in


    图11. 登录名和登录密码

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

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


    Figure 12. The downloading window of the bublish profile.


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

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

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


    Figure 13. The Published File.


    图13.发布的文件。

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

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

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

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


    Figure 14. The message after importing the file.


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

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

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

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

    启动get-azuresqldatabaseserver的一个好命令

    >get-azuresqldatabaseserver

    > get-azuresqldatabaseserver

    With this command you can obtain the SQL Server Name.

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


    Figure 15. The Database Servers.


    图15.数据库服务器。

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

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

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

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

    Get-AzureSqlDatabase -ServerName "kadhym5ybh"

    Get-AzureSqlDatabase -ServerName“ kadhym5ybh”


    Figure 16. The list of Azure Databases


    图16. Azure数据库列表

    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.

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

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

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

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


    Figure 17. The new database created


    图17.创建的新数据库

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

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


    Figure 18. The Database in the Azure Portal


    图18. Azure门户中的数据库

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

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

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

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

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

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

  20. 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.

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

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

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


    Figure 19. The Database size


    图19.数据库大小

  21. 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:

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

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

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


    Figure 20. The text file with the Servers


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

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

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

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

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


    Figure 21. The csv results in MS Excel


    图21. MS Excel中的csv结果

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

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

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

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


    Figure 22. The results in xml format


    图22. xml格式的结果

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

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

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

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


    Figure 23. The results in html format


    图23. html格式的结果

  25. 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.

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

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

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


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


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

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

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

    Get-Command

    获取命令


    Figure 25. The list of cmdlets of Azure PowerShell


    图25. Azure PowerShell的cmdlet列表

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

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

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

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

    get-help Remove-AzureSqlDatabase –detailed

    获取帮助Remove-AzureSqlDatabase –详细


    Figure 26. The help of the Remove-AzureSqlDatabase


    图26. 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.

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值