Advertisement
Guest User

Untitled

a guest
Apr 24th, 2017
392
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # the maximum time until restart
  2. [int32] $MAXMINUTES = 30;
  3. # used for displaying the timer label
  4. [string] $LABEL_FMT = "Minuten bis zum Neustart (Fenster schließt in {0} Sekunden)";
  5. # used for displaying time until reboot on the button
  6. [string] $BUTTON_FMT = "Neustart in {0} Minute(n)";
  7. # XAML that defines the WPF GUI
  8. [xml] $XAML = @"
  9. <Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  10.        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  11.        Title="Neustart dringend erforderlich" Height="200" Width="400" ResizeMode="NoResize"
  12.        Topmost="True" WindowStartupLocation="CenterScreen" WindowStyle="None" ShowInTaskbar="False">
  13.    <Grid>
  14.        <Button Name="button" Content="Neustart in 30 Minuten" HorizontalAlignment="Center"
  15.            VerticalAlignment="Bottom" Margin="30" Width="170" Height="25"/>
  16.        <Slider Name="slider" HorizontalAlignment="Center" VerticalAlignment="Center"
  17.            Margin="0,0,0,50" Width="300" Height="30" TickPlacement="BottomRight" Minimum="0"
  18.            Maximum="30" Value="30"/>
  19.        <Label Name="lbl1" Content="0" HorizontalAlignment="Center" VerticalAlignment="Center"
  20.            Margin="0,40,290,50"/>
  21.        <Label Name="lbl2" Content="30" HorizontalAlignment="Center" VerticalAlignment="Center"
  22.            Margin="290,40,0,50"/>
  23.        <Label Name="label" Content="Minuten bis zum Neustart (Fenster schließt in 60 Sekunden)"
  24.            HorizontalAlignment="Center" VerticalAlignment="Center" Margin="10,0,0,110"/>
  25.    </Grid>
  26. </Window>
  27. "@
  28.  
  29. [int32] $minutesUntilRestart = $MAXMINUTES;
  30. [int32] $secondCount = 60;
  31.  
  32. # Initialization
  33. try {
  34.     Add-Type -AssemblyName PresentationFramework -ErrorAction Stop;
  35.     Add-Type -AssemblyName PresentationCore -ErrorAction Stop;
  36.     Add-Type -AssemblyName WindowsBase -ErrorAction Stop;
  37.     Add-Type -AssemblyName System.Windows.Forms -ErrorAction Stop;
  38. } catch {
  39.     Log-Error
  40. }
  41.  
  42. $window = [Windows.Markup.XamlReader]::Load(( New-Object System.Xml.XmlNodeReader( $XAML ) ));
  43.  
  44. $XAML.SelectNodes( "//*[@Name]" ) | ForEach-Object {
  45.     Set-Variable -Name( $_.Name ) -Value $window.FindName( $_.Name ) -Scope Script;
  46. };
  47.  
  48. # WPF Controls
  49.  
  50. # Timer that counts down from 1 minute
  51. $timer = New-Object System.Windows.Forms.Timer;
  52. $timer.Interval = 1000;
  53.  
  54. # the slider that can be used to change the desired reboot time
  55. $slider.Add_ValueChanged({
  56.     $value = [int] $slider.Value;
  57.     if ( $value -eq 0 ) {
  58.         $button.Content = "Jetzt Neustarten";
  59.     } else {
  60.         $button.Content = $BUTTON_FMT -f $value;
  61.     }
  62.     $script:minutesUntilRestart = $value;
  63. })
  64.  
  65. # the 'accept' button
  66. $button.Add_Click({
  67.     $script:window.Close();
  68. })
  69.  
  70. # WPF window functionality
  71. $window.Add_Closed({
  72.     $script:timer.Stop();
  73.     $script:timer.Dispose();
  74.     if ( $script:minutesUntilRestart -eq 0 ) {
  75.         Restart-Computer -Force;
  76.     } else {
  77.         Schedule-PSTask $REBOOT_TASK $REBOOT_CMD $script:minutesUntilRestart;
  78.     }
  79. })
  80.  
  81. $window.Add_Loaded({
  82.     $script:timer.Add_Tick({
  83.         $newcount = $script:secondCount - 1;
  84.         $label.Content = $LABEL_FMT -f $newcount;
  85.         $script:secondCount = $newcount;
  86.         if ( $newcount -le 0 ){
  87.             $script:window.Close();
  88.         }
  89.     })
  90.     $script:timer.Start();
  91. })
  92.  
  93. # source Log-Error and Schedule-PSTask
  94. . "$PSScriptRoot\Functions.ps1"
  95.  
  96. # this schedules a reboot with the Task Scheduler
  97. Schedule-PSTask $REBOOT_TASK $REBOOT_CMD $MAXMINUTES;
  98. $window.ShowDialog() | Out-Null;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement