如何使用PowerShell在Azure中创建SQL虚拟机

介绍 ( Introduction )

The first time that you create your VM in Azure manually using the UI is a very pleasant experience. However, when you have hundred and sometimes thousands of computers it is extremely exhausting and boring to create and configure it.

第一次使用UI在Azure中手动创建VM的经历非常愉快。 但是,当您有成百上千的计算机时,创建和配置它就非常耗费精力。

With PowerShell, it is possible to automate several administrative tasks and create scripts to automatically create VMs, enable ports, download and create remote desktop files, administer services, etc.

使用PowerShell,可以自动执行多个管理任务并创建脚本以自动创建VM,启用端口,下载和创建远程桌面文件,管理服务等。

In this new chapter, we will show how to create a Virtual Machine in Azure with SQL Server installed using PowerShell.

在这一新的章节中,我们将展示如何在使用PowerShell安装SQL Server的Azure中创建虚拟机。

要求 ( Requirements )

入门 ( Getting started )

  1. The first thing that you need to know is your subscription information. You can use the following command to get your subscription information:

    Get-AzureSubscription

    您需要了解的第一件事是您的订阅信息。 您可以使用以下命令获取您的订阅信息:

    获得天蓝色订阅

  2. The command will provide all the Subscription information:


    Figure 1. Subscription information

    该命令将提供所有订阅信息:


    图1.订阅信息

  3. The information that we need is the SubscriptionName. You could also use the following command to get just the required information:

    Get-AzureSubscription | select SubscriptionName

    我们需要的信息是SubscriptionName。 您还可以使用以下命令来获取所需的信息:

    Get-Azure订阅| 选择SubscriptionName

  4. Once you have the SubscriptionName, store the information in a variable:

    $SubscriptionName= Visual Studio Ultimate with MSDN

    $ SubscriptionName = 带有MSDN Visual Studio Ultimate

  5. In the Azure Portal you can find the same information just to compare by clicking in the user name> View my bill. This step is just to verify and compare PowerShell with the portal.


    Figure 2. Billing information

    在Azure门户中,您可以通过单击用户名>查看我的帐单找到相同的信息,只是进行比较。 此步骤仅是为了验证PowerShell并将其与门户进行比较。


    图2.帐单信息

  6. In the subscriptions, you will find the subscription name.


    Figure 3. The type of subscription

    在订阅中,您将找到订阅名称。


    图3. 订阅类型

  7. We also need the storageaccount. The storage is used to store information like backups, files and in this case the Virtual Machine information.

    We can get the list of StorageAccounts with the following command:

    Get-AzureStorageAccount | select label

    我们还需要存储帐户。 该存储用于存储诸如备份,文件之类的信息,在这种情况下,还用于存储虚拟机信息。

    我们可以使用以下命令获取StorageAccounts列表:

    Get-AzureStorageAccount | 选择标签

  8. The information displayed is the following:


    Figure 4. The Azure Storage Accounts

    显示的信息如下:


    图4. Azure存储帐户

  9. You can store the storageAccount information with this command:

    $storageaccount=’mysqlshackstorage’

    您可以使用以下命令存储storageAccount信息:

    $ storageaccount ='mysqlshackstorage'

  10. You can compare this information with the Storage section in the Azure Portal.


    Figure 5. The storage list in the Azure Portal

    您可以将此信息与Azure门户中的“存储”部分进行比较。


    图5. Azure门户中的存储列表

  11. You can also check the currentStorageAccount using the command of the step 1.


    Figure 6. The current Storage Account Name

    您还可以使用步骤1的命令检查currentStorageAccount。


    图6. 当前的存储帐户名称

  12. Something important is the Location and Geo-Location. You can find this information using the following commands:

    Get-AzureStorageAccount | select
    label,location,geoprimarylocation

    The location is used to locate your service near you. That may improve the speed of your Services in Azure.


    Figure 7. The storage, locaton and primary location.

    In this example the location will be West US.

    重要的是位置和地理位置。 您可以使用以下命令找到此信息:

    Get-AzureStorageAccount | 选择
    标签,位置,地原位置

    该位置用于在您附近找到您的服务。 这可以提高Azure中服务的速度。


    图7. 存储,位置和主要位置。

    在此示例中,位置将是美国西部。

  13. If you do not have an storage account you can create one with the following commands:

    $dclocation=’West US’

    $storageaccount=’sqlshackstorage

    New-AzureStorageAccount -StorageAccountName $storageaccount -Location $dclocation

    The $dclocation variable will store the location. You can check the storage created following the step 10.

    如果您没有存储帐户,则可以使用以下命令创建一个:

    $ dclocation ='美国西部'

    $ storageaccount =' sqlshackstorage '

    New-AzureStorageAccount -StorageAccountName $ storageaccount-位置$ dclocation

    $ dclocation变量将存储位置。 您可以检查按照步骤10创建的存储。

  14. Another step is the image used to create the virtual machine. The following command will provide the images related to SQL Server:

    Get-AzureVMImage | select ImageName

    The command will show all the Azure images including Servers with Linux, Oracle, SharePoint, SQL Server, etc. The images are used to easily create virtual machines with software installed instead of installing from zero.


    Figure 8. The list of virtual Images.

    另一个步骤是用于创建虚拟机的映像。 以下命令将提供与SQL Server相关的图像:

    GET-AzureVMImage | 选择 ImageName

    该命令将显示所有Azure映像,包括具有Linux,Oracle,SharePoint,SQL Server等的服务器。这些映像用于轻松创建安装了软件的虚拟机,而不是从零开始安装。


    图8. 虚拟映像列表。

  15. In this example, we will use a SQL Server 14 RTM Web Edition:

    $image=’fb83b3509582419d99629ce476bcb5c8__SQL-Server-2014-RTM-12.0.2361.0-Web-ENU-Win2012R2-cy14su05′

    在此示例中,我们将使用SQL Server 14 RTM Web版:

    $ image ='fb83b3509582419d99629ce476bcb5c8__SQL-Server-2014-RTM-12.0.2361.0-Web-ENU-Win2012R2-cy14su05'

  16. Now set the Azure Subscription

    Set-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccount $storageaccount

    设置AzureSubscription -SubscriptionName $ SubscriptionName -CurrentStorageAccount $ storageaccount

  17. You will also need a CloudService name. First, let’s add a service name in a variable:

    $cloudServiceName = ‘sqlshackpsserv

    您还将需要一个CloudService名称。 首先,让我们在变量中添加服务名称:

    $ cloudServiceName =' sqlshackpsserv '

  18. You can create a new Azure service with the following commands:

    New-AzureService -ServiceName $cloudServiceName -Label "my service" -Location $dclocation

    New-AzureService -ServiceName $ cloudServiceName-标签“我的服务”-位置$ dclocation

  19. You can verify the cloud service created in the portal.


    Figure 9. The list of cloud services

    您可以验证在门户中创建的云服务。


    图9.云服务列表

  20. We will also need the Administrator username and password to login the Virtual Azure machine.

    $UserName = ‘Daniel
    $Password = ‘Security12[email protected]

    我们还需要管理员用户名和密码来登录Virtual Azure计算机。

    $ UserName =' 丹尼尔 '
    $ 密码='Security12 !@ '

  21. You will also need a Virtual Machine name:

    $virtual_name = ‘VMSqlShack

    您还将需要一个虚拟机名称:

    $ 虚拟 _ 名称 =' VMSqlShack '

  22. Now, create a Virtual Machine with a User and Password using the cloudServiceName:

    New-AzureVMConfig -Name $virtual_nameInstanceSize Large –ImageName $image | Add-AzureProvisioningConfig -Windows -Password $Password –AdminUsername $UserName | New-AzureVMServiceName $cloudServiceName

    现在,使用cloudServiceName使用用户和密码创建虚拟机:

    新建- AzureVMConfig 杂牌$ V irtual _name - InstanceSize 大- ImageName $图片| Add- AzureProvisioningConfig -Windows -Password $ Password – AdminUsername $ UserName | 新建- AzureVM - 服务名称 $ V I C 名称

  23. If everything is correct, you will be able to see the the Virtual Machine created in the Azure Portal:


    Figure 10. The virtual machine created

    It may take some minutes for the virtual machine to start.

    如果一切正确,您将能够看到在Azure门户中创建的虚拟机:


    图10.创建的虚拟机

    虚拟机可能需要几分钟才能启动。

  24. You can get the virtual machine information using the following command:

    GetAzureVM ServiceName $cloudServiceName Name $virtual_name

    您可以使用以下命令获取虚拟机信息:

    获取 AzureVM ServiceName $ cloudServiceName 名称$ virtual_name

  25. The information displayed by the Get-AzureVM will be similar to this picture:


    Figure 11. The VM information

    Get-AzureVM显示的信息将类似于此图片:


    图11. VM信息

  26. You can create a remote desktop file using the following command:

    Get-AzureRemoteDesktopFileServiceName $cloudServiceName -Name $virtual_nameLocalPath "c:\myvm\psVirtual.rdp"

    Get- AzureRemoteDesktopFile ServiceName $ cloudServiceName -Name $ virtual_name LocalPath “ c:\ myvm \ psVirtual.rdp”

  27. You can now connect to your machine by double clicking on the rdp file.


    File 12. The rpd file

    现在,您可以通过双击rdp文件连接到计算机。


    文件12. rpd 文件

  28. Now you can connect to your machine with the credentials provided on step 20.


    Figure 13. The virtual machine with SQL Server created

    现在,您可以使用步骤20提供的凭据连接到计算机。


    图13. 创建了SQL Server的虚拟机

  29. Finally, if the machine is for testing purposes, you can stop the Virtual machine using this command (strongly recommended because Azure charges per minute of started machine):

    Stop-AzureVMServiceName $cloudServiceName Name $virtual_name

    最后,如果该计算机用于测试目的,则可以使用以下命令停止虚拟机(强烈建议使用该命令,因为Azure会按每分钟启动的计算机收费):

    停止 -AzureVM ServiceName $ cloudServiceName 名称$ virtual_name

