Enumerate list of network adapters and assign variable to each

Hi there I’m in the process of creating an interactive script to create virtual machines for my techs.

I would like to enumerate all network adapters and then assign a variable to each. I’m running into problems programmatically doing this, since the number of network adapters on each workstation might change. I’m not quite certain how to assign a variable dynamically based on the returned data. Eventually these variables will be loaded into an array witch will utilize the .NET method $host.ui.PromptForChoice()

$NicAdapters = Get-CimInstance -Class Win32_NetworkAdapter -Property NetConnectionID,NetConnectionStatus | Where-Object { $_.NetConnectionStatus -eq 2 } | Select-Object -Property NetConnectionID -ExpandProperty NetConnectionID

ForEach ($nic in $NicAdapters){

}

In 99.99% of the cases I know that’s unnecessary. :wink:

If your script is interactive anyway you could use the cmdlet

to prompt your techs and with the -OutputMode set to Single they can only choose one of the displayed NICs.
Another option would be to add an index to the list of NICs show this list and let them choose the number.

Regardless of that - when you post code, error messages, console output or sample data please fomrat it as code using the “preformatted text” button ( </> ).

Thanks in advance.

1 Like