Search code examples
wpfvisual-studiopowershellvariablesitemssource

Show output in DataGridView from Powershell Script


I want to show output in DataGridView that runs from a powershell script:


$domain1Groups     = (Get-ADuser -identity $var_userName.Text -server domain1 -Properties MemberOf).MemberOf -Replace 'CN=', '' -Replace ',.*', ''
$domain2Groups     = (Get-ADuser -identity $var_userName.Text -server domain2 -Properties MemberOf).MemberOf -Replace 'CN=', '' -Replace ',.*', ''
$domain3Groups     = (Get-ADuser -identity $var_userName.Text -server domain3 -Properties MemberOf).MemberOf -Replace 'CN=', '' -Replace ',.*', ''



with this script I get a list in powershell with 1 column with all memberships from that domain.

Tried with .ItemsSource but I cannot see anything in the DataGridView


$var_comboBox.Add_SelectionChanged({



    $item = $var_comboBox.SelectedItem.Content

        if ($item -eq "domain1")
            {$var_DataGridView.ItemsSource = $domain1Groups




    }elseif
           ($item -eq "domain2" )
            {$var_DataGridView.ItemsSource = "$domain2Groups"

}



      else{
           ($item -eq "domain3" )
            $var_DataGridView.ItemSource = "$domain3Groups"

}})

I also want a customized Column that displays the text "Member Of"

Solution

  • If you define the DataGrid like this:

    <DataGrid Name="dataGrid" AutoGenerateColumns="False">
        <DataGrid.Columns>
            <DataGridTextColumn Header="Member Of" Binding="{Binding}" IsReadOnly="True" />
        </DataGrid.Columns>
    </DataGrid>
    

    ...you should be able to set the ItemsSource property like this:

    $domain1Groups = (Get-ADuser ...
    $dataGrid.ItemsSource = $domain1Groups