I created a .xaml file using Visual Studio to be used in Powershell. the purpose of the script i am trying to create is designed to popup for the user and have them select 1 of the provided options and hit the button. It will then schedule a restart based on the time they selected in the listbox. I have them somewhat working but the problem is that it when clicking on the button it keeps returning the same value despite the listbox changing values.
i have been running this in the Powershell ISE. the Write-Host $listBox.SelectedIndex
and Write-Host $listBox.SelectedItem
returns System.Windows.Controls.ListBoxItem: 3 Hours
and System.Windows.Controls.ListBox Items.Count:4
again despite the listbox changing values between clicks.
I know powershell decently well but the xaml i am brand new with
Powershell code
Add-Type -AssemblyName PresentationFramework
#gets the xaml information from the app.xaml, its the design of the window
[XML]$form = Get-Content "$PSScriptRoot\app.xaml"
$NR = (New-Object System.Xml.XmlNodeReader $form)
$win = [Windows.Markup.XamlReader]::Load( $NR )
function execute-restart{
[CmdletBinding()]
Param(
[Parameter(Mandatory)]
[System.Object] $listBox
)
Write-Host $listBox.SelectedIndex
Write-Host $listBox.SelectedItem
Write-Host $listBox
}
$listBox = $win.FindName('listBox')
$button = $win.FindName('restart')
#the button
$button.add_click({
execute-restart $listBox
})
$win.showdialog()
Xaml code
<Window
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Title="Your computer needs a restart" Height="450" Width="800" Icon="C:\ITTemp\PoShGuid\wallpaper.jpg" WindowStyle="None" ResizeMode="NoResize">
<Grid>
<Image Name="image" HorizontalAlignment="Left" Height="450" Margin="0" VerticalAlignment="Top" Width="800" Source="C:\ITTemp\PoShGuid\wallpaper.jpg" Stretch="Fill"/>
<Label Name="TopLabel" Content="there is a need to restart your computer" HorizontalAlignment="Left" Margin="50,23,0,0" VerticalAlignment="Top" Height="45" Width="465" FontSize="24"/>
<Label Name="BottomLabel" Content="Please select when you would like to reschedule your pc" HorizontalAlignment="Left" Margin="50,73,0,0" VerticalAlignment="Top" Width="635" Height="44" FontSize="24"/>
<ListBox Name="listBox" HorizontalAlignment="Left" Height="40" Margin="125,330,0,0" VerticalAlignment="Top" Width="256" FontSize="24" SelectionMode="Single" IsSynchronizedWithCurrentItem="True">
<ListBoxItem Name="ListBoxItem1" Content="Immediately" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
<ListBoxItem Name="ListBoxItem2" Content="1 Hour" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
<ListBoxItem Name="ListBoxItem3" Content="2 Hours" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
<ListBoxItem Name="ListBoxItem4" Content="3 Hours" FontSize="24" VerticalAlignment="Center" IsSelected="True"/>
</ListBox>
<Button Name="restart" Content="Restart In" HorizontalAlignment="Left" Margin="407,330,0,0" VerticalAlignment="Top" Width="203" Height="40"/>
</Grid>
I hope this makes sense, and i thank you for taking the time to look at my problem.