使用 Azure PowerShell 建立虛擬機
在本教學課程中,您將瞭解使用 Azure PowerShell 設定虛擬機時所涉及的所有步驟。 本教學課程也涵蓋輸出查詢、Azure 資源重複使用和資源清除。
本教學課程可透過 Azure Cloud Shell 提供的互動式體驗來完成,或者您可以 在本機安裝 Azure PowerShell。
使用 ctrl-shift-v (cmd-shift-v macOS) 將教學課程文字貼到 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 物件中,我們選取 [名稱] 和 [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 命令來擷取要儲存在殼層變數中的數據,並查看針對 Azure VM 建立的一些資源。
您從這裡前往的位置取決於您打算如何使用 Azure PowerShell。 本教學課程所涵蓋的功能有各種更深入的材料。
深入的 Azure PowerShell 檔
您可能想要花一些時間探索完整的 azure PowerShell 檔 集。
如需本教學課程中使用的命令詳細資訊,請參閱下列文章。
- New-AzResourceGroup
- Get-Credential
- New-AzVM
- Select-Object
- Get-AzPublicIpAddress
- Remove-AzResourceGroup
- Wait-Job
此外,還有文章會深入探討教學課程中所顯示的功能。
範例腳本
如果您想要立即開始使用特定工作,請查看一些範例腳本。
反饋
如果您想要提供意見反應、建議或提出問題,有多種方式可供您聯繫。
-
Send-Feedback
是 Azure PowerShell 的內建命令,可讓您為小組提供自由格式的意見反應。 - 在 Azure PowerShell 存放庫中提出功能要求或錯誤報告,。
- 在 Azure PowerShell 檔存放庫中提出問題,以提出問題或取得釐清。
我們希望您喜歡使用 Azure PowerShell!
在這個區段有遇到問題嗎? 如果有,請提供意見反應,好讓我們可以改善這個區段。