Add variable to be used with foreach to get the owner object on AD

Welcome Forums General PowerShell Q&A Add variable to be used with foreach to get the owner object on AD

This topic contains 5 replies, has 4 voices, and was last updated by

 
Participant
9 months, 3 weeks ago.

  • Author
    Posts
  • #100204

    Participant
    Points: 0
    Rank: Member

    Hello..... the sccript below is working, however I woull like to add all the information from each object in variable in order to organize the results with "| select " command. Could please somebody help me out?

    ___________________________________________________________________
    cls
    $DomainName = 'LDAP://CN=Computers,DC=london,DC=com'
    $Root = New-Object DirectoryServices.DirectoryEntry $DomainName
    $objSearcher = New-Object DirectoryServices.DirectorySearcher
    $objSearcher.SearchRoot = $Root
    $objSearcher.SearchScope = "SubTree"

    $colResults = $objSearcher.FindAll()
    foreach ($objResult in $colResults)
    {

    $computer = $objResult.GetDirectoryEntry()
    $computer.name
    $computer.ObjectSecurity.Owner

    }

  • #100213

    Participant
    Points: 43
    Rank: Member

    There are a couple of ways to accomplish this. The easiest would be to create calculated properties of $computer. Calculated properties are just one-off hashtables you can use as custom or replacement properties.

    $computer = $objResult.GetDirectoryEntry()
    $computer | Select-Object -Property name,@{Name='Owner';Expression={$_.ObjectSecurity.Owner}}
    

    In that example, you actually would not need to create the $computer variable. You could pipe $objResult.GetDirectoryEntry() directly to Select-Object.

    • #100285

      Participant
      Points: 0
      Rank: Member

      Very appreciate your help.. so I am very beginner with powershel...How can I get the pthers proprieties that I could get from the computer? Do you advice any how to with firts steps?

      Thanks so much.

  • #100243

    Participant
    Points: 518
    Helping Hand
    Rank: Major Contributor

    Is there a reason you are not using the ActiveDirectory module installed with RSAT tools? The code is as simple as:

    Import-Module ActiveDirectory
    
    $computers = Get-ADComputer -Filter * -Properties ManagedBy
    
    • #100282

      Participant
      Points: 0
      Rank: Member

      Thanks

  • #100251
    js

    Participant
    Points: 545
    Helping Hand
    Rank: Major Contributor

    Also, you can't pipe from statements like "foreach($i in $list)" in powershell. That's one of the big gotchas.

The topic ‘Add variable to be used with foreach to get the owner object on AD’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort