您可以通过 powershell 编写所有虚拟机创建过程的脚本,包括创建资源组、子网、vnet、公共 ip、网络安全组、存储等等。
您可以将所有 vm 资源附加到一个资源组,通过执行此操作,您可以通过执行一个命令行轻松删除 vm 和 realted 资源。
安装和导入 AzureRM
为了使用和执行以下命令,如果我们还没有 AzureRM powershell 模块,我们需要安装和导入它,如果你已经拥有它可以跳过本节。
PS:您需要提升权限才能从 PowerShell 库安装模块
`Install-Module -Name AzureRM -AllowClobber`
默认情况下,PowerShell 库未配置为 PowerShellGet 的受信任存储库。首次使用 PSGallery 时,会显示以下消息:
Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change
its InstallationPolicy value by running the Set-PSRepository cmdlet.
Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes [A] Yes to All [N] No [L] No to All [S] Suspend [?] Help (default is "N"):
So, don't worry about this message.
安装后,您可以通过运行以下命令导入 AzureRM:
Import-Module AzureRM
最后,要完成本节,我们需要连接到 Azure 帐户,只需执行此命令,系统就会提示您:
# Connect to Azure with an interactive dialog for sign-in
Connect-AzureRmAccount
创建资源组
资源组是部署和管理 Azure 资源的逻辑容器。
在您的 SDK 中,运行以下代码块来创建资源组:
# Create variables to store the location and resource group names.
$location = "francecentral"
$ResourceGroupName = "resource-group-1"
New-AzureRmResourceGroup -Name $ResourceGroupName -Location $location
创建存储资源
创建一个存储帐户来存储启动诊断的输出。
# Create variables to store the storage account name and the storage account SKU information
$StorageAccountName = "msstorage01"
$SkuName = "Standard_LRS"
# Create a new storage account
$StorageAccount = New-AzureRMStorageAccount `
-Location $location `
-ResourceGroupName $ResourceGroupName `
-Type $SkuName `
-Name $StorageAccountName
Set-AzureRmCurrentStorageAccount `
-StorageAccountName $storageAccountName `
-ResourceGroupName $resourceGroupName
创建网络资源
创建 VNet(虚拟网络)、子网和公共 IP 地址。创建这些 Azure 资源有助于我们为我们的 VM 提供网络连接。
# Create a the subnet configuration
$subnetConfig = New-AzureRmVirtualNetworkSubnetConfig `
-Name subNet-1 `
-AddressPrefix 192.168.1.0/24
创建虚拟网络
$vnet = New-AzureRmVirtualNetwork `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name vNet-1 `
-AddressPrefix 192.168.0.0/16 `
-Subnet $subnetConfig
创建公共 IP 地址并指定 DNS 名称
$publicip = New-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-AllocationMethod Static `
-IdleTimeoutInMinutes 4 `
-Name "mypublicdns$(Get-Random)"
创建 NSG(网络安全组)和 NSG 规则
NSG 使用入站和出站规则保护我们的 VM。
现在,我们需要为端口 3389 创建一个入站规则以允许传入 RDP(远程桌面)连接,并为端口 80 创建一个入站规则以让我们的 VM 接收传入的 Web 流量。
为 3389 端口创建入站 NSG 规则
# Create an inbound NSG rule for the 3389 port
# This rule will allow us to connect to the VM via an RDP connection
$nsgrdprule = New-AzureRmNetworkSecurityRuleConfig `
-Name nsg-rdp-rule `
-Protocol Tcp `
-Direction Inbound `
-Priority 1000 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 3389 `
-Access Allow
为端口 80 创建入站网络安全组规则
# This rule will allow the VM to receive incoming web connections via the port 80
$nsgwebrule = New-AzureRmNetworkSecurityRuleConfig `
-Name nsg-inbound-www-rule `
-Protocol Tcp `
-Direction Inbound `
-Priority 1001 `
-SourceAddressPrefix * `
-SourcePortRange * `
-DestinationAddressPrefix * `
-DestinationPortRange 80 `
-Access Allow
创建 NSG(网络安全组)
# This will wrap up previously created rules (nsg-web-rule and nsg-rdp-rule) within an NSG
$nsg = New-AzureRmNetworkSecurityGroup `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-Name nsg-1 `
-SecurityRules $nsgrdprule,$nsgwebrule
# This command will create a VNC (virtual network card) and associate it with public IP address and NSG
$nic = New-AzureRmNetworkInterface `
-Name nic-1 `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-SubnetId $vnet.Subnets[0].Id `
-PublicIpAddressId $publicip.Id `
-NetworkSecurityGroupId $nsg.Id
# Define a credential object to store the username and password for the VM
$UserName='ali.mselmi'
$Password='P@ssword123'| ConvertTo-SecureString -Force -AsPlainText
$Credential=New-Object PSCredential($UserName,$Password)
创建虚拟机配置对象
$VmName = "VirtualMachinelatest"
$VmSize = "Standard_A1"
$VirtualMachine = New-AzureRmVMConfig `
-VMName $VmName `
-VMSize $VmSize
$VirtualMachine = Set-AzureRmVMOperatingSystem `
-VM $VirtualMachine `
-Windows `
-ComputerName "MainComputer" `
-Credential $Credential -ProvisionVMAgent
$VirtualMachine = Set-AzureRmVMSourceImage `
-VM $VirtualMachine `
-PublisherName "MicrosoftWindowsServer" `
-Offer "WindowsServer" `
-Skus "2016-Datacenter" `
-Version "latest"
# Sets the operating system disk properties on a VM.
$VirtualMachine = Set-AzureRmVMOSDisk `
-VM $VirtualMachine `
-CreateOption FromImage | `
Set-AzureRmVMBootDiagnostics -ResourceGroupName $ResourceGroupName `
-StorageAccountName $StorageAccountName -Enable |`
Add-AzureRmVMNetworkInterface -Id $nic.Id
创建虚拟机
最后我们可以创建虚拟机部署配置了。
# Create the VM.
New-AzureRmVM `
-ResourceGroupName $ResourceGroupName `
-Location $location `
-VM $VirtualMachine
我们可以通过 Azure 门户检查 VM 创建:
连接到虚拟机
要远程访问我们在上一步中创建的 VM,我们还需要预先设置其公共 IP 地址。
为此,我们只需要运行以下命令并获取公共 IP 地址:
Get-AzureRmPublicIpAddress `
-ResourceGroupName $ResourceGroupName | Select IpAddress
现在,我们可以在与虚拟机创建远程桌面会话后进行远程桌面访问,只需将 IP 地址替换为虚拟机的 publicIPAddress。
出现提示时,您可以使用创建 VM 时使用的凭据登录。
`mstsc /v publicIpAddress`
使用 RDP、SSH 或堡垒连接到 Azure VM
您可以通过 RDP、SSH 或 Bastion 连接到 VM,您只需通过 Azure 门户单击创建的 VM,然后单击连接。
删除虚拟机
我们可以使用以下命令来移除包含虚拟机及其相关资源的资源组:
Remove-AzureRmResourceGroup `
-Name $ResourceGroupName
最后的话...
编写整个虚拟机创建过程的脚本的好处是,我们通常不需要创建单个虚拟机,而是创建多个虚拟机,为该过程创建脚本可以让我们灵活地大规模自定义虚拟机的创建.
原创博文
Create a Windows Server virtual machine with PowerShell