关于自定义 powershell 重启脚本的查询

关于自定义 powershell 重启脚本的查询

全部,

我有一个 PS 脚本(准确地说是借来的,我不是程序员)。此脚本用于使用“立即重启”、“4 小时后重启”和“8 小时后重启”这些选项重启系统。问题是我不知道如何在标题栏中包含“最小化”选项,我不希望最终用户退出,所以有人定制了这个框,可惜删除了标题中的所有内容(没有最小化,没有退出)。请问我可以请求某人帮忙吗?提前谢谢。

剧本:

Function Create-GetSchedTime {
Param(
$SchedTime
)
$script:StartTime = (Get-Date).AddSeconds($TotalTime)
$RestartDate = ((get-date).AddSeconds($TotalTime)).AddMinutes(-1)
$RDate = (Get-Date $RestartDate -f 'dd.MM.yyyy') -replace "\.","/"
$RTime = Get-Date $RestartDate -f 'HH:mm'
&schtasks /delete /tn "Post Maintenance Restart" /f
&schtasks /create /sc once /tn "Post Maintenance Restart" /tr "'C:\Windows\system32\cmd.exe' /c shutdown /r /t 400" /SD $RDate /ST $RTime /f
}
[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.VisualBasic") | Out-Null
$Title = "Message IT service desk - Action required "
$Message = "Your computer has not been restarted since it was last patched. Please save all your work and make the appropriate selections below"
$Button1Text = "Restart now"
$Button2Text = "Postpone for 4 hours"
$Button3Text = "Postpone for 8 hours"
$Form = $null
$Button1 = $null
$Button2 = $null
$Label = $null
$TextBox = $null
$Result = $null
$timerUpdate = New-Object 'System.Windows.Forms.Timer'
$TotalTime = 300 #in seconds
Create-GetSchedTime -SchedTime $TotalTime
$timerUpdate_Tick={
# Define countdown timer
[TimeSpan]$span = $script:StartTime - (Get-Date)
# Update the display
$hours = "{0:00}" -f $span.Hours
$mins = "{0:00}" -f $span.Minutes
$secs = "{0:00}" -f $span.Seconds
$labelTime.Text = "{0}:{1}:{2}" -f $hours, $mins, $secs
$timerUpdate.Start()
if ($span.TotalSeconds -le 0)
{
$timerUpdate.Stop()
&schtasks /delete /tn "Post Maintenance Restart" /f
shutdown /r /t 0
}
}
$Form_StoreValues_Closing=
{
#Store the control values
}

$Form_Cleanup_FormClosed=
{
#Remove all event handlers from the controls
try
{
$Form.remove_Load($Form_Load)
$timerUpdate.remove_Tick($timerUpdate_Tick)
#$Form.remove_Load($Form_StateCorrection_Load)
$Form.remove_Closing($Form_StoreValues_Closing)
$Form.remove_FormClosed($Form_Cleanup_FormClosed)
}
catch [Exception]
{ }
}

# Form
$Form = New-Object -TypeName System.Windows.Forms.Form
$Form.Text = $Title
$Form.Size = New-Object -TypeName System.Drawing.Size(450,210)
$Form.StartPosition = "CenterScreen"
$Form.ControlBox = $False
$Form.Topmost = $true
$Form.KeyPreview = $true
$Form.ShowInTaskbar = $Formalse
$Form.FormBorderStyle = "FixedDialog"
$Form.MaximizeBox = $Formalse
$Form.MinimizeBox = $Formalse
$Icon = [system.drawing.icon]::ExtractAssociatedIcon("c:\Windows\System32\UserAccountControlSettings.exe")
$Form.Icon = $Icon

# Button One (Reboot/Shutdown Now)
$Button1 = New-Object -TypeName System.Windows.Forms.Button
$Button1.Size = New-Object -TypeName System.Drawing.Size(95,25)
$Button1.Location = New-Object -TypeName System.Drawing.Size(10,135)
$Button1.Text = $Button1Text
$Button1.Font = 'Tahoma, 10pt'
$Button1.Add_Click({
&schtasks /delete /tn "Post Maintenance Restart" /f
shutdown /r /t 0
$Form.Close()
})
$Form.Controls.Add($Button1)
# Button Two (Postpone for 4 Hours)
$Button2 = New-Object -TypeName System.Windows.Forms.Button
$Button2.Size = New-Object -TypeName System.Drawing.Size(138,25)
$Button2.Location = New-Object -TypeName System.Drawing.Size(105,135)
$Button2.Text = $Button2Text
$Button2.Font = 'Tahoma, 10pt'
$Button2.Add_Click({
$Button2.Enabled = $False
$timerUpdate.Stop()
$TotalTime = 14400
Create-GetSchedTime -SchedTime $TotalTime
$timerUpdate.add_Tick($timerUpdate_Tick)
$timerUpdate.Start()
})
$Form.Controls.Add($Button2)
# Button Three (Postpone for 8 Hours)
$Button3 = New-Object -TypeName System.Windows.Forms.Button
$Button3.Size = New-Object -TypeName System.Drawing.Size(145,25)
$Button3.Location = New-Object -TypeName System.Drawing.Size(243,135)
$Button3.Text = $Button3Text
$Button3.Font = 'Tahoma, 10pt'
$Button3.Add_Click({
$Button3.Enabled = $False
$timerUpdate.Stop()
$TotalTime = 28800
Create-GetSchedTime -SchedTime $TotalTime
$timerUpdate.add_Tick($timerUpdate_Tick)
$timerUpdate.Start()
})
$Form.Controls.Add($Button3)

# Label
$Label = New-Object -TypeName System.Windows.Forms.Label
$Label.Size = New-Object -TypeName System.Drawing.Size(445,35)
$Label.Location = New-Object -TypeName System.Drawing.Size(10,15)
$Label.Text = $Message
$label.Font = 'Tahoma, 10pt'
$Form.Controls.Add($Label)

# Label2
$Label2 = New-Object -TypeName System.Windows.Forms.Label
$Label2.Size = New-Object -TypeName System.Drawing.Size(355,30)
$Label2.Location = New-Object -TypeName System.Drawing.Size(10,100)
$Label2.Text = $Message2
$label2.Font = 'Tahoma, 10pt'
$Form.Controls.Add($Label2)

# labelTime
$labelTime = New-Object 'System.Windows.Forms.Label'
$labelTime.AutoSize = $True
$labelTime.Font = 'Arial, 26pt, style=Bold'
$labelTime.Location = '120, 60'
$labelTime.Name = 'labelTime'
$labelTime.Size = '43, 15'
$labelTime.TextAlign = 'MiddleCenter'
$labelTime.ForeColor = '242, 103, 34'
$Form.Controls.Add($labelTime)

#Start the timer
$timerUpdate.add_Tick($timerUpdate_Tick)
$timerUpdate.Start()
# Show
$Form.Add_Shown({$Form.Activate()})
#Clean up the control events
$Form.add_FormClosed($Form_Cleanup_FormClosed)
#Store the control values when form is closing
$Form.add_Closing($Form_StoreValues_Closing)
#Show the Form
$Form.ShowDialog() | Out-Null

答案1

请参阅此处发布的该脚本的版本:

自定义重启脚本

它源自相同的源代码,并且具有禁用的关闭按钮和可正常运行的最小化按钮。如果您将脚本的版本与该链接中的版本进行比较,就会看到需要更改哪些关键项才能禁用关闭按钮并启用最小化按钮。

以下是您需要进行的更改...

旧代码:

$Form.ControlBox = $False
$Form.MinimizeBox = $Formalse
...
$Form.add_Closing($Form_StoreValues_Closing)

新代码:

$Form.ControlBox = $True
$Form.MinimizeBox = $True
...
$Form.Add_Closing({$_.cancel = $True})

还请注意,所有出现的“$Formalse”都应改为“$False”。这是从原始脚本中遗留下来的拼写错误。它与“$Formalse”配合得很好(就像与任何未定义的变量配合一样),但与“$False”配合使用读起来更好。

相关内容