Good evening. Stack Overflow and Powershell and RegEx novice here.
The below Powershell code is returning some but not enough parts of the string to make a visual verification. It might be returning the wrong fields.
For example, it is capturing variables in a JSON file for a variable called “CallingApplication”. However I need more parts of the string.
Expected results: When I select Item1 checkbox, the below text should appear underneath the check icon and Item 1 text box. (the example CSV file shows enough characters and the results I want)
Actual Results: When I select Item1 checkbox, the below texts appears, which is incorrect or lacking
Below is the code I have so far. Any help would be appreciated. Please keep answers simple and with examples as I am a newbie. Thank you!
`#Build the GUI
$file = 'C:UsersB187515Downloadsj.json'
#Remove-Item -path C:UsersB187515Downloadstest.csv
[xml]$xaml = @"
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Name="Window" Title="Initial Window" WindowStartupLocation = "CenterScreen"
SizeToContent = "WidthAndHeight" ShowInTaskbar = "True" Background = "lightgray">
<StackPanel >
<CheckBox x:Name="Item1" Content = 'Item1' />
<TextBox x:Name='Item1_txt' />
</StackPanel>
</Window>
"@
$reader=(New-Object System.Xml.XmlNodeReader $xaml)
#Connect to Controls
$Window=[Windows.Markup.XamlReader]::Load( $reader )
$Item1 = $Window.FindName('Item1')
$Item1_txt = $Window.FindName('Item1_txt')
#Events
#Item1
$Item1.Add_Checked({
If ($Item1.IsChecked) { $CA = “CallingApplication"
Get-Content $file | Select-String -Pattern $CA -Context 2| ForEach-Object {
$_.LineNumber
$_.Line
$_.Pattern
$_.Context.PreContext
$_.Context.PostContext
$Item1_txt.Text = $.LineNumber, $.Line,$_.Context.PostContext, $CA
}
}
else {$CA ="!$!"}
})
$Item1.Add_UnChecked({
$Item1_txt.Text = "$($This.name) make this blank"
})
$Window.Showdialog() | Out-Null`
Big Smooth is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.