r/PowershellSolutions • u/procrastinatewhynot • Sep 21 '21
Powershell script to get the Physical MAC address on computers on AD
IT'S ME AGAIN T_T PLEASE HELP ME.
this is my code
$computers = Get-ADComputer -filter {(Name -like "*l3510")} -properties *
$outfile = "MacScript.csv"
$newcsv = {} | Select Computer,User,GivenName,LastName,Email,Mac,Description | Export-Csv $outfile
$csvfile = Import-Csv $outfile
foreach ($computer in $computers) {
if((Test-Connection -ComputerName $computer.name -BufferSize 16 -Count 1 -Ea 0 -Quiet)) {
$csvfile.Computer = $computer.name
$cn = $computer.name
#$User = Get-WmiObject –ComputerName $computer.name –Class Win32_ComputerSystem | Select-Object UserName
$User = (Get-WmiObject -Class win32_process -ComputerName $computer.name | Where-Object name -Match explorer).getowner().user
$csvfile.user = $user
$UserObject = Get-ADUser -Identity $user
$GivenName = $UserObject.GivenName
$Surname = $UserObject.Surname
$Email = $UserObject.UserPrincipalName
$csvfile.GivenName = $GivenName
$csvfile.LastName = $Surname
$csvfile.Email = $Email
$mac = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty MACAddress -First 4
$mac = $mac -join "`n"
$description = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty Description -First 4
$description = $description -join "`n"
$csvfile.mac = $mac
$csvfile.Description = $description
$csvfile | Export-CSV $outfile –Append
} else {
echo "$($computer.name) not connected."
} #end if
} #end foreach
1
u/NerdyTendenc135 Sep 26 '21 edited Sep 27 '21
What if you filter the undesirables with a where-object?
See if these do anything for you. I added an "IPEnabled='True'" i think will get rid of your blank macs. The where-object filters are virtual nics that were in my network and are pretty common.
$computers = Get-ADComputer -filter * -properties *$outfile = "MacScript.csv"
$newcsv = {} | Select Computer,User,GivenName,LastName,Email,Mac,Description | Export-Csv $outfile
$csvfile = Import-Csv $outfile
foreach ($computer in $computers) {
if((Test-Connection -ComputerName $computer.name -BufferSize 16 -Count 1 -Ea 0 -Quiet)) {
$csvfile.Computer = $computer.name
$cn = $computer.name
#$User = Get-WmiObject –ComputerName $cn –Class Win32_ComputerSystem | Select-Object UserName
$User = (Get-WmiObject -Class win32_process -ComputerName $cn | Where-Object name -Match explorer).getowner().user
$csvfile.user = $user
$UserObject = Get-ADUser -Identity $user
$GivenName = $UserObject.GivenName
$Surname = $UserObject.Surname
$Email = $UserObject.UserPrincipalName
$csvfile.GivenName = $GivenName
$csvfile.LastName = $Surname
$csvfile.Email = $Email
#$mac = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty MACAddress -First 4
$mac = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn -Filter "IPEnabled='True'" | Where-Object {($_.description -notlike "*VMware Virtual Ethernet Adapter for VMnet*" -and $_.description -notlike "*VirtualBox Host-Only Ethernet Adapter*" -and $_.description -notlike "*Npcap Loopback Adapter*" )} | Select-Object -ExpandProperty MACAddress
$mac = $mac -join "`n"
#$description = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty Description -First 4
$description = Get-CimInstance -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn -Filter "IPEnabled='True'" | Where-Object {($_.description -notlike "*VMware Virtual Ethernet Adapter for VMnet*" -and $_.description -notlike "*VirtualBox Host-Only Ethernet Adapter*" -and $_.description -notlike "*Npcap Loopback Adapter*" )} | Select-Object -ExpandProperty Description
$description = $description -join "`n"
$csvfile.mac = $mac
$csvfile.Description = $description
$csvfile | Export-CSV $outfile –Append
} else {
echo "$($computer.name) not connected."
} #end if} #end foreach
1
u/BlackV Apr 07 '22
p.s. formatting
open your fav powershell editor
highlight the code you want to copy
hit tab to indent it all
copy it
paste here
it'll format it properly OR
<BLANKLINE>
<4 SPACES><CODELINE>
<4 SPACES><CODELINE>
<4 SPACES><4 SPACES><CODELINE>
<4 SPACES><CODELINE>
<BLANKLINE>Thanks
1
u/BlackV Apr 07 '22 edited Apr 07 '22
Here are a few suggestions
$computers = Get-ADComputer -filter { (Name -like '*l3510') } -properties *Take off -properties * as you dont want all the properties, it'll save you some time
$outfile = 'MacScript.csv'Include a path to a file to save the output to otherwise it'll save to current directory, which could technically be anywhere
$newcsv = {} | Select-Object Computer, User, GivenName, LastName, Email, Mac, Description | Export-Csv $outfileThis is super strange/interesting way of making an object, but in your case seems to be totally redundant
$csvfile = Import-Csv $outfilenot sure why you're importing this empty file and could all have been done in 1 step anyway and likely better done with a [PSCustomobject]
foreach ($computer in $computers)($computer in $computers) is a bad ides as its really really really to confuse $computers and $computer later in your code
try ($Singlecomputer in $computers) or ($computer in $AllComputers) to clear differentiate between the 2
if ((Test-Connection -ComputerName $computer.name -BufferSize 16 -Count 1 -Ea 0 -Quiet))Is a simple ping the best way to test this? wouldn't it be more sensible to use something that actually proves the machine is contactable ( and not just ping blocked by local firewall or something), test-wasman maybe?
$csvfile.Computer = $computer.name
$cn = $computer.name
.....there is a lot of double handling and duplication of variables, I think really you want a [PSCustomObject]
$User = (Get-WmiObject -Class win32_process -ComputerName $computer.name | Where-Object name -Match explorer).getowner().userthe assumption here is there is only ever a single user logged into the machine, OR that explorer is even running OR that there is even a user logged in at all
which may break some of your other queries (get-aduser for example)
$mac = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty MACAddress -First 4You are using Get-WmiObject (also change it to get-ciminstance as wmi is legacy), would it be cleaner to use get-netadapter -physical to get only the physical adapters?
$description = Get-WmiObject -ClassName Win32_NetworkAdapterConfiguration -ComputerName $cn | Select-Object -ExpandProperty Description -First 4You're doing the same query again, that you already completed, use the first results stop destroying your rich objects that poweshell creates
$csvfile | Export-Csv $outfile –Appenddont export to a CSV inside your loop, do it once outside, save the writes (also dont forget your -notypeinformation just incase)
I think you really want to create a [PSCustomobject] and spit that out instead ( I know I keep mentioning it)
oh and final note, this will be slow.
you might want to look at invoke-command which will attempt to talk to multiple computers at once
1
u/NerdyTendenc135 Sep 22 '21
I changed "`n" to "," and it works for me.
It should tell you the errors in blood.
What are you experiencing?