Split Active Directory Computer DNSHostname

Welcome Forums General PowerShell Q&A Split Active Directory Computer DNSHostname

Viewing 7 reply threads
  • Author
    Posts
    • #212397
      Participant
      Topics: 4
      Replies: 8
      Points: 72
      Rank: Member

      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

      • This topic was modified 2 months, 1 week ago by PowerDude.
      • This topic was modified 2 months, 1 week ago by PowerDude.
      • This topic was modified 2 months, 1 week ago by PowerDude.
      • This topic was modified 2 months, 1 week ago by PowerDude.
      • This topic was modified 2 months, 1 week ago by PowerDude.
    • #212430
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      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.

    • #212463
      Participant
      Topics: 4
      Replies: 8
      Points: 72
      Rank: Member

      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.

      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 🙂

      • This reply was modified 2 months, 1 week ago by PowerDude.
      • This reply was modified 2 months, 1 week ago by PowerDude.
      • This reply was modified 2 months, 1 week ago by PowerDude.
    • #212625
      Participant
      Topics: 5
      Replies: 322
      Points: 446
      Helping Hand
      Rank: Contributor
       $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
      
    • #212649
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

      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. 😉

    • #212901
      Participant
      Topics: 4
      Replies: 8
      Points: 72
      Rank: Member

      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.

    • #212910
      Participant
      Topics: 4
      Replies: 2249
      Points: 5,494
      Helping Hand
      Rank: Community MVP

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

      Who is Oleg? 😉

    • #212916
      Participant
      Topics: 4
      Replies: 8
      Points: 72
      Rank: Member

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

      Who is Oleg? 😉

      Oops, thanks, Olaf 🙂

Viewing 7 reply threads
  • You must be logged in to reply to this topic.