一些典型的错误 ( Some typical errors )

  1. When you run this command:

    New-AzureVMConfig -Name $virtual_nameInstanceSize Large –ImageName $image | Add-AzureProvisioningConfig -Windows -Password $Password –AdminUsername $UserName | New-AzureVMServiceName $cloudServiceName

    A typical error is this one:

    Add-AzureProvisioningConfig : A parameter cannot be found that matches parameter name ‘MediaLocation’.

    When I had this error I just changed the virtual images (in step 14) to a different SQL Server image because the one used expired.

    运行此命令时:

    新建 -AzureVMConfig -Name $ virtual_name InstanceSize Large – ImageName $ image | Add- AzureProvisioningConfig -Windows -Password $ Password – AdminUsername $ UserName | 新建 -AzureVM ServiceName $ cloudServiceName

    一个典型的错误是这样的:

    Add-AzureProvisioningConfig:找不到与参数名称“ MediaLocation”匹配的参数。

    发生此错误时,我只是将虚拟映像更改为另一个SQL Server映像(在步骤14中),因为使用的映像已过期。

  2. When I run this command:

    New-AzureService -ServiceName $cloudSvcName -Label "MyLabel" -Location "DC-LOCATION"

    I received the following output:

    VERBOSE: 9:12:48 AM – Begin Operation: New-AzureService

    New-AzureService : ConflictError: The specified DNS name is already taken.

    At line:1 char:1

    + New-AzureService -ServiceName $cloudSvcName -Label "MyLabel" -Location $dclocati …

    + CategoryInfo : CloseError: (:) [New-AzureService], CloudException

    + FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.NewAzureServiceCommand

    The solution is just to try to assign a diferent service name.

    当我运行此命令时:

    New-AzureService -ServiceName $ cloudSvcName -Label“ MyLabel” -位置“ DC-LOCATION”

    我收到以下输出:

    详细信息:9:12:48 AM –开始运营:New-AzureService

    New-AzureService:ConflictError:指定的DNS名称已被使用。

    在第1行:char:1

    + New-AzureService -ServiceName $ cloudSvcName -Label“ MyLabel”-位置$ dclocati…

    + CategoryInfo:CloseError :( :) [New-AzureService],CloudException

    + FullyQualifiedErrorId:Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.NewAzureServiceCommand

    解决方案只是尝试分配不同的服务名称。

结论 ( Conclusion )

In this chapter, we learned how to create a virtual machine using PowerShell. First, you need to verify the subscription information, use an existing StorageAccount or create a StorageAccount and set Azure Subscriptions.

在本章中,我们学习了如何使用PowerShell创建虚拟机。 首先,您需要验证订阅信息,使用现有的StorageAccount或创建StorageAccount并设置Azure订阅。

You have to create an Azure Service.

您必须创建一个Azure服务。

With that information, you can create a Virtual Machine assigning a user name and password.

利用这些信息,您可以创建分配用户名和密码的虚拟机。

The last part is to create an rdp file to connect remotely to the virtual machine.

最后一部分是创建rdp文件以远程连接到虚拟机。

翻译自: https://www.sqlshack.com/how-to-use-powershell-to-create-a-sql-virtual-machine-in-azure/