Issues with get-content inside select-object

Welcome Forums General PowerShell Q&A Issues with get-content inside select-object

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

js
 
Participant
1 month ago.

  • Author
    Posts
  • #170851

    Participant
    Topics: 21
    Replies: 37
    Points: 16
    Rank: Member

    I have a share containing a text file per machine with the name of the last logged on user. I wanted to add that information to my search. The get-content works fine when running alone. It is only returning a single string as checked with trim and length. What am I doing wrong?

     

    Get-ADComputer -Filter {operatingsystem -like "*7*" -and enabled -eq $true} -Properties Name,Operatingsystem,lastlogondate,distinguishedname |
    Select-Object Name,OperatingSystem,LastLogonDate,distinguishedname,@{name='User';Expression={
    Get-Content -Path "\\server\share\computers\$($_.name).txt" -Tail 1 }} |
    Where-Object {$_.LastlogonDate -gt $((Get-date).AddDays(-30))}

     

     

  • #170989

    Participant
    Topics: 4
    Replies: 269
    Points: 115
    Helping Hand
    Rank: Participant

    Try this.

    Get-ADComputer -Filter {operatingsystem -like "*7*" -and enabled -eq $true} -Properties Name,
    Operatingsystem,lastlogondate,distinguishedname |
    Where-Object {$_.LastlogonDate -gt $((Get-date).AddDays(-30))} | 
    Select-Object Name,OperatingSystem,LastLogonDate,distinguishedname,
    @{name='User';Expression={Get-Content -Path "\\server\share\computers\$($_.name).txt" -Tail 1 }}
    
  • #171043

    Participant
    Topics: 21
    Replies: 37
    Points: 16
    Rank: Member

    Thanks for your reply but I still have the same issue. For some reason the get-content part will not run inside of the select object.

  • #171046

    Participant
    Topics: 1
    Replies: 1528
    Points: 2,581
    Helping Hand
    Rank: Community Hero

    ... will not run ...

    What exactly does that mean? Do you get an error? Do you get nothing at all? Did you try to output the path you're building just to check if it is what you expect. Do these paths really exist? "... will not run ..." is pretty unprofessional. 😉

  • #171049

    Participant
    Topics: 21
    Replies: 37
    Points: 16
    Rank: Member

    Sorry for the short answer I agree it was not very informative. If I run the get-content command on its own I get the results supplementing the computername for a chosen device that would of appeared in the results. However when I run it combined the user field is blank. I have tried running trim and length commands and the string returned by the single command should be easily displayed.

  • #171085
    js

    Participant
    Topics: 24
    Replies: 677
    Points: 1,613
    Helping Hand
    Rank: Community Hero

    What's an example of the output you're getting? It seems like it should work.

    echo 'one
    two' > joe.txt
    
    [pscustomobject]@{name='joe'; lastlogondate=$((Get-date).AddDays(-29))} | 
    select-object lastlogondate,
      @{name='user';e={get-content -path "$($_.name).txt" -tail 1}} | 
    Where-Object {$_.LastlogonDate -gt $((Get-date).AddDays(-30))}
    
    lastlogondate      user
    -------------      ----
    7/18/19 8:28:46 AM two
    

You must be logged in to reply to this topic.