All functions of the gui are working as expected, except one, after the user postpons the start of migration 3 times the postpone button and start button should disappear and start countdown should begin and display countdown of example 10 minutes, but the countdown timer is not displaying
Add-Type -AssemblyName PresentationFramework
function Show-GUI {
[xml]$xaml = @"
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Migration Tool" Height="450" Width="400" WindowStartupLocation="CenterScreen" Background="LightGray">
<Grid Margin="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition Height="*"/>
<RowDefinition Height="Auto"/>
<RowDefinition Height="Auto"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>
<Image Source="C:MigrationGUI_LayoutsPic1.png" Width="100" Height="100" HorizontalAlignment="Center" Grid.Row="0"/>
<TextBox Name="MessageTextBox" TextWrapping="Wrap" IsReadOnly="True" VerticalScrollBarVisibility="Auto" Margin="0,10,0,10" Grid.Row="1"/>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Grid.Row="2">
<ProgressBar Name="ProgressBar" Height="20" Width="300" Minimum="0" Maximum="100" Value="0" Visibility="Collapsed" Margin="0,10,0,10"/>
<TextBlock Name="CountdownTextBlock" Visibility="Collapsed" Foreground="Green" FontSize="16" HorizontalAlignment="Center"/>
</StackPanel>
<StackPanel Orientation="Vertical" HorizontalAlignment="Center" Grid.Row="3" Margin="0,0,0,10">
<Button Name="StartButton" Content="Start" Width="100" Margin="5"/>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center">
<Button Name="PostponeButton" Content="Postpone" Width="70" Height="22" Margin="6"/>
<ComboBox Name="TimeComboBox" Width="76" Margin="6">
<ComboBoxItem Content="15 Minutes" Tag="15"/>
<ComboBoxItem Content="30 Minutes" Tag="30"/>
<ComboBoxItem Content="1 Hour" Tag="60"/>
</ComboBox>
</StackPanel>
<Button Name="RestartButton" Content="Restart" Width="100" Margin="5" Visibility="Collapsed"/>
</StackPanel>
<Image Source="C:MigrationGUI_LayoutsPic2.png" Width="100" Height="100" HorizontalAlignment="Center" Grid.Row="4"/>
</Grid>
</Window>
"@
$reader = (New-Object System.Xml.XmlNodeReader $xaml)
$window = [Windows.Markup.XamlReader]::Load($reader)
$window.FindName("MessageTextBox").Text = "Welcome to the Migration Tool"
$startButton = $window.FindName("StartButton")
$postponeButton = $window.FindName("PostponeButton")
$timeComboBox = $window.FindName("TimeComboBox")
$progressBar = $window.FindName("ProgressBar")
$countdownTextBlock = $window.FindName("CountdownTextBlock")
$restartButton = $window.FindName("RestartButton")
$messageTextBox = $window.FindName("MessageTextBox")
$postponeCount = 0
$timer = New-Object System.Timers.Timer
$timer.Interval = 1000
$timer.AutoReset = $true
$timer.Enabled = $false
Register-ObjectEvent -InputObject $timer -EventName Elapsed -SourceIdentifier TimerElapsed -Action {
[System.Windows.Application]::Current.Dispatcher.Invoke({
$global:countdownTime--
$countdownTextBlock.Text = "Starting in $global:countdownTime seconds..."
if ($global:countdownTime -le 0) {
$timer.Stop()
$countdownTextBlock.Text = "Starting..."
if (Check-Prerequisites) {
Start-Process
}
}
})
}
$startButton.Add_Click({
if (Check-Prerequisites) {
Start-Process
}
})
$postponeButton.Add_Click({
$selectedItem = $timeComboBox.SelectedItem
if ($selectedItem -ne $null) {
$postponeMinutes = [int]$selectedItem.Tag
Postpone $postponeMinutes
} else {
$messageTextBox.Text = "Please select a postpone time."
}
})
$restartButton.Add_Click({
Restart-Computer
})
function Postpone($minutes) {
$postponeCount++
if ($postponeCount -ge 3) {
Hide-Buttons
Start-Countdown
} else {
$window.Hide()
#Start-Sleep -Seconds ($minutes * 60)
Start-Sleep -Seconds ($minutes * 1)
$window.ShowDialog() | Out-Null
}
}
function Hide-Buttons {
$startButton.Visibility = 'Collapsed'
$postponeButton.Visibility = 'Collapsed'
$timeComboBox.Visibility = 'Collapsed'
$messageTextBox.Visibility = 'Collapsed'
}
function Start-Countdown {
$countdownTextBlock.Visibility = 'Visible'
$global:countdownTime = 600
$timer.Start()
}
function Check-Prerequisites {
$diskSpace = Get-PSDrive -PSProvider FileSystem | Where-Object {$_.Free -gt 5GB}
if ($diskSpace -eq $null) {
$messageTextBox.Text = "Error: Not enough disk space."
return $false
}
$osVersion = [System.Environment]::OSVersion.Version
$latestVersion = [Version]"10.0.19041.0" # Example latest version
if ($osVersion -lt $latestVersion) {
$messageTextBox.Text = "Error: Windows version is not the latest."
return $false
}
$internet = Test-Connection -ComputerName google.com -Count 1 -Quiet
if (-not $internet) {
$messageTextBox.Text = "Error: No internet connection."
return $false
}
return $true
}
function Start-Process {
$progressBar.Visibility = 'Visible'
$progressBar.Value = 0
$steps = 4
for ($i = 1; $i -le $steps; $i++) {
Start-Sleep -Seconds 2 # Simulate work being done
$progressBar.Value = ($i / $steps) * 100
$messageTextBox.Text = "Step $i of $steps completed."
}
$messageTextBox.Text = "Migration completed, please reboot your computer."
$progressBar.Value = 100
$restartButton.Visibility = 'Visible'
}
$window.ShowDialog() | Out-Null
}
Show-GUI
All functions of the gui are working as expected, except one, after the user postpons the start of migration 3 times the postpone button and start button should disappear and start countdown should begin and display countdown of example 10 minutes, but the countdown timer is not displaying
Tanoj Mahiskar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.