0

我现在正在编写一个 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 子句的括号中使用什么代码。

4

1 回答 1

1

要结束这个问题,答案是:

if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...}
于 2021-09-14T12:11:12.210 回答