Welcome › Forums › General PowerShell Q&A › Split Active Directory Computer DNSHostname
- This topic has 9 replies, 3 voices, and was last updated 10 months ago by
Participant.
-
AuthorPosts
-
-
March 25, 2020 at 1:34 pm #212397
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.NameHowever, I would need to export them in the following format:
DNSHostName
———–ComputerNameTest1 Net.BIOSDomain.Name
ComputerNameTest2 Net.BIOSDomain.Name
ComputerNameTest3 Net.BIOSDomain.NameIs there any way this can be achieved amending the below script?
PowerShell12345foreach ($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
-
March 25, 2020 at 3:38 pm #212430
You could start with something like this:
PowerShell12345678$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' -NoTypeInformationFor structured data you should use CSV files.
-
March 25, 2020 at 5:06 pm #212463
You could start with something like this:
PowerShell8 lines<textarea class=”ace_text-input” style=”opacity: 0; height: 18px; width: 6.59781px; left: 44px; top: 0px;” spellcheck=”false” wrap=”off”></textarea>12345678$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’ –NoTypeInformationXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXFor structured data, you should use CSV files.
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?
PowerShell12<span style="font-family: 'andale mono', monospace">Name Domain</span><span style="font-family: 'andale mono', monospace">---- ------</span>Ideally, i would need it to be just like that:
ComputerNameTest1 Net.BIOSDomain.Name
ComputerNameTest2 Net.BIOSDomain.Name
ComputerNameTest3 Net.BIOSDomain.NameThanks for your help much appreciated 🙂
-
March 26, 2020 at 1:23 pm #212625PowerShell123456$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
-
March 26, 2020 at 2:52 pm #212649
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?
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. 😉
-
March 27, 2020 at 1:10 pm #212901
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?
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 favourite internet search engine – you will find more than enough examples of it.
Thanks, Oleg. I will use the recommendation provided. Have a great day.
-
March 27, 2020 at 1:52 pm #212910
Thanks, Oleg. I will use the recommendation provided. Have a great day.
Who is Oleg? 😉
-
March 27, 2020 at 1:56 pm #212916
Thanks, Oleg. I will use the recommendation provided. Have a great day.
Who is Oleg?
Oops, thanks, Olaf 🙂
-
-
AuthorPosts
- The topic ‘Split Active Directory Computer DNSHostname’ is closed to new replies.