Select OUTPUT of a calculated property

This topic contains 12 replies, has 3 voices, and was last updated by Profile photo of 37mm 37mm 1 year, 4 months ago.

  • Author
    Posts
  • #29244
    Profile photo of 37mm
    37mm
    Participant

    How do I just select the first output of the macaddress field.

    Get-ADComputer -Filter * -Property * | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet }},@{ label = "Mac Address"; Expression = ({(Get-WmiObject win32_networkadapter).macaddress})}
    

    When I try this
    @{ label = "Mac Address"; Expression = ({(Get-WmiObject win32_networkadapter).macaddress | select @{n='Mac Address';e={ $_.Name.split(',')[0].trim() }}})

    I get @{Mac Address=} in the column...
    Thanks again for the help!

  • #29245
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    Two things:

    1. You need to add a -ComputerName parameter to your expression, otherwise you're going to be selecting your own Macaddress every time this runs.

    2. I think your calculated property code was a bit too complex, too many select statements. I shortened it to the following.

    @{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter) | ? MacAddress -ne $null | select -first 1 -expand MacAddress}}

  • #29246
    Profile photo of 37mm
    37mm
    Participant
    Get-ADComputer -Filter * -Property * | Select-Object name, dnshostname, operatingsystem, operatingsystemservicepack, ipv4address, lastlogondate, logoncount, @{ label = "PingResults"; Expression = { tnc $_.ipv4address -InformationLevel Quiet }},@{ label = "Mac Address"; Expression = ({(Get-WmiObject win32_networkadapter).macaddress | select @{n='Mac Address';e={ $_.Name.split(',')[1].trim() }}})}
    

    this is the full line of code. I am trying to pull mac address on a domain query inventory report

  • #29247
    Profile photo of 37mm
    37mm
    Participant

    Ok soo currently I am only pulling my local address, smh.

    How do I pass the computer name for each computer?

  • #29249
    Profile photo of 37mm
    37mm
    Participant

    @{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter -ComputerName $_.name).macaddress}}

    This works, but i Get alot of null values

  • #29251
    Profile photo of Stephen Owen
    Stephen Owen
    Participant

    Use my logic in the Calculated property, it filters out nulls for you.

    Expression={(Get-WmiObject win32_networkadapter -ComputerName $_.Name) | ? MacAddress -ne $null | select -first 1 -expand MacAddress}}

  • #29252
    Profile photo of 37mm
    37mm
    Participant

    This works thanks brother you got the ball rolling!

    @{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter -ComputerName $_.name).macaddress -ne $null
    
  • #29253
    Profile photo of 37mm
    37mm
    Participant

    Saw your post after I posted, Yes that works great now the question how to know that I am getting the mac of the Ethernet connection

  • #29255
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    If you filter by the adapter type, you can get the macaddress of all of your ethernet adapters. If you need a specific ethernet adapter, you will need to know the name of it.

    @{ label = "Mac Address"; Expression={(Get-WmiObject win32_networkadapter | Where-Object {$_.AdapterType -eq "Ethernet 802.3"}).macaddress -ne $null}}
    
  • #29256
    Profile photo of 37mm
    37mm
    Participant

    Thanks for the input Curtis. I am running it now. I there a reason you ommited the $_.name variable after defining the wmi object? I figure it was a mistake?

  • #29257
    Profile photo of 37mm
    37mm
    Participant
  • #29258
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    Sorry, I was just testing against my local machine, but yes add -ComputerName $_.name back to run against remote machines.

  • #29259
    Profile photo of 37mm
    37mm
    Participant

    I credited you in the creation of tool.
    Thanks a bunch shoot me a email g60wall@gmail.com
    Cheers!

You must be logged in to reply to this topic.