Can't add a NoteProperty on a PSCustomObject

Welcome Forums General PowerShell Q&A Can't add a NoteProperty on a PSCustomObject

This topic contains 7 replies, has 3 voices, and was last updated by

 
Participant
3 years, 4 months ago.

  • Author
    Posts
  • #28410

    Participant
    Points: 42
    Rank: Member

    Hi,

    I'm trying to add a noteproperty to a variable that I've created importing a CSV file.
    And I can't... Any help?

    CSV file:

    Name;Title
    THOMAS JEFFERSON;SUPPORT ANALYST
    MORGAN FREEMAN;DATABASE ANALYST
    etc...

    Code:

    $Users = Import-Csv C:\Users\user\Desktop\users.csv -Delimiter ";"
    $Users | ForEach-Object {
    $_.Name = (get-culture).TextInfo.ToTitleCase($_.Name.tolower())
    $_.Title = (get-culture).TextInfo.ToTitleCase($_.Title.tolower())
    }
    $Users | ForEach-Object {$_ | Add-Member NoteProperty ADuser (Get-ADUser -Filter {displayname -eq $_.Name} -Properties SamAccountName).SamAccountName}

    Error:

    Get-ADUser : Propriedade: 'Nome' não encontrado no objeto de tipo:
    'System.Management.Automation.PSCustomObject'.
    No linha:1 caractere:71
    + $Colaboradores | ForEach-Object {$_ | Add-Member NoteProperty ADuser
    (Get-ADUser ...
    +
    ~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentExcep
    tion
    + FullyQualifiedErrorId : Propriedade: 'Nome' não encontrado no objeto de
    tipo: 'System.Management.Automation.PSCustomObject'.,Microsoft.ActiveDirec
    tory.Management.Commands.GetADUser

  • #28411

    Participant
    Points: 42
    Rank: Member

    I forgot:

    Get-Member:

    PS C:\> $Users | gm

    TypeName: System.Management.Automation.PSCustomObject

    Name MemberType Definition
    ---- ---------- ----------
    Equals Method bool Equals(System.Object obj)
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    Title NoteProperty System.String Title=Support Analyst
    Name NoteProperty System.String Name=Thomas Jefferson

  • #28414

    Participant
    Points: 350
    Helping Hand
    Rank: Contributor

    Try this:

    $Users = Import-Csv C:\Users\user\Desktop\users.csv -Delimiter ";" |
    Select @{Name="Name";Expression={(get-culture).TextInfo.ToTitleCase($_.Name.ToLower())}},
           @{Name="Title";Expression={(get-culture).TextInfo.ToTitleCase($_.Title.ToLower())}},
           @{Name="AdUser";Expression={Get-ADUser -Filter "Displayname -eq $((get-culture).TextInfo.ToTitleCase($_.Title.ToLower()))" | Select -ExpandProperty SamAccountName}}
    
  • #28416

    Participant
    Points: 42
    Rank: Member

    Rob Simmers,

    Thanks once again.
    But something isn't working ok.
    When I change this part to:

    @{Name="AdUser";Expression={Get-ADUser -Filter {Displayname -eq "THOMAS JEFFERSON"} | Select -ExpandProperty SamAccountName}}

    it works...
    When I use this:

    $((get-culture).TextInfo.ToTitleCase($_.Title.ToLower()))

    Don't work...

  • #28418

    Participant
    Points: 42
    Rank: Member

    Trying this, gives me the same error...


    $Users | ForEach-Object {
    $_.Name = (get-culture).TextInfo.ToTitleCase($_.Name.tolower())
    $_.Title = (get-culture).TextInfo.ToTitleCase($_.Title.tolower())
    $_.ADUsers = Get-ADUser -Filter {DisplayName -eq $_.Name} | select -ExpandProperty SamAccountName
    }

    Error:

    Get-ADUser : Propriedade: 'Nome' não encontrado no objeto de tipo: 'System.Management.Automation.PSCustomObject'.
    No linha:5 caractere:18
    + $_.UsuarioNoAD = Get-ADUser -Filter {DisplayName -eq $_.Nome} | select -ExpandPr ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : Propriedade: 'Nome' não encontrado no objeto de tipo: 'System.Management.Automation.PSCustomObject'.,Microsoft
    .ActiveDirectory.Management.Commands.GetADUser

  • #28428

    Participant
    Points: 0
    Rank: Member
    $users = Import-Csv -Path users.csv -Delimiter ";"
    $results = $users | ForEach-Object {
        $name = (get-culture).TextInfo.ToTitleCase($_.Name.tolower())
        [PSCustomObject]@{
            Name = $name
            Title = (get-culture).TextInfo.ToTitleCase($_.Title.tolower())
            SamAccountName = (Get-ADUser -Filter "Name -eq ""$name""").SamAccountName
        }
    }
    # Sample outputs - pick one or more, your choice
    $results
    $results | Format-Table -AutoSize
    $results | Out-GridView
    $results | Export-Csv -Path .\foo.csv -NoTypeInformation -Encoding ASCII
    $results | Out-File -FilePath .\foo.txt -Encoding ASCII
    $results | Export-Clixml -Path .\foo.xml -Encoding ASCII
    
  • #28478

    Participant
    Points: 42
    Rank: Member

    Bob McCoy,

    Thank you very much!
    Worked like a charm =]

    Nice to have smart people guiding us who doesn't understand PowerShell very well.

  • #28480

    Participant
    Points: 0
    Rank: Member

    I'm always a little leery of folks who will create an object and then do a bunch of add-member's to it. It doesn't make sense and it's not very efficient. It's better to create a hash table and then create the object once. I typically use the [PSCustomObject] accelerator and collect all the objects in a collection, such as $results above. That gives me tremendous flexibility with what I want to do with the resulting objects on the tail end.

    Best of luck as you become more PowerShell proficient. It's all in the doing.

The topic ‘Can't add a NoteProperty on a PSCustomObject’ is closed to new replies.