small problem

This topic contains 10 replies, has 2 voices, and was last updated by Profile photo of Brad Brad 2 weeks, 4 days ago.

  • Author
    Posts
  • #72226
    Profile photo of Brad
    Brad
    Participant

    Hi All,

    i have a small problem
    simple script to find computer in AD

    ### Get-ADComputer by Name ###
    ### C:\Users.txt - user names
    
    $username = Get-Content "C:\Users.txt"
    foreach ($user in $username)
    {
    Get-ADComputer -Filter $("Description -like '*$user*'") -Property Name,Description,OperatingSystem | Select Name,Description,OperatingSystem | 
    Export-Csv "C:\Users.csv" -Append -NoTypeInformation
    }
    

    this part working Ok, just one things
    if it didn't find description in AD it will remove it from list
    My question, how i can have the same list in scv and if its can't find in AD it will say not in AD

    Thanks

  • #72230
    Profile photo of Olaf Soyk
    Olaf Soyk
    Participant

    This could be a starting point for you:

    $username = Get-Content "C:\Users.txt"
    foreach ($user in $username)
    {
        $SearchHit = Get-ADComputer -Filter $("Description -like '*$user*'") -Property Name,Description,OperatingSystem 
        If($SearchHit){
            $output=[PSCustomObject]@{
                'Name' = $SearchHit.Name
                'Description' = $SearchHit.Description
                'OperatingSystem' = $SearchHit.OperatingSystem 
            }
        }
        Else{
            $output=[PSCustomObject]@{
                'Name' = $user
                'Description' = 'Not in AD'
                'OperatingSystem' = 'Not in AD'
            }
        }
        $output | 
        Export-Csv "C:\Users.csv" -Append -NoTypeInformation
    }

    (untested!!)

  • #72233
    Profile photo of Brad
    Brad
    Participant

    Hi Olaf,

    Thank you, this is what i was looking for
    one more things, if in list i have name like O'laf or B'rad it throwing error
    how to avoid this

    Thanks

    • #72239
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Change

      "Description -like '*$user*'"

      to

      "Description -like ""*$user*"""
  • #72241
    Profile photo of Brad
    Brad
    Participant

    Thank you Olaf

    why its not working in ADUser, i try modify your script
    to

    $username = Get-Content "C:\Users.txt"
    
    foreach ($user in $username)
    {
        
    $SearchHit = Get-ADUser -Filter ("cn -Like '*$user*'") -Property Name,SamAccountName,mail
     If($SearchHit){
            $output=[PSCustomObject]@{
                'Name' = $SearchHit.Name
                'SamAccountName' = $SearchHit.SamAccountName
                'Mail' = $SearchHit.mail 
            }
        }
        Else{
            $output=[PSCustomObject]@{
                'Name' = $user
                'SamAccountName' = 'Not in AD'
                'Mail' = 'Not in AD'
            }
        }
        $output | 
    Export-Csv "C:\Users.csv" -NoTypeInformation
    }
    

    but its not working this way

    Thanks again for your help

    • #72242
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      Do you get errors? I already have a suspision ... 😉

  • #72245
    Profile photo of Brad
    Brad
    Participant

    No, no error
    in csv file just one last name from the list

    i just removed from code -Append

    • #72247
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      OK ... try to change

      -Filter ("cn -Like '*$user*'")

      to

      -Filter $("cn -like ""*$user*""")
  • #72373
    Profile photo of Brad
    Brad
    Participant

    Hi Olaf,
    still not working
    no errors but it in output file just last name from list

    • #72374
      Profile photo of Olaf Soyk
      Olaf Soyk
      Participant

      So you have to add the "-append" again. Otherwise every new entry will overwrite the last one.

  • #72376
    Profile photo of Brad
    Brad
    Participant

    Thank you 😉

You must be logged in to reply to this topic.