Expression Help

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Mike F Robbins Mike F Robbins 2 years, 2 months ago.

  • Author
    Posts
  • #18699
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    Hi,
    I am using get-adcomputer to get the name and dnshostname of servers.
    The dnshostname value is servername.domain.global.
    I would like to change this to just the domain.global value in my output, not servername.domain.global.

    Struggling, testing with only one server, I have tried to get the computername and domain name only (as opposed to the fqdn) into two columns:

    $computerdetails = Get-ADComputer -server $dc -Filter {Name -like "servername*" } -Property * | select @{N="computername";E={$_.name}}`
    ,@{N="dnsdomain";E={$computerdetails.DNSHostName.Replace("$computerdetails.computername.","")}}

    The computername works ok, but the dnsdomain is empty.

    Can someone advise me please?
    Thanks!

  • #18705
    Profile photo of Jason Hofferle
    Jason Hofferle
    Participant

    Try:
    select @{n="ComputerName";e={$_.name}},@{n="dnsdomain";e={$_.DNSHostName.Replace("$($_.name).","")}}

    • #18707
      Profile photo of bvi1998 .
      bvi1998 .
      Participant

      Thanks! In the meantime I used split, but Jason's solution worked as well 🙂

  • #18706
    Profile photo of bvi1998 .
    bvi1998 .
    Participant

    haha I'm answering my own questions today it seems.
    I did this and it worked:

    $computerdetails = Get-ADComputer -server $DC -Filter {Name -like "servername*" } -Property * | select @{N="computername";E={$_.name}}`
    ,@{N="dnsdomain";E={$_.DNSHostName.Split(".")[1] + "." + $_.DNSHostName.Split(".")[2]}}

    🙂

  • #18709
    Profile photo of Mike F Robbins
    Mike F Robbins
    Participant

    Here's another option. Note that you don't need to use -Properties * as the properties you're looking for are returned by Get-ADComputer by default:

    Get-ADComputer -server $dc -Filter {Name -like 'servername*'} |
    Select-Object -Property @{name='computername';expression={$_.name}},
    @{name='dnsdomain';expression={$_.DNSHostName -Replace '^(.*?)\.'}}

You must be logged in to reply to this topic.