【发布时间】:2021-11-09 13:04:24
【问题描述】:
我现在正在编写一个 powershell 脚本,它应该会在 Windows 11 上自动配置 USB 到以太网适配器。有时适配器已经配置了 IPv6 地址,有时必须添加地址。
我现在正在努力区分这两种情况,以便在我尝试添加新的 IPv6 地址时不会出错。
if (<No IPv6 address is configured for this interface alias>) {
New-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
} else {
Set-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
}
if 子句中的括号中我需要什么代码来区分这两种情况。
【问题讨论】:
-
试试:
if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...} -
@guiwhatsthat:非常感谢您的快速回复。它就像魅力一样。
标签: powershell networking windows-11