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

  • This topic has 5 replies, 4 voices, and was last updated 6 months ago by
    js
    Participant
    .
Viewing 5 reply threads
  • Author
    Posts
    • #170851
      Participant
      Topics: 21
      Replies: 37
      Points: 17
      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: 5
      Replies: 288
      Points: 229
      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: 17
      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: 2
      Replies: 1736
      Points: 3,552
      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: 17
      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: 28
      Replies: 751
      Points: 2,094
      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
      
Viewing 5 reply threads
  • The topic ‘Issues with get-content inside select-object’ is closed to new replies.