[Azure]使用Powershell重新生成ARM虚拟机网卡
2017-03-14 19:57
441 查看
Azure ARM 的 Windows 虚拟机如果将网卡禁用会直接导致无法连接。
解决方法是将网卡的内网IP地址设置为静态地址,将地址的值修改为子网下的另外一个未使用的地址即可。保存设置后,这个操作会为虚拟机分配一个新的网卡。
除了上面简单的方法外,还可以使用下面的脚本进行网卡重置,脚本本身相对于上面的操作要复杂的多,之所以写出来,主要是方便更为深入的理解 ARM 模式下各个功能组件互相之间的关联关系。
脚本如下:
脚本运行示例:
PS C:\Users\DanielHX> &.\[ARM]rebuild_virtualmachine_networkinterface.ps1 -SubscriptionName XXXXXXXXXXX -ResourceGroupName XXXXXX -VMName XXXXXXXX
Collecting Virtual Machine Information...
Network interface information collected.
Network interface name: danserver2012r2629Renewed
Network interface resource group name: DanEastResourceGroup1
Public ip information collected.
Public ip name: DanServer2012R2ip956
Public ip resource group name: DanEastResourceGroup1
Vnet information collected.
Vnet name: DanEastVNET
Subnet name: Subnet-1
Vnet resource group name: DanEastResourceGroup1
Location: chinaeast
Renewing network interface...
Finished deassociate old network interface
Old private ipaddress copied
Creating a new network interface
Finished new network interface creation
Finished renewing network interface
Deleting old network interface
Old network interface deleted
Reset private ip address
Succeeded
PS C:\Users\DanielHX>
脚本运行后,虚拟机网卡会重新生成,原公网IP和内网IP不会发生变化。
解决方法是将网卡的内网IP地址设置为静态地址,将地址的值修改为子网下的另外一个未使用的地址即可。保存设置后,这个操作会为虚拟机分配一个新的网卡。
除了上面简单的方法外,还可以使用下面的脚本进行网卡重置,脚本本身相对于上面的操作要复杂的多,之所以写出来,主要是方便更为深入的理解 ARM 模式下各个功能组件互相之间的关联关系。
脚本如下:
param( #The name of the subscription to take all the operations within. [Parameter(Mandatory = $true)] [string]$SubscriptionName, # Resource Group Name [Parameter(Mandatory = $true)] [string]$ResourceGroupName, # Virtual Machine Name [Parameter(Mandatory = $true)] [string]$VMName ) Function GetResourceNameFromResourceId($resourceId) { return $resourceId.Substring($resourceId.LastIndexOf('/')+1); } Function GetResourcePropertyFromResourceId($resourceId, $propertyName) { $propertyName = $propertyName + "/"; $rgName = $resourceId.Substring($resourceId.IndexOf($propertyName)+$propertyName.Length); return $rgName.Substring(0, $rgName.IndexOf("/")); } Function RebuildVirtualMachineNetworkInterface() { Write-Host "Collecting Virtual Machine Information..."; $vm = Get-AzureRmVM -ResourceGroupName $ResourceGroupName -Name $VMName; $oldNicId = $vm.NetworkProfile.NetworkInterfaces[0].Id; if ($oldNicId -eq $null) { Write-Host "Failed to get network interface of the virtual machine."; return; } $oldNicName = GetResourceNameFromResourceId $oldNicId; $oldNicResourceGroup = GetResourcePropertyFromResourceId $oldNicId "resourceGroups"; $oldNic = Get-AzureRmNetworkInterface -Name $oldNicName -ResourceGroupName $oldNicResourceGroup; Write-Host "Network interface information collected."; Write-Host "Network interface name:" $oldNicName; Write-Host "Network interface resource group name:" $oldNicResourceGroup; $oldPublicIPId = $oldNic.IpConfigurations[0].PublicIpAddress.Id; if ($oldPublicIPId -eq $null) { Write-Host "Failed to get public ip of the virtual machine."; } $oldPublicIPName = GetResourceNameFromResourceId $oldPublicIPId; $oldPublicIPResourceGroup = GetResourcePropertyFromResourceId $oldPublicIPId "resourceGroups"; $oldPublicIP = Get-AzureRmPublicIpAddress -Name $oldPublicIPName -ResourceGroupName $oldPublicIPResourceGroup; Write-Host "Public ip information collected."; Write-Host "Public ip name:" $oldPublicIPName; Write-Host "Public ip resource group name:" $oldPublicIPResourceGroup; if ($oldPublicIP.PublicIpAllocationMethod -eq "Dynamic") { $oldPublicIP.PublicIpAllocationMethod = "Static"; [void]($oldPublicIP | Set-AzureRmPublicIpAddress); Write-Host "Successfully set current public ip address to 'Static'"; } $subnetId = $oldNic.IpConfigurations[0].Subnet.Id; if ($subnetId -eq $null) { Write-Host "Failed to get vnet information of the virtual machine."; } $subnetName = GetResourceNameFromResourceId $subnetId; $vnetName = GetResourcePropertyFromResourceId $subnetId "virtualNetworks"; $vnetResourceGroup = GetResourcePropertyFromResourceId $subnetId "resourceGroups"; $vnet = Get-AzureRmVirtualNetwork -Name $vnetName -ResourceGroupName $vnetResourceGroup; $subnet = Get-AzureRmVirtualNetworkSubnetConfig -Name $subnetName -VirtualNetwork $vnet; $location = $vnet.Location; Write-Host "Vnet information collected."; Write-Host "Vnet name:" $vnetName; Write-Host "Subnet name:" $subnetName; Write-Host "Vnet resource group name:" $vnetResourceGroup; Write-Host "Location:" $location; Write-Host "Renewing network interface..."; $newPublicIP = New-AzureRmPublicIpAddress -Name ($oldPublicIP.Name + "Renewed") -ResourceGroupName $vnetResourceGroup -Location $location -AllocationMethod Dynamic; $oldNic.IpConfigurations[0].PublicIpAddress = $newPublicIP; [void]($oldNic | Set-AzureRmNetworkInterface); Write-Host "Finished deassociate old network interface"; $oldPrivateIP = $oldNic.IpConfigurations[0].PrivateIpAddress; Write-Host "Old private ipaddress copied"; Write-Host "Creating a new network interface"; $newNic = $null; if ($oldNic.NetworkSecurityGroup -eq $null) { $newNic = New-AzureRmNetworkInterface -Name ($oldNic.Name + "Renewed") -ResourceGroupName $oldNicResourceGroup -Location $location -SubnetId $subnetId -PublicIpAddressId $oldPublicIP.Id; } else { $nicNsg = $oldNic.NetworkSecurityGroup; $newNic = New-AzureRmNetworkInterface -Name ($oldNic.Name + "Renewed") -ResourceGroupName $oldNicResourceGroup -Location $location -SubnetId $subnetId -PublicIpAddressId $oldPublicIP.Id -NetworkSecurityGroupId $nicNsg.Id; } Write-Host "Finished new network interface creation"; [void](Remove-AzureRmVMNetworkInterface -VM $vm -NetworkInterfaceIDs $vm.NetworkProfile.NetworkInterfaces[0].Id); [void](Add-AzureRmVMNetworkInterface -VM $vm -Id $newNic.Id -Primary); [void](Update-AzureRmVM -VM $vm -ResourceGroupName $ResourceGroupName); Write-Host "Finished renewing network interface"; Write-Host "Deleting old network interface"; # Remove old NIC [void](Remove-AzureRmNetworkInterface -Name $oldNic.Name -ResourceGroupName $oldNicResourceGroup -Force); [void](Remove-AzureRmPublicIpAddress -Name $newPublicIP.Name -ResourceGroupName $newPublicIP.ResourceGroupName -Force); Write-Host "Old network interface deleted"; Write-Host "Reset private ip address"; $newNic = Get-AzureRmNetworkInterface -Name $newNic.Name -ResourceGroupName $newNic.ResourceGroupName; $newNic.IpConfigurations[0].PrivateIpAddress = $oldPrivateIP; $newNic.IpConfigurations[0].PrivateIpAllocationMethod = "Static"; [void]($newNic | Set-AzureRmNetworkInterface); Write-Host "Succeeded"; } [void](Select-AzureRmSubscription -SubscriptionName $SubscriptionName); RebuildVirtualMachineNetworkInterface;
脚本运行示例:
PS C:\Users\DanielHX> &.\[ARM]rebuild_virtualmachine_networkinterface.ps1 -SubscriptionName XXXXXXXXXXX -ResourceGroupName XXXXXX -VMName XXXXXXXX
Collecting Virtual Machine Information...
Network interface information collected.
Network interface name: danserver2012r2629Renewed
Network interface resource group name: DanEastResourceGroup1
Public ip information collected.
Public ip name: DanServer2012R2ip956
Public ip resource group name: DanEastResourceGroup1
Vnet information collected.
Vnet name: DanEastVNET
Subnet name: Subnet-1
Vnet resource group name: DanEastResourceGroup1
Location: chinaeast
Renewing network interface...
Finished deassociate old network interface
Old private ipaddress copied
Creating a new network interface
Finished new network interface creation
Finished renewing network interface
Deleting old network interface
Old network interface deleted
Reset private ip address
Succeeded
PS C:\Users\DanielHX>
脚本运行后,虚拟机网卡会重新生成,原公网IP和内网IP不会发生变化。
相关文章推荐
- [Azure]使用Powershell克隆ARM虚拟机(托管磁盘)
- [Azure]使用Powershell调整ARM虚拟机的系统磁盘和数据磁盘大小
- [Azure]使用Powershell获取ARM模式下可用的虚拟机平台映像信息
- [Azure]使用Powershell导出所有订阅下的ARM虚拟机的信息
- [Azure]使用Powershell输出某台ARM虚拟机的NSG
- [Azure]使用Powershell克隆ARM虚拟机(非托管磁盘)
- [Azure]使用Azure Powershell查看ARM模式下Azure存储中的vhd文件对应的虚拟机
- [Azure]使用Powershell将ARM虚拟机(托管磁盘)加入可用性集
- Azure Powershell使用已有特殊化非托管磁盘创建ARM虚拟机
- [Azure]使用Powershell将ARM虚拟机加入可用性集
- [Azure]使用Azure Powershell重新部署ASM虚拟机
- 使用Json Template在Azure China创建ARM类型的虚拟机
- [Azure]使用Powershell输出某台ASM虚拟机的NSG和ACL
- [Azure]使用Powershell获取ASM和ARM存储的实际使用量
- [Azure]使用Powershell删除ARM模式下单台虚拟机及相关资源(托管磁盘)
- [Azure]使用Powershell删除ARM模式下单台虚拟机及相关资源(非托管磁盘)
- [Azure]使用Azure Powershell输出ARM模式下某个账号中所有订阅下的虚拟网络拓扑
- [Azure]使用Powershell输出Azure订阅下所有虚拟机的ACL信息
- Azure China (8) 使用Azure PowerShell创建虚拟机,并设置固定Virtual IP Address和Private IP
- [Azure]使用Powershell获取Azure ARM模式订阅下的一些常用信息