Forms 屏幕锁定时不显示PowerShell窗体

Forms 屏幕锁定时不显示PowerShell窗体,forms,powershell,locking,screen,display,Forms,Powershell,Locking,Screen,Display,我正在使用PowerShell表单提示用户关闭Adobe Reader,以便对其进行升级。它通过SCCM执行,因此在系统上下文中运行。如果用户已登录且会话处于活动状态,则此功能有效,但如果屏幕已锁定,则在我解锁屏幕时,表单不会显示,尽管脚本正在运行 我最初使用$Form.ShowDialog()来显示它,但看到其他一些帖子说它不可靠,所以我切换到[void][System.Windows.Forms.Application]::运行($Form),但它仍然不起作用。谁能帮我修一下这个吗 下面是精

我正在使用PowerShell表单提示用户关闭Adobe Reader,以便对其进行升级。它通过SCCM执行,因此在系统上下文中运行。如果用户已登录且会话处于活动状态,则此功能有效,但如果屏幕已锁定,则在我解锁屏幕时,表单不会显示,尽管脚本正在运行

我最初使用
$Form.ShowDialog()
来显示它,但看到其他一些帖子说它不可靠,所以我切换到
[void][System.Windows.Forms.Application]::运行($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)

我决定通过检测logonui进程的存在来“解决”这个问题,这意味着屏幕被锁定。在这种情况下,我将关闭Adobe Reader。

嗨,我从来没有说过我希望表单显示在锁定屏幕的顶部。我很高兴它能在屏幕锁定的情况下在后台运行。这只是为了防止表单出现时用户不在办公桌上,并且在倒计时结束前他们碰巧解锁了屏幕。在这种情况下,如果他们使用Adobe Reader,它将在没有警告的情况下停止,这虽然不是世界末日,但可能会产生不理想的帮助热线事件。我不想争论我为什么这样做的逻辑,我更愿意专注于提出一个解决方案。谢谢。至于需要以用户身份登录,那是不正确的。当通过SCCM部署时,该表单显示得非常完美,正如我所说,SCCM在系统上下文中运行(尽管有一个选项可以在用户上下文中运行它,我还没有设置)。好的,删除了其他注释和此说明,但您正在向当前登录的用户发送GUI。因此,它必须在用户的上下文中才能看到它。PowerShell只运行/输出到启动它的用户上下文。实际上,我认为我可能已经找到了一种“绕过”它的方法,将锁定的屏幕视为与注销的用户相同,因此在这种情况下只需结束Adobe Reader。使用logonui过程作为决定因素。我会测试一下,看看是否有帮助。当然,这并不理想,但它可以工作-至少Adobe Reader不会在他们眼前关闭,他们会想知道发生了什么。很好,如果SCCM方法是catch22,我的下一个建议是通过计划任务使用logonui检查,并在用户屏幕解锁时启动脚本弹出UI。是的,如果他们只是在看PDF<没有伤害/没有犯规,但是如果他们正在编辑,那么,你知道。好吧,如果他们通过Word和Adobe编辑PDF,仍然没有伤害,没有犯规。