将 System.Object 加载到 DataGrid

Load System.Object into DataGrid

我正在尝试从 SCCM 获取一些数据并将此信息放入数据网格中。我的数据网格的代码如下所示:

[xml]$xaml = @"
<Window 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Name="Window_GuiManagement" Title="SCCM" WindowStartupLocation = "CenterScreen" 
    Width = "587.000" Height = "500.000" Visibility="Visible" WindowStyle="ToolWindow" ResizeMode="NoResize" Topmost="True">
<Grid>
    <DataGrid x:Name="datagrid_result" ItemsSource="{Binding}" Margin="11,10,10,0" VerticalAlignment="Top" Height="299" SelectionMode="Single" AlternationCount="1">
    </DataGrid>
</Grid>
</Window>
"@ 

我抓到的SCCM信息是这样的:

$data = Get-CMDevice | Where-Object {
    $_.LastLogonUser -like '*Walker*'
} | Select Name 

我试过了

$datagrid_result.ItemsSource = $data

结果是 System.Object,与 运行 时得到的结果相同。 Get-Culture | Select Name 但会导致错误:

Exception setting "ItemsSource": "Cannot convert the "@{Name=PCName}" value of type "Selected.Microsoft.ConfigurationManagement.ManagementProvider.WqlQueryEngine.WqlResultObject" to type "System.Collections.IEnumerable"."

最后我想要的是特定用户最近登录的所有计算机的列表。

Select Name更改为Select -Expand Name并将$data的类型更改为[string[]]:

[string[]]$data = Get-CMDevice | Where-Object {
    $_.LastLogonUser -like '*Walker*'
} | Select -Expand Name 

这样您将分配一个字符串数组作为 ItemSource(它将接受),而不是单个 WQL 查询结果对象