Hello - fairly new to Powershell and working on a solution for a small subset of our users. We have mandatory periodic reboots of our workstations, performed at night. We have folks in an operations monitoring area whose work can be disrupted when we kick off the reboots (via config manager). So the idea is to give them reminders to reboot when practicable but to not force them. They choose a postpone notification time in hours from a pulldown. I have cobbled together code from different scripts I've found and have something that is halfway to working but am having trouble with a couple things.
First, my while statement - when I select a number value in hours to postpone and then click postpone, I want the form to hide or close until the hour interval happens and then it will bring up the form again. I'm trying to write a null output while the time on click is less than the time plus the number of hours chosen, but I end up immediately running the script again, which I only want to do after the interval. I also tried an if/then but same results. What it seems I really need to do is be able to sleep for the interval instead of close the script.
Second I wanted to add a description in the form and didn't know how to build it from scratch so I made a blank form in primalforms and then grabbed the label code and inserted but no matter how I adjust the x and y I can't get it to appear in the form.
Note if you run it name it timed_reboot so the variable $scriptpath works. Thanks for any advice you can offer!
[void][System.Reflection.Assembly]::LoadWithPartialName( System.Windows.Forms) [void][System.Reflection.Assembly]::LoadWithPartialName( Microsoft.VisualBasic) $form = New-Object System.Windows.Forms.Form; $form.Width = 425; $form.Height = 150; $form.Text = "Computer Reboot Notification"; $form.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen; $DropDownArray = @("1", "2", "3", "4") $DDL = New-Object System.Windows.Forms.ComboBox $DDL.Location = New-Object System.Drawing.Size(170,30) $DDL.Size = New-Object System.Drawing.Size(80,40) # sets path to script to current directory $executingScriptDirectory = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent $scriptPath = Join-Path $executingScriptDirectory "timed_reboot.ps1" $form1 = New-Object System.Windows.Forms.Form $label1 = New-Object System.Windows.Forms.Label $InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState $System_Drawing_Point = New-Object System.Drawing.Point $System_Drawing_Point.X = 130 $System_Drawing_Point.Y = 10 $label1.Location = $System_Drawing_Point $label1.Name = "label1" $System_Drawing_Size = New-Object System.Drawing.Size $System_Drawing_Size.Height = 21 $System_Drawing_Size.Width = 272 $label1.Size = $System_Drawing_Size $label1.TabIndex = 0 $label1.Text = "Please select the number of hours to delay notification" ForEach ($Item in $DropDownArray) { $DDL.Items.Add($Item) | Out-Null } $DDL.SelectedIndex = 0 $button1 = New-Object System.Windows.Forms.button; $button1.Left = 30; $button1.Top = 85; $button1.Width = 100; $button1.Text = Reboot Now; $button1.Add_Click($button1_OnClick) $button1_OnClick = {Restart-Computer -Force -ComputerName localhost} $button2 = New-Object System.Windows.Forms.button; $button2.Left = 155; $button2.Top = 85; $button2.Width = 100; $button2.Text = Postpone; $button2.Add_Click($button2_OnClick) $button2_OnClick = { $Time_On_Click = (Get-Date -Format g) $AddHours = (Get-Date -Format g).AddHours($textBox_Sender.text) while ($Time_On_Click -lt $AddHours) { Write-Output = $Null $form.close() } invoke-expression "$scriptPath" } $button3 = New-Object System.Windows.Forms.button; $button3.Left = 290; $button3.Top = 85; $button3.Width = 100; $button3.Text = Abort Shutdown; $button3.Add_Click($button3_OnClick) $button3_OnClick = {$form.Close()} $form.Controls.Add($button1); $form.Controls.Add($button2); $form.Controls.Add($button3); $form.Controls.Add($DDL); $form.Controls.Add($textLabel1) $ret = $form.ShowDialog(); $form.Controls.Add($label1) $form.KeyPreview = $True $form.Add_KeyDown({if ($_.KeyCode -eq "Enter") {$x=$textBox1.Text;$form.Close()}}) $form.Add_KeyDown({if ($_.KeyCode -eq "Escape") {$form.Close()}})