Julian La Lau
Julian La Lau

Reputation: 15

Get Hostname and MAC address from all PCs in AD

I'm trying to get the hostname and the MAC address from all PCs in the Active Directory. I know that MAC addresses are not in the Activce Directory. That's why I already used a small script from someone else. The point is that I have to make a list of hostnames, which I can do, but then the other script runs into a problem because some computers are not online.

Can anyone help me get a list with only the pc's that are online?

This is the part that searches the list I create with hostnames.

$Computers = Import-CSV C:\Users\admin_stagiair\Desktop\Computers.txt
$result = @()
foreach ($c in $Computers){
    $nic = Invoke-Command {
        Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"'
    } -ComputerName $c.Name
    $x = New-Object System.Object | select ComputerName, MAC
    $x.Computername = $c.Name
    $x.Mac = $Nic.MACAddress
    $result += $x
}
$result | Export-Csv C:\Users\admin_stagiair\Desktop\Computers.csv -Delimiter ";" -NoTypeInformation

And this is the part that I tried to make search the list and filter out the online computers, which absolutely does not work and I can't figure out how to do it.

$Computers = Import-Csv C:\Users\admin_stagiair\Desktop\Computers.txt
foreach ($c in $Computers) {
    $ping = Test-Connection -Quiet -Count 1
    if ($ping) {
        $c >> (Import-Csv -Delimiter "C:\Users\admin_stagiair\Desktop\online.txt")
    } else {
        "Offline"
    }
}

Last bit, this is the part I use to create a list of all computers in the Active Directory.

Get-ADComputer -Filter {enabled -eq $true} -Properties * |
    select Name > C:\Users\(user)\Desktop\Computers.txt

Upvotes: 0

Views: 13801

Answers (2)

user6811411
user6811411

Reputation:

  • If you only want one property from Get-ADComputer don't fetch all
  • a computer could have more than one MAC, to avoid an array be returned join them.
  • $result += inefficiently rebuilds the array each time, use a PSCustomObject instead.

Try this (untested):
EDIT: first test connection, get MAC only when online

## Q:\Test\2018\09\18\SO_52381514.ps1

$Computers = (Get-ADComputer -Filter {enabled -eq $true} -Property Name).Name

$result = ForEach ($Computer in $Computers){
    If (Test-Connection -Quiet -Count 1 -Computer $Computer){
        [PSCustomPbject]@{
            ComputerName = $Computer
            MAC = (Invoke-Command {
                     (Get-WmiObject Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"').MACAddress -Join ', '
                  } -ComputerName $Computer)
            Online = $True
            DateTime = [DateTime]::Now
        }
    } Else {
        [PSCustomPbject]@{
            ComputerName = $Computer
            MAC = ''
            Online = $False
            DateTime = [DateTime]::Now
        }
    }

}
$result | Export-Csv C:\Users\admin_stagiair\Desktop\Computers.csv -Delimiter ";" -NoTypeInformation

Upvotes: 2

The Fish
The Fish

Reputation: 1039

What about trying something like this:

# Get all computers in list that are online
$Computers = Import-Csv C:\Users\admin_stagiair\Desktop\Computers.txt | 
    Select-Object -ExpandProperty Name | 
    Where-Object {Test-Connection -ComputerName $_ -Count 1 -Quiet}

# Grab the ComputerName and MACAddress
$result = Get-WmiObject -ComputerName $computers -Class Win32_NetworkAdapterConfiguration -Filter 'ipenabled = "true"' |
    Select-Object -Property PSComputerName, MacAddress

$result | Export-Csv C:\Users\admin_stagiair\Desktop\Computers.csv -Delimiter ";" -NoTypeInformation

Upvotes: 1

Related Questions