I have two objects $A & $B that each contain a CSV import. Within both objects two columns exist that share the same property name ( DisplayName
& LoginName
). First of all, I want to combine those and at some point within my script pipe them to Out-GridView
.
Unfortunately, I can't seem to find a way to do that.
$A = Import-csv -Path "C:\Users\[...]" | Where-Object {$_.LoginName -like "*[...]*"} | Sort DisplayName
$B = Import-csv -Path "C:\Users\[...]" | Where-Object {$_.LoginName -like "*[...]*"} | Sort DisplayName
$displayNames = ($A.DisplayName) + ($B.DisplayName)
$loginNames = ($A.UserPrincipalName) + ($B.LoginName)
Now, at this point I can't use both $displayNames
and $loginNames
and simultaneously pipe them to Out-GridView
.
I was thinking of combining the two objects but doing $displayNames + $loginNames
results in an object that doesn't maintain the two properties.
Then I thought I could create a pscustomobject like so:
$combined = [pscustomobject] @{
DisplayNames = $displayNames
LoginNames = $loginNames
}
However, if I piped this into Out-GridView
it would show me the two columns DisplayNames
and LoginNames
but all the values would be within an array so the output would only display one line.
Then I tried to convert both arrays into strings like so:
$combined = [pscustomobject] @{
DisplayNames = ($displayNames | Out-String)
LoginNames = ($loginNames | Out-String)
}
This time, I was almost there. Out-GridView
indeed shows the expanded values in the two columns. Unfortunately, they could not be selected with the mouse anymore.
This is what it looks like:
What should I be doing differently?
Instead of using two arrays and all the nonsense of sorting them and trying to keep them in sync, plus the effort to joining them back for output, use a psobject instead with which can append the second files results to the first one. Something like this:
$results = Import-csv -Path "C:\Users\[...]" | Where-Object {$_.LoginName -like "*[...]*"} | % {
New-Object psobject -Property @{
DisplayName = $_.DisplayName
LoginName = $_.UserPrincipalName
}
}
$results += Import-csv -Path "C:\Users\[...]" | Where-Object {$_.LoginName -like "*[...]*"} | % {
New-Object psobject -Property @{
DisplayName = $_.DisplayName
LoginName = $_.LoginName
}
}
$results | Out-GridView
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.