Automate the removal of the DNS Root of a computer name

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of I Am Sir Ask Alot I Am Sir Ask Alot 2 months, 4 weeks ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #44373
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    I am trying to figure out how I can remove the DNS Root of a computer so I can plug that into the Get-ADComputer cmdlet.

    What I am doing is taking a list of IP addresses and resolving those to computer names like so:

    $ComputerNames = @(Get-Content 'C:\IPAddressList.txt' | ForEach-Object{ 
    ([System.Net.Dns]::GetHostEntry($_)).Hostname.ToUpper()})

    Then when I call on the $ComputerNames variable it gives me the computer names along with the DNS Root at the end, like so:

    SalesComputer102.my.domain.com

    How can I go about removing the ".my.domain.com" DNS Root so I can just use the computer name for Get-ADComputer?

    Get-ADComputer errors if I try to use 'SalesComputer102.my.domain.com'

    Thanks

    #44393
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Assuming $computername has the entire string,

    $justthecomputer = $computername -replace ".mydomain.com",""

    Would do that.

    #45062
    Profile photo of I Am Sir Ask Alot
    I Am Sir Ask Alot
    Participant

    Thanks Don, I already figured this out by doing the following:

    $FQDNComputers = 'SalesComputer102.my.domain.com'
    $DNSRoot = (Get-ADDomain).DNSRoot 
    Foreach ($Computer in $FQDNComputers)
    {           
        $Computer -Replace(".$DNSRoot","")
    }
    
Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.