0

I'm right now writing a powershell script, that should automatically configure an USB to Ethernet adapter on Windows 11. Sometimes the adapter has already a configured IPv6 adress and sometimes the address must be added.

I'm now struggling in how to distinguish between the two cases, so that there will be no error in case I'm trying to add a new IPv6 address, which is already there.

if (<No IPv6 address is configured for this interface alias>) {
    New-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
} else {
    Set-NetIPAddress –InterfaceAlias $myAdapter –IPAddress $myAddress
}

What code do I need in the brackets in the if clause in order to distinguish between the two cases.

2
  • 1
    try: if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...} Commented Sep 14, 2021 at 11:09
  • @guiwhatsthat: Many thanks for fast reply. It works like charm. Commented Sep 14, 2021 at 11:37

1 Answer 1

2

to close the question the answer was:

if ($null -eq (Get-NetIPAddress -InterfaceAlias $myAdapter -ErrorAction SilentlyContinue)) {...}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.