1

I'm writing a PowerShell script that will allow our teams to rename network adapters on Windows Server Core. The script contains a function that will gather info on the network adapters and list them in a table with their DeviceID (Index) number, NetConnectionID (Name), and MAC Address. This is supposed to run and display the table for the user to see and they will be prompted to enter the Index number of the network adapter they want to rename.

The issue I am having is that even though I'm calling the function as the very first command in the script, nothing is displayed and then the prompt asking which Index number they want to select. However, if all I have in the script is the function and then call the function, it works fine.

Expected output...

Index # Name                         MAC Address
------- ----                         -----------
1       Local Area Connection        0X:XX:XX:XX:XX:X1
2       Local Area Connection 2      0X:XX:XX:XX:XX:X2
3       Ethernet 2                   0X:XX:XX:XX:XX:X3
...

Here is the code I'm working with. The script is not complete, but I don't want to continue on until I figure out this hurdle.

function List-NetAdapterNames {
    # Initialize Network Adapters array
    $naTable = @()
    
    # Query the active network adapters
    $existingNA = Get-CimInstance -Query "SELECT * FROM Win32_NetworkAdapter WHERE NOT ServiceName LIKE '%kdnic%'"
    
    # Create the Network Adapters table
    foreach ( $ena in $existingNA ) {
        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name "Index #" -Value $ena.DeviceID
        $obj | Add-Member -MemberType NoteProperty -Name "Name" -Value $ena.NetConnectionID
        $obj | Add-Member -MemberType NoteProperty -Name "MAC Address" -Value $ena.MACAddress
        $naTable += $obj
    }
    
    # Initialize Network Adapter index numbers index array
    $indexNo = @()
    
    # Add the Network Adapter index numbers to index array
    $naTable."Index #" | ForEach-Object { $indexNo += $_ }
    
    # Display Network Adapters table
    $naTable
}


do {
    List-NetAdapterNames
    
    # Ask user which adapter number they want to rename
    [int]$adapterNo = Read-Host "Which adapter to rename? [Index #]"

    # Ask if they want to continue
    $UsrConfirm = Read-Host "Do you want to continue? [Yes/No]"
} until ( $UsrConfirm -match '[Nn][Oo]?' )

When I run the script, I'm asked for the Index # without the network adapter table...

Which adapter to rename? [Index #]:

And if I enter anything (not complete, will have verification that the Index # exists), it asks for user confirmation. If I enter anything other than "No", it looks through the do/until loop and THIS time it displays the list of network adapters.

Which adapter to rename? [Index #]: 1
Do you want to continue? [Yes/No]: y
1       Local Area Connection        0X:XX:XX:XX:XX:X1
2       Local Area Connection 2      0X:XX:XX:XX:XX:X2
3       Ethernet 2                   0X:XX:XX:XX:XX:X3
...
sudosysadmin
  • 23
  • 1
  • 7
  • 1
    Long story short, pipe to `Out-Host`: `List-NetAdapterNames | Out-Host`. – Abraham Zinala Apr 22 '23 at 21:56
  • In short: This is a _display_ problem: Your output results in implicit `Format-Table` formatting, which incurs a 300-msec. delay before output is rendered (to determine suitable column widths). By contrast, the `Read-Host` prompt prints _right away_, and therefore _before_ your tabular output prints to the display.The - suboptimal - workaround is to force _synchronous_ display output of the tabular data, with `Format-Table` or `Out-Host`. See the linked duplicates for details. – mklement0 Apr 23 '23 at 00:27

0 Answers0