Formatting Custom Object with Array

Welcome Forums General PowerShell Q&A Formatting Custom Object with Array

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

 
Inactive
3 months, 2 weeks ago.

  • Author
    Posts
  • #110206

    Participant
    Points: 47
    Rank: Member

    Hi Guys.. I am running through an OU and pinging machines to see if they are ON then collecting the profiles from the computer. I am wanting to then email this information but I'm having some format issues.

    Below is my code so you can see.

    $object = @()
    
    
    
    Foreach ($Computer in $Computers){
    
    $DNSHostName = $Computer.DNSHostName
    
    If (Test-Connection $DNSHostName -Count 1 -Quiet){
    
    $Names = Get-ChildItem -Directory "\\$DNSHostName\C$\Users" -ErrorAction SilentlyContinue | Where {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue | Select Name
    
    $line = [pscustomobject]@{
    
    Hostname = $DNSHostName
    
    Profiles = $Names
    
    }
    
    $object += $line
    
    }
    
    }
    
    $object | Format-List
    
    

    Basicly, I am getting a Array of profile names and this is causing the output to look like this

    Hostname : Computer1.domain.local
    Profiles : {@{Name=User.One}, @{Name=User.Two}}
    
    Hostname : Computer2.domain.local
    Profiles : {@{Name=User.One}, @{Name=User.Two}}
    

    Is it possible to clean up the output of the profiles?

    I'd prefer it to be like this, but I don't really mind as long as its easy to read in an email.

    Computer
    Username1
    Username2

    Computer2
    Username1
    Username2

    Can anyone suggest anything?

  • #110492

    Participant
    Points: 270
    Helping Hand
    Rank: Contributor

    You could simply create a list if you want to have it as list. 😉

    Foreach ($Computer in $Computers){
        $DNSHostName = $Computer.DNSHostName
        If (Test-Connection $DNSHostName -Count 1 -Quiet){
            ' '
            $DNSHostName
            Get-ChildItem -Path "\\$DNSHostName\C$\Users" -Directory  -ErrorAction SilentlyContinue |
                Where-Object {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue | 
                    Select-Object -ExpandProperty Name
        }
    }

    You can pipe this to whatever you like

  • #110521

    Participant
    Points: 351
    Helping Hand
    Rank: Contributor

    If you're gathering profile information, you may want to consider WMI:

    $results = Foreach ($Computer in $Computers | Select -ExpandProperty DNSHostName){
        If (Test-Connection $Computer -Count 1 -Quiet){
    
            Get-CimInstance -ClassName Win32_UserProfile -Filter "Special = 'False'" | 
            Select -Property LocalPath, 
                             SID,
                             LastUseTime,
                             @{Name='UserName';Expression={($_.LocalPath -split '\\')[2]}}
        }
    }
    

    If you are going to reach out to multiple computers and grab data, you might as well grab all the data you can. This would get the path, sid, lastusetime, username. You can get just the username, like so:

    $results | Select Name, @{Name='UserName';Expression={($_.Profiles | Select -ExpandProperty UserName)}}
    
  • #110554

    Participant
    Points: 1
    Rank: Member

    try this.

    $object = @()
    Foreach ($Computer in $Computers){
    $DNSHostName = $Computer
    If (Test-Connection $DNSHostName -Count 1 -Quiet){
    $Names = (Get-ChildItem -Directory "\\$Computer\C$\Users" -ErrorAction SilentlyContinue | Where {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue). Name

    $line = [pscustomobject]@{
    Hostname = $DNSHostName
    Profiles = $Names      }
    $object += $line

    }

    } $object | Format-List

  • #111043

    Inactive
    Points: 0
    Rank: Member

    Try this:

     

    $object = @()
    
    Foreach ($Computer in $Computers){
    
    $DNSHostName = $Computer.DNSHostName
    
    If (Test-Connection $DNSHostName -Count 1 -Quiet){
    
    $Names = Get-ChildItem -Directory "\\$DNSHostName\C$\Users" -ErrorAction SilentlyContinue | Where {$_.Name -notlike "Public" -and $_.Name -notlike "Admin*"} -ErrorAction SilentlyContinue | Select -expandproperty Name
    
    Foreach ($Name in $Names)
    
    {
    
    $line = [pscustomobject]@{
    
    Hostname = $DNSHostName
    
    Profile = $Name
    
    }
    
    $object += $line
    
    }
    
    }
    
    }
    
    $object | Format-List
    

     

The topic ‘Formatting Custom Object with Array’ is closed to new replies.