locked
Error While Export Computers Name From AD Thru Powershell RRS feed

  • Question

  • Can Any One Help me When I  Export Compters Name From AD Thru Powershelll Thru Below Queries I Didn't Received Computer OU Name in Excel (Sub OU) And One Error Show In Excel

    Script :-

    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * | Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,OU | Export-CSV c:\test1.csv -NoTypeInformation -Encoding UTF8

    Error :-

    Microsoft.ActiveDirectory.Management.ADPropertyValueCollection

    Actual Requirement :- We Have Lots of Child OU So I Want Each Computer Names Against Child OU Name


    DushYant'


    • Edited by DushYant P Thursday, September 7, 2017 12:30 PM
    Thursday, September 7, 2017 12:19 PM

Answers

  • Should work actually:

    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * | 
        Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,@{Name='OU';Expression={$_.DistinguishedName.Substring(($_.DistinguishedName.IndexOf(',') + 1),$_.DistinguishedName.Length  - $_.DistinguishedName.IndexOf(',') -1)}}  | 
            Export-CSV c:\test1.csv -NoTypeInformation -Encoding UTF


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by DushYant P Monday, September 18, 2017 10:58 AM
    Thursday, September 7, 2017 2:24 PM
  • Even simpler would be this:
    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * | 
        Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,@{Name='OU';Expression={$_.DistinguishedName.Substring(($_.DistinguishedName.IndexOf(',') + 1))}}  | 
            Export-CSV c:\test1.csv -NoTypeInformation -Encoding UTF


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by I.T Delinquent Thursday, September 7, 2017 2:59 PM
    • Marked as answer by DushYant P Monday, September 18, 2017 10:58 AM
    Thursday, September 7, 2017 2:58 PM

All replies

  • As far as I know "OU" is not an AD attribute of the computer account object. But the dinstinguished name is actually the OU extended with the Name of the actual object. But anyway the query should work even this way ...  if you get an error - post it here please.

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, September 7, 2017 12:42 PM
  • So Can You Guide Me Which Function I Use in Excel For Split Distinguished, in Distinguished Value Comes Dynamically, I Used Txt To Data Option Bt Its Not Worked For Me.. So If You Guided Me Any Other Function For Split Distinguished Its Good For Me  

    DushYant'

    Thursday, September 7, 2017 1:13 PM
  • Should work actually:

    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * | 
        Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,@{Name='OU';Expression={$_.DistinguishedName.Substring(($_.DistinguishedName.IndexOf(',') + 1),$_.DistinguishedName.Length  - $_.DistinguishedName.IndexOf(',') -1)}}  | 
            Export-CSV c:\test1.csv -NoTypeInformation -Encoding UTF


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Marked as answer by DushYant P Monday, September 18, 2017 10:58 AM
    Thursday, September 7, 2017 2:24 PM
  • Even simpler would be this:
    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * | 
        Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,@{Name='OU';Expression={$_.DistinguishedName.Substring(($_.DistinguishedName.IndexOf(',') + 1))}}  | 
            Export-CSV c:\test1.csv -NoTypeInformation -Encoding UTF


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by I.T Delinquent Thursday, September 7, 2017 2:59 PM
    • Marked as answer by DushYant P Monday, September 18, 2017 10:58 AM
    Thursday, September 7, 2017 2:58 PM
  • Although unusual, the RDN of a computer can include embedded commas, just like user objects. I have used code similar to below to parse DN values for either the RDN or distinguished name of the parent OU/Container of any AD object:

    $DN = "CN=West, Computer1,OU=Sales, West,DC=Domain,DC=com"
    $ParentRDN = ($DN -Split ',CN=|,OU=|,DC=')[1]
    $ParentDN = $DN.SubString($DN.IndexOf(($DN -Split ',CN=|,OU=|,DC=')[1]) - 3)
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Thursday, September 7, 2017 9:54 PM
  • I would recommend avoiding string parsing and use the IADsPathname interface instead.


    -- Bill Stewart [Bill_Stewart]

    Thursday, September 7, 2017 10:28 PM
  • You're right. That's even better. 

    Thanks.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, September 7, 2017 10:50 PM
  • Can You Give Me Code, Much Easy To Understand For Me

    DushYant'

    Friday, September 8, 2017 5:16 AM
  • I would use the ADSI Parent method, which is designed for this purpose. It returns the ADsPath, so I cut off the first 7 characters, "LDAP://", to retrieve the DN of the parent OU/Container. Perhaps:

    Get-ADComputer -SearchBase 'OU=test,DC=abc,DC=xyz,DC=local' -Filter * -Property * |
        Select-Object Name,OperatingSystem,DistinguishedName,DNSHostName,lastlogondate,whenCreated,@{Name='OU';Expression={(([ADSI]"LDAP://$($_.distinguishedName)").Parent).SubString(7)}}  |
            Export-CSV c:\test1.csv -NoTypeInformation
    


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, September 8, 2017 1:24 PM