[Azure]使用Powershell重新生成ARM虚拟机网卡

60 篇文章 0 订阅
59 篇文章 0 订阅

Azure ARM 的 Windows 虚拟机如果将网卡禁用会直接导致无法连接。

解决方法是将网卡的内网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不会发生变化。




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值