更新ARM虛擬機網卡名稱使其符合命名規則

更新ARM虛擬機網卡名稱使其符合命名規則

通過Azure Portal創建過虛擬機的同學都會注意到一個問題:其他資源命名都可以自定義,唯獨網絡接口的命名會默認帶3位隨機數,若企業對命名規則有要求的情況下,這顯然是不可以的。通過下面的PowerShell腳本我們可以對每個虛擬機進行網絡接口替換,以達到目的。
單網卡已測試通過。

使用下面PowerShell腳本可以將默認網絡接口替換成名稱爲“虛擬機名-nic”的:


#------------------------------------------------------------------------------    
# User own the risk, otherwise exit.
# 
# Azure PowerShell Version:  3.6.0
#
# Create by Zeno. 
#------------------------------------------------------------------------------  

"------------------------------------------------------------------------------ " | Write-Host -ForegroundColor Yellow 
""  | Write-Host -ForegroundColor Yellow 
"`t腳本說明: " | Write-Host -ForegroundColor Yellow 
""  | Write-Host -ForegroundColor Yellow 
"`t1.本腳本默認公共IP、網絡接口、虛擬機均在同一個資源組內 " | Write-Host -ForegroundColor Yellow 
"`t2.本腳本默認名稱規則爲  公共IP:虛擬機名-ip  網絡接口:虛擬機名-nic " | Write-Host -ForegroundColor Yellow 
"`t3.如果公共IP名稱相同,請在腳本運行時選擇覆蓋創建" | Write-Host -ForegroundColor Yellow 
"`t4.本腳本會改變虛擬機公共IP地址/私有IP地址,請謹慎使用" | Write-Host -ForegroundColor Yellow 
"------------------------------------------------------------------------------ " | Write-Host -ForegroundColor Yellow 

#登錄訂閱
#Login-AzureRmAccount -EnvironmentName AzureChinaCloud -Credential $(Get-Credential -UserName [email protected] -Message Login_AzureChinaCloud) |Out-Null

#定義參數
$vm = Get-AzureRmVM | Select-Object Name,ResourceGroupName,NetworkInterfaceIDs,Location | Out-GridView -PassThru -Title "Select your VM" 
#$VNet = Get-AzureRmVirtualNetwork | Select Name,ResourceGroupName | Out-GridView -PassThru -Title "Select your VNet"
$Subnet = Get-AzureRmVirtualNetwork | Get-AzureRmVirtualNetworkSubnetConfig | Select-Object Name,Id,AddressPrefix| Out-GridView -PassThru -Title "Select your Subnet"
$vmName = $vm.Name
$NewNIC = ("$vmName" + "-nic").ToLower()
$NewpublicIp = "$vmName" + "-ip"

#獲取虛擬機對象
$myvm = Get-AzureRmVM -ResourceGroupName $vm.ResourceGroupName -Name $vm.Name

#取消網絡接口關聯
$nic = Get-AzureRmNetworkInterface -ResourceGroupName $vm.ResourceGroupName -Name $myvm.NetworkProfile.NetworkInterfaces.Id.Split("/")[-1]
$nic.IpConfigurations.PublicIpAddress.Id = $null
Set-AzureRmNetworkInterface -NetworkInterface $nic | Out-Null

#創建公共IP
write-host "`n`tCreate Public Ip: $NewpublicIp!" -ForegroundColor Green
$myPublicIp = New-AzureRmPublicIpAddress -Name $NewpublicIp -ResourceGroupName $vm.ResourceGroupName -Location $vm.Location -AllocationMethod Dynamic

#創建新的網絡接口
write-host "`n`tCreate Newwork Interface: $NewNIC!" -ForegroundColor Green
$myNIC = New-AzureRmNetworkInterface -Name $NewNIC -ResourceGroupName $vm.ResourceGroupName -Location $vm.Location -SubnetId $Subnet.Id -PublicIpAddressId $myPublicIp.Id

#刪除默認網絡接口
$removenic = Remove-AzureRmVMNetworkInterface -VM $myvm -NetworkInterfaceIDs $vm.NetworkInterfaceIDs[0]
 
#添加新的網絡接口
write-host "`n`tReplace Newwork Interface to: $NewNIC!" -ForegroundColor Green
Add-AzureRmVMNetworkInterface -VM $myvm -Id $myNIC.Id –Primary | Update-AzureRmVM



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章