Can someone help me with the formatting issue I am facing.
I am trying to pull a list of Local users (C:\users) from a remote server and generate list of not available users in Active Directory
But when I tried with below, I am getting the NA field output without separated by “;” and all the usernames are getting joined without any space.
Can someone please correct me where I went wrong…
$serverlist = gc .\Desktop\ITO.txt
$serverlist|%{
$fullList = @()
$NA = @()
$server = $_
Invoke-Command -ComputerName $server -ScriptBlock {
$users =(gci C:\Users).Name
($users|measure).count
#$users_Sevrer = gci C:\Users|Select Name, PSCOMPUTERNAME|Export-Csv -Append -Path C:\Users\Admin\desktop\output.csv -Force
$FullList += $users
$users|%{
$user = $_
try{
(Get-ADUser -Identity $user -properties SamaccountName,UserPrincipalName -ErrorAction SilentlyContinue).UserPrincipalName
}
catch{
$NA += $user
}
}
$result = [pscustomobject]@{
Server = $server
User = ($FullList) -join ";"
NA = $NA -join ";"
}
$result|select User, NA
}
}