Author Posts

July 23, 2018 at 3:35 pm

$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.

July 23, 2018 at 3:43 pm

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 {
 [CmdletBinding()]
 Param(
  [Parameter(Mandatory=$True)]
  [string]$SearchBase
 )
 
 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.

July 23, 2018 at 4:00 pm

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

July 23, 2018 at 4:01 pm

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

July 23, 2018 at 4:11 pm

when i run the powershell it requests values for searchbase

July 23, 2018 at 7:22 pm

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.

July 23, 2018 at 7:23 pm

Yeah, you totally could.

July 23, 2018 at 7:24 pm

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.

July 23, 2018 at 7:26 pm

So...

 $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.