赞
踩
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中创建虚拟机。
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
获得天蓝色订阅
The command will provide all the Subscription information:
该命令将提供所有订阅信息:
图1.订阅信息
The information that we need is the SubscriptionName. You could also use the following command to get just the required information:
我们需要的信息是SubscriptionName。 您还可以使用以下命令来获取所需的信息:
Get-AzureSubscription | select SubscriptionName
Get-Azure订阅| 选择SubscriptionName
$SubscriptionName= ‘Visual Studio Ultimate with MSDN’
$ SubscriptionName = “ 带有MSDN 的 Visual Studio Ultimate ”
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.
在Azure门户中,您可以通过单击用户名>查看我的帐单找到相同的信息,只是进行比较。 此步骤仅是为了验证PowerShell并将其与门户进行比较。
图2.帐单信息
In the subscriptions, you will find the subscription name.
在订阅中,您将找到订阅名称。
图3. 订阅类型
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:
我们可以使用以下命令获取StorageAccounts列表:
Get-AzureStorageAccount | select label
Get-AzureStorageAccount | 选择标签
The information displayed is the following:
显示的信息如下:
图4. Azure存储帐户
You can store the storageAccount information with this command:
您可以使用以下命令存储storageAccount信息:
$storageaccount=’mysqlshackstorage’
$ storageaccount ='mysqlshackstorage'
You can compare this information with the Storage section in the Azure Portal.
您可以将此信息与Azure门户中的“存储”部分进行比较。
图5. Azure门户中的存储列表
You can also check the currentStorageAccount using the command of the step 1.
您还可以使用步骤1的命令检查currentStorageAccount。
图6. 当前的存储帐户名称
Something important is the Location and Geo-Location. You can find this information using the following commands:
重要的是位置和地理位置。 您可以使用以下命令找到此信息:
Get-AzureStorageAccount | select
label,location,geoprimarylocation
Get-AzureStorageAccount | 选择
标签,位置,地原位置
The location is used to locate your service near you. That may improve the speed of your Services in Azure.
该位置用于在您附近找到您的服务。 这可以提高Azure中服务的速度。
In this example the location will be West US.
在此示例中,位置将是美国西部。
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创建的存储。
Another step is the image used to create the virtual machine. The following command will provide the images related to SQL Server:
另一个步骤是用于创建虚拟机的映像。 以下命令将提供与SQL Server相关的图像:
Get-AzureVMImage | select ImageName
GET-AzureVMImage | 选择 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.
该命令将显示所有Azure映像,包括具有Linux,Oracle,SharePoint,SQL Server等的服务器。这些映像用于轻松创建安装了软件的虚拟机,而不是从零开始安装。
In this example, we will use a SQL Server 14 RTM Web Edition:
在此示例中,我们将使用SQL Server 14 RTM Web版:
$image=’fb83b3509582419d99629ce476bcb5c8__SQL-Server-2014-RTM-12.0.2361.0-Web-ENU-Win2012R2-cy14su05′
$ image ='fb83b3509582419d99629ce476bcb5c8__SQL-Server-2014-RTM-12.0.2361.0-Web-ENU-Win2012R2-cy14su05'
Set-AzureSubscription -SubscriptionName $SubscriptionName -CurrentStorageAccount $storageaccount
设置AzureSubscription -SubscriptionName $ SubscriptionName -CurrentStorageAccount $ storageaccount
You will also need a CloudService name. First, let’s add a service name in a variable:
您还将需要一个CloudService名称。 首先,让我们在变量中添加服务名称:
$cloudServiceName = ‘sqlshackpsserv‘
$ cloudServiceName =' sqlshackpsserv '
New-AzureService -ServiceName $cloudServiceName -Label "my service" -Location $dclocation
New-AzureService -ServiceName $ cloudServiceName-标签“我的服务”-位置$ dclocation
You can verify the cloud service created in the portal.
您可以验证在门户中创建的云服务。
We will also need the Administrator username and password to login the Virtual Azure machine.
我们还需要管理员用户名和密码来登录Virtual Azure计算机。
$UserName = ‘Daniel‘
$Password = ‘Security12!@‘
$ UserName =' 丹尼尔 '
$ 密码='Security12 !@ '
You will also need a Virtual Machine name:
您还将需要一个虚拟机名称:
$virtual_name = ‘VMSqlShack‘
$ 虚拟 _ 名称 =' VMSqlShack '
New-AzureVMConfig -Name $virtual_name –InstanceSize Large –ImageName $image | Add-AzureProvisioningConfig -Windows -Password $Password –AdminUsername $UserName | New-AzureVM –ServiceName $cloudServiceName
新建- AzureVMConfig 杂牌$ V irtual _name - InstanceSize 大- ImageName $图片| Add- AzureProvisioningConfig -Windows -Password $ Password – AdminUsername $ UserName | 新建- AzureVM - 服务名称 $ 云 洱 V I C ^ê 名称
If everything is correct, you will be able to see the the Virtual Machine created in the Azure Portal:
如果一切正确,您将能够看到在Azure门户中创建的虚拟机:
It may take some minutes for the virtual machine to start.
虚拟机可能需要几分钟才能启动。
You can get the virtual machine information using the following command:
您可以使用以下命令获取虚拟机信息:
Get–AzureVM –ServiceName $cloudServiceName –Name $virtual_name
获取 – AzureVM – ServiceName $ cloudServiceName – 名称$ virtual_name
The information displayed by the Get-AzureVM will be similar to this picture:
Get-AzureVM显示的信息将类似于此图片:
Get-AzureRemoteDesktopFile –ServiceName $cloudServiceName -Name $virtual_name –LocalPath "c:\myvm\psVirtual.rdp"
Get- AzureRemoteDesktopFile – ServiceName $ cloudServiceName -Name $ virtual_name – LocalPath “ c:\ myvm \ psVirtual.rdp”
You can now connect to your machine by double clicking on the rdp file.
现在,您可以通过双击rdp文件连接到计算机。
文件12. rpd 文件
Now you can connect to your machine with the credentials provided on step 20.
现在,您可以使用步骤20提供的凭据连接到计算机。
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):
最后,如果该计算机用于测试目的,则可以使用以下命令停止虚拟机(强烈建议使用该命令,因为Azure会按每分钟启动的计算机收费):
Stop-AzureVM –ServiceName $cloudServiceName –Name $virtual_name
停止 -AzureVM – ServiceName $ cloudServiceName – 名称$ virtual_name
When you run this command:
运行此命令时:
New-AzureVMConfig -Name $virtual_name –InstanceSize Large –ImageName $image | Add-AzureProvisioningConfig -Windows -Password $Password –AdminUsername $UserName | New-AzureVM –ServiceName $cloudServiceName
新建 -AzureVMConfig -Name $ virtual_name – InstanceSize Large – ImageName $ image | Add- AzureProvisioningConfig -Windows -Password $ Password – AdminUsername $ UserName | 新建 -AzureVM – ServiceName $ cloudServiceName
A typical error is this one:
一个典型的错误是这样的:
Add-AzureProvisioningConfig : A parameter cannot be found that matches parameter name ‘MediaLocation’.
Add-AzureProvisioningConfig:找不到与参数名称“ 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.
发生此错误时,我只是将虚拟映像更改为另一个SQL Server映像(在步骤14中),因为使用的映像已过期。
When I run this command:
当我运行此命令时:
New-AzureService -ServiceName $cloudSvcName -Label "MyLabel" -Location "DC-LOCATION"
New-AzureService -ServiceName $ cloudSvcName -Label“ MyLabel” -位置“ DC-LOCATION”
I received the following output:
我收到以下输出:
VERBOSE: 9:12:48 AM – Begin Operation: New-AzureService
详细信息:9:12:48 AM –开始运营:New-AzureService
New-AzureService : ConflictError: The specified DNS name is already taken.
New-AzureService:ConflictError:指定的DNS名称已被使用。
At line:1 char:1
在第1行:char:1
+ New-AzureService -ServiceName $cloudSvcName -Label "MyLabel" -Location $dclocati …
+ New-AzureService -ServiceName $ cloudSvcName -Label“ MyLabel”-位置$ dclocati…
+ CategoryInfo : CloseError: (:) [New-AzureService], CloudException
+ CategoryInfo:CloseError :( :) [New-AzureService],CloudException
+ FullyQualifiedErrorId : Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.NewAzureServiceCommand
+ FullyQualifiedErrorId:Microsoft.WindowsAzure.Commands.ServiceManagement.HostedServices.NewAzureServiceCommand
The solution is just to try to assign a diferent service name.
解决方案只是尝试分配不同的服务名称。
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/
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。