none
Retrieving remote NIC name on multiple machines RRS feed

  • Question

  • Hi guys,

    I need assistance in finishing my script. The script is to read a list of servers from a text file, get the nic name and display the results into another text file. I want the result to display (servername, nicname). Please view and tell me what I'm missing.

    When I run it now, it output result but it says "Ethernet" but the server I have it looking up nic name is something else

    $IPaddress = Get-Content "C:\Scripts\nicservers.txt"

    ForEach ($servername in $IPaddress) {
        $nicid = get-wmiobject win32_networkadapter | select-object -expandproperty netconnectionid
        
        $nicid | Out-File "C:\Scripts\nicnameresult.txt"

        }

    Any assistance would be appreciated


    • Edited by Winfanity Tuesday, January 23, 2018 9:51 PM
    Tuesday, January 23, 2018 9:45 PM

Answers

  • Your output statement is inside your loop, so you are overwriting the output file over and over again.

    Your output statement needs to be outside of the loop.

    Example:


    $computerNames | ForEach-Object {
      $computerName = $_
      Get-WmiObject Win32_NetworkAdapter -ComputerName $computerName |
        Where-Object { $_.NetConnectionID } | Select-Object `
        @{Name = "ComputerName";    Expression = {$computerName}},
        @{Name = "NetConnectionID"; Expression = {$_.NetConnectionID}}
    } | Export-Csv C:\Scripts\results.csv -NoTypeInformation



    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 23, 2018 10:27 PM
    Moderator

All replies

  • You are getting Win32_NetworkAdapter on the local machine over and over again.

    (You forgot to specify -ComputerName parameter for Get-WmiObject.)


    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 23, 2018 10:01 PM
    Moderator
  • Bill thanks for pointing that out. I somehow missed including it.

    Now it picks up the right name when I have one server in the text file, but testing with two server names only output the last server nic name in the output text file. 

    I would like the output file to also first display the server name then the nic name



    • Edited by Winfanity Tuesday, January 23, 2018 10:08 PM
    Tuesday, January 23, 2018 10:06 PM
  • Your output statement is inside your loop, so you are overwriting the output file over and over again.

    Your output statement needs to be outside of the loop.

    Example:


    $computerNames | ForEach-Object {
      $computerName = $_
      Get-WmiObject Win32_NetworkAdapter -ComputerName $computerName |
        Where-Object { $_.NetConnectionID } | Select-Object `
        @{Name = "ComputerName";    Expression = {$computerName}},
        @{Name = "NetConnectionID"; Expression = {$_.NetConnectionID}}
    } | Export-Csv C:\Scripts\results.csv -NoTypeInformation



    -- Bill Stewart [Bill_Stewart]

    Tuesday, January 23, 2018 10:27 PM
    Moderator
  • Bill thank you!!!
    Tuesday, January 30, 2018 8:07 PM