Skip to content

Commit 2f15cf5

Browse files
WIP add: Win_Reboot_usingIdleandUptime.ps1 script for rebooting device
1 parent f801441 commit 2f15cf5

1 file changed

Lines changed: 351 additions & 0 deletions

File tree

Lines changed: 351 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,351 @@
1+
#Reboot Device Upon The User’s Preferences: Wait, reboot at 18:00 or reboot now. The prompt mesage and colors can be changed upon your choice
2+
3+
4+
$days = 7
5+
$system = Get-WmiObject win32_operatingsystem
6+
7+
if($system.ConvertToDateTime($system.LastBootUpTime) -lt (Get-Date).AddDays(-$days)){
8+
#----------------------------------------------
9+
#region Import Assemblies
10+
#----------------------------------------------
11+
[void][Reflection.Assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
12+
[void][Reflection.Assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
13+
[void][Reflection.Assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
14+
#endregion Import Assemblies
15+
16+
17+
#Define a Param block to use custom parameters in the project
18+
#Param ($CustomParameter)
19+
20+
function Main {
21+
<#
22+
.SYNOPSIS
23+
The Main function starts the project application.
24+
25+
.PARAMETER Commandline
26+
$Commandline contains the complete argument string passed to the script packager executable.
27+
28+
.NOTES
29+
Use this function to initialize your script and to call GUI forms.
30+
31+
.NOTES
32+
To get the console output in the Packager (Forms Engine) use:
33+
$ConsoleOutput (Type: System.Collections.ArrayList)
34+
#>
35+
Param ([String]$Commandline)
36+
37+
#--------------------------------------------------------------------------
38+
#TODO: Add initialization script here (Load modules and check requirements)
39+
40+
41+
#--------------------------------------------------------------------------
42+
43+
if((Call-MainForm_psf) -eq 'OK')
44+
{
45+
46+
}
47+
48+
$global:ExitCode = 0 #Set the exit code for the Packager
49+
}
50+
51+
52+
53+
54+
55+
56+
57+
#endregion Source: Startup.pss
58+
59+
#region Source: MainForm.psf
60+
function Call-MainForm_psf
61+
{
62+
63+
#----------------------------------------------
64+
#region Import the Assemblies
65+
#----------------------------------------------
66+
[void][reflection.assembly]::Load('System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
67+
[void][reflection.assembly]::Load('System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
68+
[void][reflection.assembly]::Load('System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
69+
#endregion Import Assemblies
70+
71+
#----------------------------------------------
72+
#region Generated Form Objects
73+
#----------------------------------------------
74+
[System.Windows.Forms.Application]::EnableVisualStyles()
75+
$MainForm = New-Object 'System.Windows.Forms.Form'
76+
$panel2 = New-Object 'System.Windows.Forms.Panel'
77+
$ButtonCancel = New-Object 'System.Windows.Forms.Button'
78+
$ButtonSchedule = New-Object 'System.Windows.Forms.Button'
79+
$ButtonRestartNow = New-Object 'System.Windows.Forms.Button'
80+
$panel1 = New-Object 'System.Windows.Forms.Panel'
81+
$labelITSystemsMaintenance = New-Object 'System.Windows.Forms.Label'
82+
$labelSecondsLeftToRestart = New-Object 'System.Windows.Forms.Label'
83+
$labelTime = New-Object 'System.Windows.Forms.Label'
84+
$labelInOrderToApplySecuri = New-Object 'System.Windows.Forms.Label'
85+
$timerUpdate = New-Object 'System.Windows.Forms.Timer'
86+
$InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
87+
#endregion Generated Form Objects
88+
89+
#----------------------------------------------
90+
# User Generated Script
91+
#----------------------------------------------
92+
$TotalTime = 1500 #in seconds
93+
94+
$MainForm_Load={
95+
#TODO: Initialize Form Controls here
96+
$labelTime.Text = "{0:D2}" -f $TotalTime #$TotalTime
97+
#Add TotalTime to current time
98+
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
99+
#Start the timer
100+
$timerUpdate.Start()
101+
}
102+
103+
104+
$timerUpdate_Tick={
105+
# Define countdown timer
106+
[TimeSpan]$span = $script:StartTime - (Get-Date)
107+
#Update the display
108+
$labelTime.Text = "{0:N0}" -f $span.TotalSeconds
109+
$timerUpdate.Start()
110+
if ($span.TotalSeconds -le 0)
111+
{
112+
$timerUpdate.Stop()
113+
Restart-Computer -Force
114+
}
115+
116+
}
117+
118+
$ButtonRestartNow_Click = {
119+
# Restart the computer immediately
120+
Restart-Computer -Force
121+
}
122+
123+
$ButtonSchedule_Click={
124+
# Schedule restart for 6pm
125+
if(Get-ScheduledTask -TaskName "auto shutdown my computer" -ErrorAction SilentlyContinue){Get-ScheduledTask -TaskName "auto shutdown my computer" | Unregister-ScheduledTask -Confirm:$false}
126+
if((schtasks /create /sc once /tn "auto shutdown my computer" /tr "shutdown /r /d p:1:1 /c 'Initiating reboot since the device has not been rebooted for 7 days'" /st 18:00) -like "*Success*"){
127+
$SetT=Get-ScheduledTask -TaskName "auto shutdown my computer"
128+
$SetT.Triggers[0].EndBoundary=[DateTime]::Now.Date.ToString("yyyy-MM-dd")+"T"+"19:00:00"
129+
$SetT.Settings.DeleteExpiredTaskAfter ='PT0S'
130+
Set-ScheduledTask -InputObject $SetT
131+
}
132+
$MainForm.Close()
133+
}
134+
135+
$ButtonCancel_Click={
136+
#TODO: Place custom script here
137+
$MainForm.Close()
138+
}
139+
140+
$labelITSystemsMaintenance_Click={
141+
#TODO: Place custom script here
142+
143+
}
144+
145+
$panel2_Paint=[System.Windows.Forms.PaintEventHandler]{
146+
#Event Argument: $_ = [System.Windows.Forms.PaintEventArgs]
147+
#TODO: Place custom script here
148+
149+
}
150+
151+
$labelTime_Click={
152+
#TODO: Place custom script here
153+
154+
}
155+
# --End User Generated Script--
156+
#----------------------------------------------
157+
#region Generated Events
158+
#----------------------------------------------
159+
160+
$Form_StateCorrection_Load=
161+
{
162+
#Correct the initial state of the form to prevent the .Net maximized form issue
163+
$MainForm.WindowState = $InitialFormWindowState
164+
}
165+
166+
$Form_StoreValues_Closing=
167+
{
168+
#Store the control values
169+
}
170+
171+
172+
$Form_Cleanup_FormClosed=
173+
{
174+
#Remove all event handlers from the controls
175+
try
176+
{
177+
$ButtonCancel.remove_Click($buttonCancel_Click)
178+
$ButtonSchedule.remove_Click($ButtonSchedule_Click)
179+
$ButtonRestartNow.remove_Click($ButtonRestartNow_Click)
180+
$panel2.remove_Paint($panel2_Paint)
181+
$labelITSystemsMaintenance.remove_Click($labelITSystemsMaintenance_Click)
182+
$labelTime.remove_Click($labelTime_Click)
183+
$MainForm.remove_Load($MainForm_Load)
184+
$timerUpdate.remove_Tick($timerUpdate_Tick)
185+
$MainForm.remove_Load($Form_StateCorrection_Load)
186+
$MainForm.remove_Closing($Form_StoreValues_Closing)
187+
$MainForm.remove_FormClosed($Form_Cleanup_FormClosed)
188+
}
189+
catch [Exception]
190+
{ }
191+
}
192+
#endregion Generated Events
193+
194+
#----------------------------------------------
195+
#region Generated Form Code
196+
#----------------------------------------------
197+
$MainForm.SuspendLayout()
198+
$panel2.SuspendLayout()
199+
$panel1.SuspendLayout()
200+
#
201+
# MainForm
202+
#
203+
$MainForm.Controls.Add($panel2)
204+
$MainForm.Controls.Add($panel1)
205+
$MainForm.Controls.Add($labelSecondsLeftToRestart)
206+
$MainForm.Controls.Add($labelTime)
207+
$MainForm.Controls.Add($labelInOrderToApplySecuri)
208+
$MainForm.AutoScaleDimensions = '6, 13'
209+
$MainForm.AutoScaleMode = 'Font'
210+
$MainForm.BackColor = 'White'
211+
$MainForm.ClientSize = '373, 279'
212+
$MainForm.MaximizeBox = $False
213+
$MainForm.MinimizeBox = $False
214+
$MainForm.Name = 'MainForm'
215+
$MainForm.ShowIcon = $False
216+
$MainForm.ShowInTaskbar = $False
217+
$MainForm.StartPosition = 'CenterScreen'
218+
$MainForm.Text = 'MSP Name'
219+
$MainForm.TopMost = $True
220+
$MainForm.add_Load($MainForm_Load)
221+
#
222+
# panel2
223+
#
224+
$panel2.Controls.Add($ButtonCancel)
225+
$panel2.Controls.Add($ButtonSchedule)
226+
$panel2.Controls.Add($ButtonRestartNow)
227+
$panel2.BackColor = 'ScrollBar'
228+
$panel2.Location = '0, 205'
229+
$panel2.Name = 'panel2'
230+
$panel2.Size = '378, 80'
231+
$panel2.TabIndex = 9
232+
$panel2.add_Paint($panel2_Paint)
233+
#
234+
# ButtonCancel
235+
#
236+
$ButtonCancel.Location = '250, 17'
237+
$ButtonCancel.Name = 'ButtonCancel'
238+
$ButtonCancel.Size = '77, 45'
239+
$ButtonCancel.TabIndex = 7
240+
$ButtonCancel.Text = 'Wait'
241+
$ButtonCancel.UseVisualStyleBackColor = $True
242+
$ButtonCancel.add_Click($buttonCancel_Click)
243+
#
244+
# ButtonSchedule
245+
#
246+
$ButtonSchedule.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
247+
$ButtonSchedule.Location = '139, 17'
248+
$ButtonSchedule.Name = 'ButtonSchedule'
249+
$ButtonSchedule.Size = '105, 45'
250+
$ButtonSchedule.TabIndex = 6
251+
$ButtonSchedule.Text = 'Reboot at 18:00'
252+
$ButtonSchedule.UseVisualStyleBackColor = $True
253+
$ButtonSchedule.add_Click($ButtonSchedule_Click)
254+
#
255+
# ButtonRestartNow
256+
#
257+
$ButtonRestartNow.Font = 'Microsoft Sans Serif, 8.25pt, style=Bold'
258+
$ButtonRestartNow.ForeColor = 'DarkRed'
259+
$ButtonRestartNow.Location = '42, 17'
260+
$ButtonRestartNow.Name = 'ButtonRestartNow'
261+
$ButtonRestartNow.Size = '91, 45'
262+
$ButtonRestartNow.TabIndex = 0
263+
$ButtonRestartNow.Text = 'Reboot'
264+
$ButtonRestartNow.UseVisualStyleBackColor = $True
265+
$ButtonRestartNow.add_Click($ButtonRestartNow_Click)
266+
#
267+
# panel1
268+
#
269+
$panel1.Controls.Add($labelITSystemsMaintenance)
270+
$panel1.BackColor = '22, 54, 36'
271+
$panel1.Location = '0, 0'
272+
$panel1.Name = 'panel1'
273+
$panel1.Size = '375, 67'
274+
$panel1.TabIndex = 8
275+
#
276+
# labelITSystemsMaintenance
277+
#
278+
$labelITSystemsMaintenance.Font = 'Microsoft Sans Serif, 14.25pt'
279+
$labelITSystemsMaintenance.ForeColor = 'White'
280+
$labelITSystemsMaintenance.Location = '11, 18'
281+
$labelITSystemsMaintenance.Name = 'labelITSystemsMaintenance'
282+
$labelITSystemsMaintenance.Size = '269, 23'
283+
$labelITSystemsMaintenance.TabIndex = 1
284+
$labelITSystemsMaintenance.Text = 'MSP Name'
285+
$labelITSystemsMaintenance.TextAlign = 'MiddleLeft'
286+
$labelITSystemsMaintenance.add_Click($labelITSystemsMaintenance_Click)
287+
#
288+
# labelSecondsLeftToRestart
289+
#
290+
$labelSecondsLeftToRestart.AutoSize = $True
291+
$labelSecondsLeftToRestart.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
292+
$labelSecondsLeftToRestart.Location = '87, 176'
293+
$labelSecondsLeftToRestart.Name = 'labelSecondsLeftToRestart'
294+
$labelSecondsLeftToRestart.Size = '155, 15'
295+
$labelSecondsLeftToRestart.TabIndex = 5
296+
$labelSecondsLeftToRestart.Text = 'Seconds to reboot :'
297+
#
298+
# labelTime
299+
#
300+
$labelTime.AutoSize = $True
301+
$labelTime.Font = 'Microsoft Sans Serif, 9pt, style=Bold'
302+
$labelTime.ForeColor = '192, 0, 0'
303+
$labelTime.Location = '237, 176'
304+
$labelTime.Name = 'labelTime'
305+
$labelTime.Size = '43, 15'
306+
$labelTime.TabIndex = 3
307+
$labelTime.Text = '00:60'
308+
$labelTime.TextAlign = 'MiddleCenter'
309+
$labelTime.add_Click($labelTime_Click)
310+
#
311+
# labelInOrderToApplySecuri
312+
#
313+
$labelInOrderToApplySecuri.Font = 'Microsoft Sans Serif, 9pt'
314+
$labelInOrderToApplySecuri.Location = '12, 84'
315+
$labelInOrderToApplySecuri.Name = 'labelInOrderToApplySecuri'
316+
$labelInOrderToApplySecuri.Size = '350, 83'
317+
$labelInOrderToApplySecuri.TabIndex = 2
318+
$labelInOrderToApplySecuri.Text = 'Every 7 days your PC should be restarted for maintenance and updates.
319+
320+
If this does not fit, you can press wait or restart at. 6:00 p.m.'
321+
#
322+
# timerUpdate
323+
#
324+
$timerUpdate.add_Tick($timerUpdate_Tick)
325+
$panel1.ResumeLayout()
326+
$panel2.ResumeLayout()
327+
$MainForm.ResumeLayout()
328+
#endregion Generated Form Code
329+
330+
#----------------------------------------------
331+
332+
#Save the initial state of the form
333+
$InitialFormWindowState = $MainForm.WindowState
334+
#Init the OnLoad event to correct the initial state of the form
335+
$MainForm.add_Load($Form_StateCorrection_Load)
336+
#Clean up the control events
337+
$MainForm.add_FormClosed($Form_Cleanup_FormClosed)
338+
#Store the control values when form is closing
339+
$MainForm.add_Closing($Form_StoreValues_Closing)
340+
#Show the Form
341+
return $MainForm.ShowDialog()
342+
343+
}
344+
#endregion Source: MainForm.psf
345+
346+
#Start the application
347+
Main ($CommandLine)
348+
}else{
349+
Write-Host "Machine was rebooted less than $days days ago"
350+
351+
}

0 commit comments

Comments
 (0)