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

为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


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值