Author Posts

August 28, 2018 at 11:34 pm

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?

August 30, 2018 at 12:47 pm

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

August 30, 2018 at 3:36 pm

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)}}

August 30, 2018 at 7:04 pm

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

September 5, 2018 at 4:59 pm

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