你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn。
使用 Azure PowerShell 创建虚拟机
在本教程中,你将了解使用 Azure PowerShell 设置虚拟机所涉及的所有步骤。 本教程还介绍了输出查询、Azure 资源重用和资源清理。
本教程可以通过 Azure Cloud Shell 提供的交互式体验完成,也可以 在本地安装 Azure PowerShell。
使用 ctrl-shift-v(macOS 上的 cmd-shift-v)将教程文本粘贴到 Azure Cloud Shell 中。
登录
如果使用 Azure PowerShell 的本地安装,则需要在执行任何其他步骤之前登录。
Connect-AzAccount
按照终端中显示的步骤完成登录过程。
创建资源组
在 Azure 中,所有资源都在资源管理组中分配。 资源组提供资源的逻辑分组,使它们更易于用作集合。
在本教程中,所有已创建的资源都进入名为 TutorialResources
的单个组。
New-AzResourceGroup -Name TutorialResources -Location eastus
ResourceGroupName : TutorialResources
Location : eastus
ProvisioningState : Succeeded
Tags :
ResourceId : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/TutorialResources
为 VM 创建管理员凭据
在创建新的虚拟机之前,必须创建一个凭据对象,其中包含 Windows VM 的管理员帐户的用户名和密码。
$cred = Get-Credential -Message "Enter a username and password for the virtual machine."
出现提示时输入用户名和密码。 生成的凭据对象在下一步中作为参数传递。
Windows PowerShell credential request.
Enter a username and password for the virtual machine.
User: tutorAdmin
Password for user tutorAdmin: *********
创建虚拟机
Azure 中的虚拟机具有大量依赖项。 Azure PowerShell 根据指定的命令行参数为你创建这些资源。 为了便于阅读,我们使用 PowerShell splatting 将参数传递给 Azure PowerShell cmdlet。
创建新的运行 Windows 的虚拟机。
$vmParams = @{
ResourceGroupName = 'TutorialResources'
Name = 'TutorialVM1'
Location = 'eastus'
ImageName = 'Win2016Datacenter'
PublicIpAddressName = 'tutorialPublicIp'
Credential = $cred
OpenPorts = 3389
Size = 'Standard_D2s_v3'
}
$newVM1 = New-AzVM @vmParams
创建 VM 时,会看到所使用的参数值和正在创建的 Azure 资源。 PowerShell 将显示进度栏,如下所示。
Creating Azure resources
39% \
[ooooooooooooooooooooooooooooooooooo ]
Creating TutorialVM1 virtual machine.
VM 准备就绪后,可以在 Azure 门户中或通过检查 $newVM1
变量来查看结果。
$newVM1
ResourceGroupName : TutorialResources
Id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/TutorialResources/providers/Microsoft.Compute/virtualMachines/TutorialVM1
VmId : 00000000-0000-0000-0000-000000000000
Name : TutorialVM1
Type : Microsoft.Compute/virtualMachines
Location : eastus
Tags : {}
HardwareProfile : {VmSize}
NetworkProfile : {NetworkInterfaces}
OSProfile : {ComputerName, AdminUsername, WindowsConfiguration, Secrets}
ProvisioningState : Succeeded
StorageProfile : {ImageReference, OsDisk, DataDisks}
大括号内列出的属性值是嵌套对象。 在下一步中,我们将演示如何查看这些嵌套对象中的特定值。
使用查询获取 VM 信息
让我们从刚刚创建的 VM 中获取一些更详细的信息。 在此示例中,我们将验证 VM 的名称和我们创建的管理员帐户。
$newVM1.OSProfile | Select-Object -Property ComputerName, AdminUserName
ComputerName AdminUsername
------------ -------------
TutorialVM1 tutorialAdmin
可以使用其他 Azure PowerShell 命令获取有关网络配置的特定信息。
$newVM1 | Get-AzNetworkInterface |
Select-Object -ExpandProperty IpConfigurations |
Select-Object -Property Name, PrivateIpAddress
在此示例中,我们使用 PowerShell 管道将 $newVM 1 对象发送到 Get-AzNetworkInterface
cmdlet。 从生成的网络接口对象中选择嵌套的 IpConfigurations 对象。 从 IpConfigurations 对象中选择 Name 和 PrivateIpAddress 属性。
Name PrivateIpAddress
---- ----------------
TutorialVM1 192.168.1.4
若要确认 VM 正在运行,需要通过远程桌面进行连接。 为此,我们需要知道公共 IP 地址。
$publicIp = Get-AzPublicIpAddress -Name tutorialPublicIp -ResourceGroupName TutorialResources
$publicIp |
Select-Object -Property Name, IpAddress, @{label='FQDN';expression={$_.DnsSettings.Fqdn}}
在此示例中,我们使用 Get-AzPublicIpAddress
并将结果存储在 $publicIp
变量中。 在此变量中,我们将选择属性并使用表达式来检索嵌套的 Fqdn 属性。
Name IpAddress FQDN
---- --------- ----
tutorialPublicIp <PUBLIC_IP_ADDRESS> tutorialvm1-8a0999.eastus.cloudapp.azure.com
从本地计算机,可以运行以下命令,通过远程桌面连接到 VM。
mstsc.exe /v $publicIp.IpAddress
有关查询对象属性的详细信息,请参阅 查询 Azure 资源。
在现有子网上创建新 VM
第二个 VM 使用现有子网。
$vm2Params = @{
ResourceGroupName = 'TutorialResources'
Name = 'TutorialVM2'
ImageName = 'Win2016Datacenter'
VirtualNetworkName = 'TutorialVM1'
SubnetName = 'TutorialVM1'
PublicIpAddressName = 'tutorialPublicIp2'
Credential = $cred
OpenPorts = 3389
}
$newVM2 = New-AzVM @vm2Params
$newVM2
ResourceGroupName : TutorialResources
Id : /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/TutorialResources/providers/Microsoft.Compute/virtualMachines/TutorialVM2
VmId : 00000000-0000-0000-0000-000000000000
Name : TutorialVM2
Type : Microsoft.Compute/virtualMachines
Location : eastus
Tags : {}
HardwareProfile : {VmSize}
NetworkProfile : {NetworkInterfaces}
OSProfile : {ComputerName, AdminUsername, WindowsConfiguration, Secrets}
ProvisioningState : Succeeded
StorageProfile : {ImageReference, OsDisk, DataDisks}
FullyQualifiedDomainName : tutorialvm2-dfa5af.eastus.cloudapp.azure.com
可以跳过几个步骤来获取新 VM 的公共 IP 地址,因为它在 $newVM2
对象的 FullyQualifiedDomainName 属性中返回。 使用以下命令通过远程桌面进行连接。
mstsc.exe /v $newVM2.FullyQualifiedDomainName
清理
完成本教程后,即可清理已创建的资源。 可以使用 Remove-AzResource
命令删除单个资源,但删除资源组中的所有资源的最安全方法是使用 Remove-AzResourceGroup
命令删除该组。
$job = Remove-AzResourceGroup -Name TutorialResources -Force -AsJob
$job
Id Name PSJobTypeName State HasMoreData Location Command
-- ---- ------------- ----- ----------- -------- -------
1 Long Running... AzureLongRun... Running True localhost Remove-AzResource...
此命令将删除在教程中创建的资源,并保证按正确的顺序解除分配资源。
AsJob
参数使 PowerShell 在删除时阻止。 若要等到删除完成,请使用以下命令:
Wait-Job -Id $job.Id
完成清理后,完成本教程。 继续了解所学内容以及指向资源的链接,这些资源将帮助你完成后续步骤。
总结
祝贺! 你已了解如何使用新的或现有的资源创建 VM、已使用表达式和其他 Azure PowerShell 命令来捕获要存储在 shell 变量中的数据,并查看为 Azure VM 创建的一些资源。
从此处转到的位置取决于你计划如何使用 Azure PowerShell。 在本教程中介绍的功能方面,有各种各样的材料进一步深入介绍。
深入的 Azure PowerShell 文档
可能需要一些时间来浏览 Azure PowerShell 文档的完整 集。
有关本教程中使用的命令的详细信息,请参阅以下文章。
- New-AzResourceGroup
- Get-Credential
- New-AzVM
- Select-Object
- Get-AzPublicIpAddress
- Remove-AzResourceGroup
- 等待作业
此外,还有一些文章更深入地介绍本教程中显示的功能。
示例脚本
若要立即开始使用特定任务,请查看一些示例脚本。
反馈
如果要提供反馈、建议或提出问题,可通过多种方式联系。
-
Send-Feedback
是 Azure PowerShell 的内置命令,可用于向团队提供自由格式反馈。 - Azure PowerShell 存储库中提交功能请求或 bug 报告。
- 通过在 Azure PowerShell 文档存储库提交问题来提问或获取澄清。
我们希望你喜欢使用 Azure PowerShell!
你有关于此部分的问题? 如果有,请向我们提供反馈,以便我们对此部分作出改进。