您的位置:首页 > 运维架构 > Shell

为Azure虚拟机添加第二块网卡

2017-06-09 15:51 211 查看

为Azure虚拟机添加第二块网卡

有时候我们将虚拟机创建好之后发现还需要第二块网卡,使用下面的脚本我们可以轻松的添加第二块网卡到ARM虚拟机。
脚本运行步骤:
1. 弹出登录窗口中输入登录凭据
2.选择要添加网卡的虚拟机
3.选择网卡所在的子网
4.去倒杯水吧......

#------------------------------------------------------------------------------
# User own the risk, otherwise exit.
#
# Azure PowerShell Version:  3.6.0
#
# Create by Zeno.
#------------------------------------------------------------------------------
Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $(Get-Credential -UserName admin@xxx.partner.onmschina.cn -Message Login_AzureChinaCloud) |Out-Null

#获取虚拟机信息
$vm = Get-AzureRmVM | Select-Object Name,ResourceGroupName,Location,NetworkInterfaceIDs | Out-GridView -PassThru -Title "Select your VM"
$rgName = $vm.ResourceGroupName
$location = $vm.Location
$nic2 = ($vm.Name + "-nic2").ToLower()
$Subnet = Get-AzureRmVirtualNetwork | Get-AzureRmVirtualNetworkSubnetConfig | Select-Object Name,Id,AddressPrefix| Out-GridView -PassThru -Title "Select your New NIC's Subnet"

#解除分配
write-host "`n`tStop the Select VM!" -ForegroundColor Green
$stopvm = Stop-AzureRmVM -Name $vm.Name -ResourceGroupName $rgName

#添加新的NIC
write-host "`n`tAdd the Second NIC to VM!" -ForegroundColor Green
$nic2 = New-AzureRmNetworkInterface -ResourceGroupName $rgName -Name $nic2 -Location $location -SubnetId $Subnet.Id
$addnic = Get-AzureRmVM -Name $vm.Name -ResourceGroupName $rgName | Add-AzureRmVMNetworkInterface -Id $nic2.Id -Primary |Update-AzureRmVM

#切换主网卡
write-host "`n`tChange the Primary NIC!" -ForegroundColor Green
$myvm = Get-AzureRmVM -Name $vm.Name -ResourceGroupName $rgName
$myvm.NetworkProfile.NetworkInterfaces[0].Primary = $true
$myvm.NetworkProfile.NetworkInterfaces[1].Primary = $false
$changenic = Update-AzureRmVM -VM $myvm -ResourceGroupName $rgName

#启动虚拟机
write-host "`n`tStart the VM!" -ForegroundColor Green
$startvm = Get-AzureRmVM -Name $vm.Name -ResourceGroupName $rgName | Start-AzureRmVM
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息