I'm a beginner in WPF and would like to transform my PS script in a GUI
I was possible to do this with a textblock but it seems a bit messy and therefore I want to use a DataGrid to have more structured view. I'm a bit stuck and I've searched everywhere to find a solution but I couldn't find one that suits for me.
I've tried to do this with the following:
foreach ($Property in $agfCustom){
$DataGridView1.Rows.Add.($Property)
}
I don't want any headers just rows and 2 columns with data in it.
full script below:
#-------------------------------------------------------------#
#----Initial Declarations-------------------------------------#
#-------------------------------------------------------------#
Add-Type -AssemblyName PresentationCore, PresentationFramework
$Xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" VerticalAlignment="Top" Width="75" Margin="266.17498779296875,29.987503051757812,0,0" Name="leznbuixc7f7z"/>
<TextBox HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="120" TextWrapping="Wrap" Margin="42.17498779296875,33.1875,0,0" Name="userName"/>
<DataGrid HorizontalAlignment="Left" VerticalAlignment="Top" Width="326" Height="211" Margin="375.17498779296875,103.1875,0,0" Name="DataGridView1"/>
</Grid></Window>
"@
#-------------------------------------------------------------#
#----Control Event Handlers-----------------------------------#
#-------------------------------------------------------------#
#region section
function OnClick {
# search for hostname assigned to user
$Computernames = Get-Content -Path "\\data\PC.csv" |
Select-Object -Skip 3 |
ConvertFrom-Csv -Delimiter ","
$pcNamecsv = $Computernames.Where({ $_."Details_Table0_User_Name0" -eq $userName.Text})."Details_Table0_Name0" | Where { $_ -like 'BNL0*' -or $_ -like 'BNL5*'}
$ADcomputing = @(
'Name'
'LastLogonDate'
)
$ADproperty = foreach ($PC in $pcNamecsv)
{Get-ADcomputer $PC -Properties $ADcomputing}
$pcName = ($ADproperty | Sort LastLogonDate -Descending | Select -First 1).Name
# find properties in AD
$agfProperties = @(
'Name'
'mail'
'OfficePhone'
'Description'
'PasswordLastSet'
'LastBadPasswordAttempt'
'LockedOut'
'Enabled'
'msDS-UserPasswordExpiryTimeComputed'
'Givenname'
)
$agfUser = Get-Aduser -Identity $userName.Text -Properties $agfProperties
$agfExpiry = Get-ADUser -identity $userName.Text –Properties "msDS-UserPasswordExpiryTimeComputed" |
Select-Object -Property @{Name="ExpiryDate";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}} |
select -expandproperty "ExpiryDate" -first 1
# Create CO
$agfCustom =
[pscustomobject]@{ UserID = $agfUser.Name
Mail = $agfUser.mail
Phone = $agfUser.OfficePhone
Hostname = $pcName
LockedStatusAGF = $agfUser.LockedOut
AccountEnabledAGF = $agfUser.Enabled
LastBadPasswordAttemptAGF = $agfUser.LastBadPasswordAttempt
PasswordExpiresAGF = $agfExpiry
PasswordLastSetAGF = $agfUser.PasswordLastSet
}
foreach ($Property in $agfCustom){
$DataGridView1.Rows.Add.($Property)
}
}
#endregion
#-------------------------------------------------------------#
#----Script Execution-----------------------------------------#
#-------------------------------------------------------------#
$Window = [Windows.Markup.XamlReader]::Parse($Xaml)
[xml]$xml = $Xaml
$xml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name $_.Name -Value $Window.FindName($_.Name) }
$leznbuixc7f7z.Add_Click({OnClick $this $_})
$Window.ShowDialog()
$DataGridView1.AddChild
or $DataGridView1.Items.Add
can be used to add an individual item to the DataGrid
but if you want the columns to be auto-generated, you should use an ItemsSource
.
Here is a working example:
$Xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Width="800" Height="400">
<StackPanel>
<DataGrid Name="DataGridView1" />
</StackPanel></Window>
"@
$Window = [Windows.Markup.XamlReader]::Parse($Xaml)
[xml]$xml = $Xaml
$xml.SelectNodes("//*[@Name]") | ForEach-Object { Set-Variable -Name $_.Name -Value $Window.FindName($_.Name) }
$items = @(
[pscustomobject]@{Id=1;Name='NameA'}
[pscustomobject]@{Id=2;Name='NameB'}
)
$DataGridView1.ItemsSource = $items
$Window.ShowDialog()