## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Add the DNS servers to the configuration. ##
$nic.DnsSettings.DnsServers.Add("192.168.1.100")
## Add a secondary DNS server if needed, otherwise set the configuration. ##
$nic.DnsSettings.DnsServers.Add("192.168.1.101")
## Apply the new configuration to the network interface. ##
$nic | Set-AzNetworkInterface
若要删除 DNS 服务器并将设置更改为从虚拟网络继承,请使用以下命令。 将 DNS 服务器 IP 地址替换为自定义 IP 地址。
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Add the DNS servers to the configuration. ##
$nic.DnsSettings.DnsServers.Remove("192.168.1.100")
## Add a secondary DNS server if needed, otherwise set the configuration. ##
$nic.DnsSettings.DnsServers.Remove("192.168.1.101")
## Apply the new configuration to the network interface. ##
$nic | Set-AzNetworkInterface
启用/禁用 IP 转发
使用 IP 转发,附加到 VM 的 NIC 可以:
接收网络流量,但这些流量不是发往在任何 NIC 的 IP 配置中分配的任何 IP 地址。
使用源 IP 地址发送网络流量,但该地址与在任何 NIC 的 IP 配置中分配的地址不同。
必须启用 IP 转发,以便附加到 VM 的每个 NIC 能够转发流量。 不管 VM 上附加了一个还是多个 NIC,该 VM 都可转发流量。
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Set the IP forwarding setting to enabled. ##
$nic.EnableIPForwarding = 1
## Apply the new configuration to the network interface. ##
$nic | Set-AzNetworkInterface
若要禁用 IP 转发,请使用以下命令:
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Set the IP forwarding setting to disabled. ##
$nic.EnableIPForwarding = 0
## Apply the new configuration to the network interface. ##
$nic | Set-AzNetworkInterface
## Place the virtual network into a variable. ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Change the subnet in the IP configuration. Replace the subnet number with number of your subnet in your VNet. Your first listed subnet in your VNet is 0, next is 1, and so on. ##
$IP = @{
Name = 'ipv4config'
Subnet = $vnet.Subnets[1]
}
$nic | Set-AzNetworkInterfaceIpConfig @IP
## Apply the new configuration to the network interface. ##
$nic | Set-AzNetworkInterface
添加到应用程序安全组或从中删除
只能将 NIC 添加到与 NIC 位于同一虚拟网络和位置的应用程序安全组。
仅在将 NIC 附加到 VM 后,才能使用门户添加或移除应用程序安全组的 NIC。 否则,请使用 PowerShell 或 Azure CLI。 有关详细信息,请参阅应用程序安全组以及如何创建应用程序安全组。
## Place the virtual network into a variable. ##
$net = @{
Name = 'myVNet'
ResourceGroupName = 'myResourceGroup'
}
$vnet = Get-AzVirtualNetwork @net
## Place the subnet configuration into a variable. ##
$subnet = Get-AzVirtualNetworkSubnetConfig -Name mySubnet -VirtualNetwork $vnet
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Place the application security group configuration into a variable. ##
$asg = Get-AzApplicationSecurityGroup -Name myASG -ResourceGroupName myResourceGroup
## Add the application security group to the IP configuration. ##
$IP = @{
Name = 'ipv4config'
Subnet = $subnet
ApplicationSecurityGroup = $asg
}
$nic | Set-AzNetworkInterfaceIpConfig @IP
## Save the configuration to the network interface. ##
$nic | Set-AzNetworkInterface
## Place the network interface configuration into a variable. ##
$nic = Get-AzNetworkInterface -Name myNIC -ResourceGroupName myResourceGroup
## Place the network security group configuration into a variable. ##
$nsg = Get-AzNetworkSecurityGroup -Name myNSG -ResourceGroupName myResourceGroup
## Add the NSG to the NIC configuration. ##
$nic.NetworkSecurityGroup = $nsg
## Save the configuration to the network interface. ##
$nic | Set-AzNetworkInterface
删除网络接口
如果 NIC 未附加到 VM,则可以删除该 NIC。 如果 NIC 已附加到 VM,则必须首先停止并解除分配 VM,然后拆离 NIC。
要从 VM 拆离 NIC,请完成从 VM 中移除网络接口中的步骤。 VM 必须始终至少附加有一个 NIC,因此无法从 VM 中删除唯一的 NIC。