VMware Cloud Community
kraney
Contributor
Contributor

Get Nic Status

Hello,

I have two nics (wan/lan) and I would like for them not to be on a the same time.

I can disable the existing nic by use the Set-NetworkAdapter cmdlet, but I'm not sure how I should get the stauts of the existing nic.

My psuedo code should look something like.

If WAN_NIC:Connected = True {

     Get-VM Server01 | Get-NetworkAdapter | ?{_.NetworkName -eq "LAN NIC"}  | Set-NetworkAdapter -Connected|$false -Confirm:$false}

If LAN_NIC:Connected = True {

     Get-VM Server01 | Get-NetworkAdapter | ?{_.NetworkName -eq "WAN NIC"}  | Set-NetworkAdapter -Connected|$false -Confirm:$false}

Now my question is how do I get the connection status of the nic?   Get-NetworkAdapter does not appear to show this status.

Also, any ideas on how I can enforce this?  I could run it on a scheduled task, but I'd prefer to have it event driven.  I'm not sure the best way to do this, short of a c# type application.

Thanks

-Kevin

0 Kudos
1 Reply
LucD
Leadership
Leadership

You can check if a NIC is connected like this

$lanNIC = "Network adapter 1"

$wanNIC = "Network adapter 2"

$vm = Get-VM Server01

if(($_.NetworkAdapters | where {$_.Name -eq $wanNIC}).ConnectionState.Connected}{

...

}

The code first finds the correct NIC and then checks if it is connected.

I'm not sure I understand what you mean with "event driven".

Which event do you want to trigger this script ?

The fact that a NIC is connected ?

Wouldn't that be causing a flip-flop of connecting-disconnecting the NICs in sequence ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos