Split Active Directory Computer DNSHostname

Hello,

I am trying to achieve the following but with no luck as yet. I need to export the following DNSHostName from Active Directory and split them in a different format. Here is what I have been able to export so far with the below script:

DNSHostName

ComputerNameTest1.Net.BIOSDomain.Name
ComputerNameTest2.Net.BIOSDomain.Name
ComputerNameTest3.Net.BIOSDomain.Name

 

However, I would need to export them in the following format:

 

DNSHostName

ComputerNameTest1 Net.BIOSDomain.Name
ComputerNameTest2 Net.BIOSDomain.Name
ComputerNameTest3 Net.BIOSDomain.Name

 

Is there any way this can be achieved amending the below script?

 

foreach ($domain in $domains)
{
$csvdata = import-csv -Path $csvfile
$csvdata | ForEach-Object { Get-ADComputer -Server $domain -Identity $_.id } | select-object -property DNSHostName | Out-file -Append -FilePath $txtfile2
}

 

Thanks,

PowerDude

You could start with something like this:

$csvdata = import-csv -Path $csvfile
$Result = foreach ($domain in $domains){
    $csvdata | 
        ForEach-Object { Get-ADComputer -Server $domain -Identity $_.id  | 
                Select-Object -Property Name , @{Name = 'Domain' ; Expression = {$_.DNSHostName.Split('.')[1..($_.DNSHostName.Split('.').count)] -join '.'}}
        }
}
$Result | Export-Csv -Path 'path to CSV file' -NoTypeInformation

For structured data you should use CSV files.

[quote quote=212430]You could start with something like this:

PowerShell
8 lines
<textarea class="ace_text-input" style="opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;" spellcheck="false" wrap="off"></textarea>
1
2
3
4
5
6
7
8
$csvdata = import-csv -Path $csvfile
$Result = foreach ($domain in $domains){
$csvdata |
ForEach-Object { Get-ADComputer -Server $domain -Identity $_.id |
Select-Object -Property Name , @{Name = 'Domain' ; Expression = {$_.DNSHostName.Split('.')[1..($_.DNSHostName.Split('.').count)] -join '.'}}
}
}
$Result | Export-Csv -Path 'path to CSV file' -NoTypeInformation
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
For structured data, you should use CSV files.

[/quote]
Hi Olaf,

Thanks that has worked for me. The reason I need it to be a txt file is that is the only file syntax supported by my tool. Is there any way we can remove the header as well?

Name Domain
---- ------

Ideally, i would need it to be just like that:

 

ComputerNameTest1 Net.BIOSDomain.Name
ComputerNameTest2 Net.BIOSDomain.Name
ComputerNameTest3 Net.BIOSDomain.Name

 

Thanks for your help much appreciated :slight_smile:

 $csvdata | 
        ForEach-Object { Get-ADComputer -Server $domain -Identity $_.id  | 
                Select-Object -ExpandProperty @{Name = 'Domain' ; Expression = {$_.DNSHostName.Split('.')[1..($_.DNSHostName.Split('.').count)] -join '.'}}
        }
}
$Result | Out-File -FilePath \\path\to\file.txt

First I’d rather talk to the vendor of that tool and ask him why he refuses to support and worldwide/industry-wide standard. Until he changes his mind and provides you with an updated version of the tool you can simply read the content of the newly created csv file with Get-Content to a variable and output this variable through a Select-Object -Skip 1 with an Out-File back to the original file. If you have problems with that use your favorite internet search engine - you will find more than enough examples of it. :wink:

Thanks, Oleg. I will use the recommendation provided. Have a great day.

Who is Oleg? :wink:

Oops, thanks, Olaf :slight_smile: