我正在使用PowerShell表单提示用户关闭Adobe Reader,以便对其进行升级。它通过SCCM执行,因此在系统上下文中运行。如果用户登录并且会话处于活动状态,则它可以工作,但如果屏幕被锁定,则在解锁屏幕时不会显示表单,尽管脚本正在运行。
我最初使用$Form.ShowDialog()
来显示它,但看到其他一些帖子说它不可靠,所以我切换到[void][System.Windows.Forms.Application]::Run($Form)
,但它仍然不起作用。有人能帮我修一下吗?
下面是精简后的脚本,只有与呈现表单相关的代码。
Add-Type -AssemblyName System.Windows.Forms, System.Drawing
$Font = New-Object System.Drawing.Font('Segoe UI Light',14,[System.Drawing.FontStyle]::Regular)
$FontBold = New-Object System.Drawing.Font('Segoe UI',13,[System.Drawing.FontStyle]::Bold)
Function EndForm
{
$Timer.Stop();
$Timer.Dispose();
$Label.Dispose();
$CountdownLabel.Dispose();
$ProgressBar.Dispose();
$OKButton.Dispose();
$Form.Close();
$Form.Dispose();
}
$OnOKClick =
{
$MsgBoxInput = [System.Windows.Forms.MessageBox]::Show("Are you sure you want to close and uninstall Adobe Reader now?","Confirm Adobe Reader Removal","YesNo","Warning")
If ($MsgBoxInput -eq "Yes") {EndForm}
}
$Script:Countdown = 3600
$OKToolTip = "Click OK to close and uninstall Adobe Reader now"
$CountdownSubtitle = "Adobe Reader will close and uninstall in"
$Label = New-Object System.Windows.Forms.Label
$Label.AutoSize = $False
$Label.Height = 120
$Label.Width = 630
$Label.Font = $Font
$Label.BackColor = "White"
$Label.ForeColor = "#4D4C5C"
$Label.Location = New-Object System.Drawing.Size(52,180)
$Label.Text = "Your computer will soon be upgraded to newer versions of Windows 10 and Office 365. In order to succeed, the upgrade process requires that Adobe Reader be closed and uninstalled first. It will not be available for use again until after the upgrade has completed."
$CountdownLabel = New-Object System.Windows.Forms.Label
$CountdownLabel.AutoSize = $False
$CountdownLabel.Height = 40
$CountdownLabel.Width = 530
$CountdownLabel.Font = $Font
$CountdownLabel.BackColor = "White"
$CountdownLabel.ForeColor = "#4D4C5C"
$CountdownLabel.Location = New-Object System.Drawing.Size(120,380)
$ProgressBar = New-Object System.Windows.Forms.ProgressBar
$ProgressBar.Location = New-Object System.Drawing.Size(52,340)
$ProgressBar.Size = New-Object System.Drawing.Size(635,35)
$ProgressBar.Style = "Continuous"
$ProgressBar.Maximum = $Script:Countdown
$ProgressBar.Minimum = 0
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Name = "OK_Button"
$OKButton.Size = New-Object System.Drawing.Size(80,40)
$OKButton.Location = New-Object System.Drawing.Size(330,450)
$OKButton.UseVisualStyleBackColor = $True
$OKButton.Text = "OK"
$OKButton.DataBindings.DefaultDataSourceUpdateMode = 0
$OKButton.Add_Click($OnOKClick)
$ToolTip = New-Object System.Windows.Forms.ToolTip
$ToolTip.IsBalloon = $False
$ToolTip.InitialDelay = 100
$ToolTip.ReshowDelay = 200
$ToolTip.SetToolTip($OKButton,$OKToolTip)
$Form = New-Object System.Windows.Forms.Form
$Form.Text = "Contoso LLC"
$Form.Font = $Font
$Form.Width = 770
$Form.Height = 580
$Form.MinimizeBox = $False
$Form.MaximizeBox = $False
$Form.ControlBox = $False
$Form.ShowIcon = $False
$Form.WindowState = "Normal"
$Form.FormBorderStyle = "Fixed3D"
$Form.ShowInTaskbar = $True
$Form.StartPosition = "CenterScreen"
$Form.Topmost = $True
$Form.Controls.Add($ProgressBar)
$Form.Controls.Add($Label)
$Form.Controls.Add($CountdownLabel)
$Form.Controls.Add($OKButton)
$Timer = New-Object System.Windows.Forms.Timer
$Timer.Interval = 1000
$Timer.Add_Tick(
{
If ($Script:Countdown -eq 0) {EndForm}
$ProgressBar.Value = $Script:Countdown
$TimeRemain = New-Timespan -Seconds $Script:Countdown
$HrsRemain = $TimeRemain.Hours
$MinsRemain = $TimeRemain.Minutes
$SecsRemain = $TimeRemain.Seconds
If ($HrsRemain -ge 2) {$HrsText = "$HrsRemain hours"}
If ($HrsRemain -eq 1) {$HrsText = "$HrsRemain hour"}
If ($MinsRemain -ge 2) {$MinsText = "$MinsRemain minutes"}
If ($MinsRemain -eq 1) {$MinsText = "$MinsRemain minute"}
If ($SecsRemain -ge 2) {$SecsText = "$SecsRemain seconds"}
If ($SecsRemain -eq 1) {$SecsText = "$SecsRemain second"}
$CountdownLabel.Text = "$CountdownSubtitle $HrsText $MinsText $SecsText"
If ($HrsRemain -ge 1 -And $MinsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $HrsText $SecsText"}
If ($HrsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $MinsText $SecsText"}
If ($HrsRemain -eq 0 -And $MinsRemain -eq 0) {$CountdownLabel.Text = "$CountdownSubtitle $SecsText"}
$Script:Countdown--
})
$Timer.Start()
[void][System.Windows.Forms.Application]::Run($Form)
我决定通过检测登录进程的存在来"解决"这个问题,这意味着屏幕被锁定。在这种情况下,我无论如何都会关闭Adobe Reader。