Powershell wont output infomation

$ErrorActionPreference = ‘SilentlyContinue’

$ComputerName =Get-ADComputer -Filter {(Name -like “*”)} -SearchBase “OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM” | Select-Object -ExpandProperty Name

$results = @{}

ForEach ($computer in $ComputerName) {

$Results += Get-NetAdapter -CimSession $ComputerName | Select-Object PsComputerName, InterfaceAlias, Status, MacAddress


$results | Export-csv -path C\users\bret.hooker\desktop\macaddress.csv -Append

Please note the base and filter are just examples and not the actual code due to work place confidentiality. Code currently will pull from AD all computer name, then will run the ForEach command to get the NetAdapter Information. I am unable to get it to output to the CSV file however. Any advice would be great.

Yeah, so, there’s an important pattern in PowerShell that you’re not following, which is making things harder on you. See https://devops-collective-inc.gitbook.io/the-big-book-of-powershell-gotchas/accumulating-output-in-a-function. You’re also not using -CimSession correctly.

Function Get-NetAdapterInfo {
 Get-ADComputer -Filter * -SearchBase $SearchBase |
 Select -Expand Name |
 ForEach-Object {
  $session = New-CimSession -ComputerName $_
  Get-NetAdapter -CimSession $session | 
  Select-Object PsComputerName, InterfaceAlias, Status, MacAddress


Get-NetAdapterInfo -searchbase "ou=whatever,dc=domain,dc=com" | Export-CSV Whatever.csv

The idea is to NOT accumulate output into an array. That’s contrary to how PowerShell’s pipeline model works. So, I’ve written a function that simply lets the output into the pipeline one object at a time. I can then run that function and pipe its output to Export-CSV or whatever I like.

I suspect you’re not getting anything as-is because you’re not using -CimSession correctly. It doesn’t accept a computer name; it accepts a CIM Session.

in your code, what items do i need to change as i do not currently understand what you have written. I attempted to replace[String] in the $searchBase and now it just declares missing function perimeter list

You don’t need to change anything. That’s the delight of parameters.

Get-NetAdapterInfom -SearchBase “OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM” | Export-CSV Whatever.csv

when i run the powershell it requests values for searchbase

From what I recall, doesn’t New-CimSession take an array of computernames as input? It should be possible to create session(s) for all machines at once, then feed that to Get-NetAdapter to do everything at the same time, rather than introducing unnecessary overhead with a loop.

Yeah, you totally could.

If it is prompting you for a value, then give it the value it is prompting you for. That’s why it’s prompting you.


 $SearchBase = "OU=AsiaPacific,OU=Sales,OU=UserAccounts,DC=FABRIKAM,DC=COM"

 $sessions = New-CimSession -ComputerName (
 Get-ADComputer -Filter * -SearchBase $SearchBase |
 Select -Expand Name)

 Get-NetAdapter -CimSession $sessions | 
 Select-Object PsComputerName, InterfaceAlias, Status, MacAddress |
 Export-CSV whatever.csv

Certainly valid. It’d be easier to add error-handling to it the way I originally had it, since you could trap any failed sessions individually and log them, but this also works if nothing goes wrong with the session creation.