none
Get-ADComputer with SearchBase from results from another command RRS feed

  • Question

  • I'm trying to gets the results from this command:

    Get-ADOrganizationalUnit -Filter *|? { ($_.distinguishedname -like '*power*') }| Select-Object -ExpandProperty DistinguishedName
    Into 
    Get-ADComputer -SearchBase <results of previous command> -Filter {Enabled -eq $true} -Properties CanonicalName | Group-Object {($_.CanonicalName -Split "/")[2]}}

    How can this be done? I've tried with {} and (). I've tried all kinds of ways, but I've been unsuccessful. 

    Thursday, August 17, 2017 6:24 PM

Answers

  • Ah i just found out what I needed to do:

    $dn=Get-ADOrganizationalUnit -Filter * | Where-Object {$_.distinguishedname -Match 'power'} | Select-Object -ExpandProperty DistinguishedName
    ForEach ($d in $dn) {Get-ADComputer -SearchBase $d -Filter {Enabled -eq $true} -Properties CanonicalName | Group-Object {($_.CanonicalName -Split "/")[2]}}

    • Marked as answer by lytvinh Monday, August 21, 2017 1:57 PM
    Thursday, August 17, 2017 7:38 PM
  • This is simpler:

    Get-ADOrganizationalUnit -Filter * | 
    	Where-Object{ $_.distinguishedname -Match 'power' } |
    	ForEach-Object{Get-ADComputer -SearchBase $d -Filter {Enabled -eq $true} -Properties CanonicalName }| 
    	Group-Object{$_.CanonicalName.Split('/')[2]}

    Both only work if the OUs are all at the same level.


    \_(ツ)_/



    • Edited by jrv Thursday, August 17, 2017 7:58 PM
    • Marked as answer by lytvinh Monday, August 21, 2017 1:57 PM
    Thursday, August 17, 2017 7:56 PM

All replies

  • $dn = Get-ADOrganizationalUnit -Filter * |
    	Where-Object {$_.distinguishedname -Match 'power'} |
    	Select-Object -ExpandProperty DistinguishedName -first 1


    \_(ツ)_/

    Thursday, August 17, 2017 6:53 PM
  • I only get 1 result. I should have 15 results though. How can I get each distinguishedname to be used as the searchbased?
    Thursday, August 17, 2017 7:20 PM
  • The $dn is the searchbase you asked for.

    \_(ツ)_/

    Thursday, August 17, 2017 7:27 PM
  • Ah i just found out what I needed to do:

    $dn=Get-ADOrganizationalUnit -Filter * | Where-Object {$_.distinguishedname -Match 'power'} | Select-Object -ExpandProperty DistinguishedName
    ForEach ($d in $dn) {Get-ADComputer -SearchBase $d -Filter {Enabled -eq $true} -Properties CanonicalName | Group-Object {($_.CanonicalName -Split "/")[2]}}

    • Marked as answer by lytvinh Monday, August 21, 2017 1:57 PM
    Thursday, August 17, 2017 7:38 PM
  • This is simpler:

    Get-ADOrganizationalUnit -Filter * | 
    	Where-Object{ $_.distinguishedname -Match 'power' } |
    	ForEach-Object{Get-ADComputer -SearchBase $d -Filter {Enabled -eq $true} -Properties CanonicalName }| 
    	Group-Object{$_.CanonicalName.Split('/')[2]}

    Both only work if the OUs are all at the same level.


    \_(ツ)_/



    • Edited by jrv Thursday, August 17, 2017 7:58 PM
    • Marked as answer by lytvinh Monday, August 21, 2017 1:57 PM
    Thursday, August 17, 2017 7:56 PM