Hello team, I am stuck with this script and just need some assistance completing it. I am trying to query computers that have more than one physical NIC and then dump it out to a CSV file. I have the dump working for the NICs, I just need help tying it to a list of computers so the CSV will have a column for computer names then matched to the corresponding columns. This is what I have so far:

$export= 'C:\TEMP\temp.csv'
$nics = get-netadapter -physical

foreach ($nic in $nics){ 
    $nicname = @{name = 'Name'; expression = {$_.name}}
    $nicinterface = @{name = 'Interface'; expression = {$_.interfacedescription}}
    $nicstatus = @{name = 'Status'; expression = {$_.status}}
    $nicmac = @{name = 'MAC'; expression = {$_.macaddress}}
    $nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
    $nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed
    $nicobjects | export-csv -append -path $export -notypeinformation
}
2 Spice ups

please look into custom objects!

if you have WinRM setup, you can invoke the command on the remote machine to get the info.

Or you can use WIM /CIM

 get-ciminstance win32_networkadapter -computername $remoteMAchine

give it a try

1 Spice up

Here is the documentation for PSCustomObjects.

1 Spice up

Thanks, but what I posted already gives me the adapter information. My challenge is applying that to a list of computers then dumping it to a csv. Forgive me if I am at a lost with your suggestion

it only gives you the adapter information for your LOCAL machine, not for remote machines.

you have to invoke the command on a remote machine or start a CIM session to get the same info.

What exactly are you lost with?

Correct I can do invoke-command -computer “computernamehere” get-ciminstance win32_networkadapter . My issue, whether with your suggestion or what I posted script-wise, is combining it to do the same for a list of computers then piping it out to a csv.

e.g.

you probably want to add error handling (e.g. if winrm does not work, or the computer is unreachable etc.)

But this is the idea.

# my csv in this sample has a 'computername' header
$export = import-csv 'C:\TEMP\temp.csv'

$nicReport = 
foreach ($remoteMachine in $export.computername) {
    try {
        $nicInfo =
        Invoke-Command -ComputerName $remoteMachine -ScriptBlock {
            Get-NetAdapter -Physical
        } -ErrorAction Stop

        foreach ($nic in $nicInfo) {
            [pscustomobject]@{
                ComputerName  = $remoteMachine
                NIC_Name      = $nic.name
                NIC_Interface = $nic.interfacedescription
                NIC_Status    = $nic.status
                NIC_Mac       = $nic.macaddress
                NIC_Speed     = $nic.linkspeed
            }
        }
    }
    catch {
        [pscustomobject]@{
            ComputerName  = $remoteMachine
            NIC_Name      = "-"
            NIC_Interface = "-"
            NIC_Status    = $error[0].exception.message
            NIC_Mac       = "-"
            NIC_Speed     = "-"
        }
    }
}

$nicreport |
export-csv "C:\temp\NIC_Report.csv" -NoTypeInformation
1 Spice up

Instead of this:

$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed

how about

$nicspeed = @{name = 'Speed'; expression = {$_.linkspeed}}
$Comp - @{Name='ComputerName'; expression= $env:computername}
$nicobjects = $nic | select-object $nicname, $nicinterface, $nicstatus, $nicmac, $nicspeed

Perhaps you could try incorporating an if statement, have it output to one csv for a single NIC, and a different one if there are more than one.

$nics = Get-NetAdapter -Physical

if (($nics.Name).count -gt 1){
Write-Output "More than one"
}
Else
{
Write-output "Only 1"
}
1 Spice up

Leave the ‘export=…’ line where it is, then wrap all the rest in a ForEach loop going over all the PCs.

ForEach ($PC in {wherever you get the list, just the names}){
     # Do what you're already doing
     # $PC is the name of the PC you're doing it to
}  # End, For Each PC in the list

On a performance note, if you have a lot of these, the ForEach piping to Export-CSV -Append will eat a lot of time per each PC (file-open, write data, file close). It would be faster to store the results in a variable for the run, then one Export-CSV at the end.

$Result = ForEach ($PC in (Get-ADComputer -Properties whatever -Filter something){
      # As before.  This will be MUCH easier with a 
      # PSCustomObject.
}
$Result | Export-CSV "filename" -etc.

1 Spice up

This is good advice. The Get-ADComputer query should be moved before the loop and stored in a variable. If it is inside the loop condition, the query will run repeatedly.

$Computers = Get-ADComputer -Properties whatever -Filter 'something -eq "something"'
$Result = ForEach ($PC in $Computers){
      # As before.  This will be MUCH easier with a 
      # PSCustomObject.
}
$Result | Export-CSV "filename" -etc.