简体   繁体   English

无法使Out-GridView显示两个对象

[英]Can't get Out-GridView to display two objects

I have two objects $A & $B that each contain a CSV import. 我有两个对象$ A和$ B,每个对象都包含CSV导入。 Within both objects two columns exist that share the same property name ( DisplayName & LoginName ). 在这两个对象中,存在共享相同属性名称( DisplayNameLoginName )的两列。 First of all, I want to combine those and at some point within my script pipe them to Out-GridView . 首先,我想将它们组合在一起,并在脚本中的某些时候将它们通过管道传递到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 . 现在,此时我不能同时使用$displayNames$loginNames并将它们同时通过管道传输到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. 我正在考虑合并两个对象,但是执行$displayNames + $loginNames导致一个对象不维护这两个属性。

Then I thought I could create a pscustomobject like so: 然后我想我可以像这样创建一个pscustomobject:

$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. 但是,如果将其通过管道传输到Out-GridView ,它将向我显示两列DisplayNamesLoginNames但是所有值都将在数组内,因此输出将仅显示一行。

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. 实际上, Out-GridView在两列中显示了展开的值。 Unfortunately, they could not be selected with the mouse anymore. 不幸的是,无法再用鼠标选择它们。

This is what it looks like: 看起来是这样的:

在此处输入图片说明

What should I be doing differently? 我应该怎么做?

This may work for you: 这可能对您有用:

$displayNames = @('aaa','bbb','ccc'); 
$loginNames=@('xxx','yyy','zzz'); 
($displayNames | select @{n='NameType';e={'display'}},@{n='Name';e={$_}}) + 
($loginNames | select @{n='NameType';e={'login'}},@{n='Name';e={$_}}) | 
Out-GridView

在此处输入图片说明

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. 而不是使用两个数组以及将它们排序并试图使其保持同步的所有废话,再加上将它们联接回以进行输出的工作,而是使用psobject,它可以将第二个文件的结果附加到第一个文件。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM