Determine domain of the computer

Welcome Forums General PowerShell Q&A Determine domain of the computer

Viewing 2 reply threads
  • Author
    Posts
    • #211938
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      I am looking for an output from the computerlist to determine if that computer exist in AD, but along with that I also need to pull out the Domain and the ping result.

      Domain =$domain – This gives me a wrong result and not sure where to put in this. I am missing something simple, but not able to resolve it

      $Computers= get-content "C:\temp\Computers.txt"
      $data = @()
      foreach ($computer in $Computers)
      {
      $DN=Foreach ($domain in $Domains)
      {
      try
      {
      Get-ADComputer -Identity $computer -Server $domain | select -expandproperty DistinguishedName
      
      }
      
      Catch
      {out-null}
      }
      
      If ($DN) {
      $data += [PSCustomObject]@{
      Name = $computer
      DN = $DN
      Domain =$domain
      }
      }
      
      else{
      $data += [PSCustomObject]@{
      Name = $computer
      DN = "Doesnt exist in AD"
      Domain = "NA"
      }
      }
      }
      
      $data |export-csv C:\temp\Oulist.csv -NoTypeInformation
      
    • #211977
      Participant
      Topics: 12
      Replies: 1622
      Points: 2,560
      Helping Hand
      Rank: Community Hero

      The domain is contained in the DistinguishedName, but try changing you loop a bit like this and see if it works (untested):

      $Domains = "Domain1', "Domain2"
      $Computers= get-content "C:\temp\Computers.txt"
      
      $results = foreach ($computer in $Computers){
          foreach ($domain in $Domains){
      
              $computer = Get-ADComputer -Filter {SamAccountName -eq $computer} -Server $domain | 
                          Select @{Name='DN';Expression={$_.DistinguishedName}},
                                 Name,
                                 @{Name='Domain';Expression={$domain}}
          }
      
          if (!$computer) {
              [PSCustomObject]@{
                  Name   = $computer
                  DN     = $null
                  Domain = $null
              }
          }
      }
      
      $results
      
    • #216315
      Participant
      Topics: 12
      Replies: 21
      Points: 135
      Rank: Participant

      The domain is contained in the DistinguishedName, but try changing you loop a bit like this and see if it works (untested):

      PowerShell
      23 lines

      <textarea class=”ace_text-input” wrap=”off” autocorrect=”off” autocapitalize=”off” spellcheck=”false” style=”opacity: 0; height: 18px; width: 7px; left: 51px; top: 0px;”></textarea>

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      $Domains = “Domain1′, “Domain2
      $Computers= get-content “C:\temp\Computers.txt”
      $results = foreach ($computer in $Computers){
      foreach ($domain in $Domains){
      $computer = Get-ADComputer Filter {SamAccountName -eq $computer} Server $domain |
      Select @{Name=‘DN’;Expression={$_.DistinguishedName}},
      Name,
      @{Name=‘Domain’;Expression={$domain}}
      }
      if (!$computer) {
      [PSCustomObject]@{
      Name = $computer
      DN = $null
      Domain = $null
      }
      }
      }
      $results
      XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

      Sorry for the late reply on this. thanks for your guidancce

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