【问题标题】:How to check with Powershell script if an IP Address is already configured如果 IP 地址已配置,如何使用 Powershell 脚本检查
【发布时间】: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


【解决方案1】:

结束问题的答案是:

if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-12
    • 1970-01-01
    • 2022-03-03
    • 2016-08-28
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多