如何通过poweshell获取,删除和更新Azure的nic dhcp?



定义用户:

$userName = " xxxx"

设置密码:

$Pwd = ConvertTo-SecureString "xxxxx" -AsPlainText -Force

设置凭据:

$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $Pwd
Connect-AzAccount -Credential $creds
Get-AzSubscription
Set-AzContext -Subscription  "xxxxxxxxxx"
Get-AzVM 
Get-AzNetworkInterface -ResourceGroupName rba-test-rg

可以使用以下脚本列出特定 VM 下的网络接口,并将其删除或更新。您可以将{nic needs to be deleted}{nic needs to be updated}替换为您自己的条件语句:

$vm = Get-AzVM -ResourceGroupName {resource group name} -Name {VM name}  
$nics = $vm.NetworkProfile.NetworkInterfaces
foreach($nic in $nics){
$aznics = Get-AzNetworkInterface -ResourceId $nic.id
if({nic needs to be deleted}){
$aznics | Remove-AzNetworkInterface
}
if({nic needs to be updated}){
$aznics.DnsSettings.DnsServers.Add("192.168.1.100");
$aznics | Set-AzNetworkInterface
}
}

最新更新