Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # the maximum time until restart
- [int32] $MAXMINUTES = 30;
- # used for displaying the timer label
- [string] $LABEL_FMT = "Minuten bis zum Neustart (Fenster schließt in {0} Sekunden)";
- # used for displaying time until reboot on the button
- [string] $BUTTON_FMT = "Neustart in {0} Minute(n)";
- # XAML that defines the WPF GUI
- [xml] $XAML = @"
- <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- Title="Neustart dringend erforderlich" Height="200" Width="400" ResizeMode="NoResize"
- Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="None" ShowInTaskbar="False">
- <Grid>
- <Button Name="button" Content="Neustart in 30 Minuten" HorizontalAlignment="Center"
- VerticalAlignment="Bottom" Margin="30" Width="170" Height="25"/>
- <Slider Name="slider" HorizontalAlignment="Center" VerticalAlignment="Center"
- Margin="0,0,0,50" Width="300" Height="30" TickPlacement="BottomRight" Minimum="0"
- Maximum="30" Value="30"/>
- <Label Name="lbl1" Content="0" HorizontalAlignment="Center" VerticalAlignment="Center"
- Margin="0,40,290,50"/>
- <Label Name="lbl2" Content="30" HorizontalAlignment="Center" VerticalAlignment="Center"
- Margin="290,40,0,50"/>
- <Label Name="label" Content="Minuten bis zum Neustart (Fenster schließt in 60 Sekunden)"
- HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,110"/>
- </Grid>
- </Window>
- "@
- [int32] $minutesUntilRestart = $MAXMINUTES;
- [int32] $secondCount = 60;
- # Initialization
- try {
- Add-Type -AssemblyName PresentationFramework -ErrorAction Stop;
- Add-Type -AssemblyName PresentationCore -ErrorAction Stop;
- Add-Type -AssemblyName WindowsBase -ErrorAction Stop;
- Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop;
- } catch {
- Log-Error
- }
- $window = [Windows.Markup.XamlReader]::Load(( New-Object System.Xml.XmlNodeReader( $XAML ) ));
- $XAML.SelectNodes( "//*[@Name]" ) | ForEach-Object {
- Set-Variable -Name( $_.Name ) -Value $window.FindName( $_.Name ) -Scope Script;
- };
- # WPF Controls
- # Timer that counts down from 1 minute
- $timer = New-Object System.Windows.Forms.Timer;
- $timer.Interval = 1000;
- # the slider that can be used to change the desired reboot time
- $slider.Add_ValueChanged({
- $value = [int] $slider.Value;
- if ( $value -eq 0 ) {
- $button.Content = "Jetzt Neustarten";
- } else {
- $button.Content = $BUTTON_FMT -f $value;
- }
- $script:minutesUntilRestart = $value;
- })
- # the 'accept' button
- $button.Add_Click({
- $script:window.Close();
- })
- # WPF window functionality
- $window.Add_Closed({
- $script:timer.Stop();
- $script:timer.Dispose();
- if ( $script:minutesUntilRestart -eq 0 ) {
- Restart-Computer -Force;
- } else {
- Schedule-PSTask $REBOOT_TASK $REBOOT_CMD $script:minutesUntilRestart;
- }
- })
- $window.Add_Loaded({
- $script:timer.Add_Tick({
- $newcount = $script:secondCount - 1;
- $label.Content = $LABEL_FMT -f $newcount;
- $script:secondCount = $newcount;
- if ( $newcount -le 0 ){
- $script:window.Close();
- }
- })
- $script:timer.Start();
- })
- # source Log-Error and Schedule-PSTask
- . "$PSScriptRoot\Functions.ps1"
- # this schedules a reboot with the Task Scheduler
- Schedule-PSTask $REBOOT_TASK $REBOOT_CMD $MAXMINUTES;
- $window.ShowDialog() | Out-Null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement