Start-Sleep 暂停整个功能 [英] Start-Sleep pauses entire function

查看:56
本文介绍了Start-Sleep 暂停整个功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 PowerShell 脚本,具有在有人输入计算机名称时提取 LAPS 信息的功能.我想要的是在 60 秒后清除输出,以免有人不小心在屏幕上留下密码.

I have a PowerShell script with a function to pull LAPS information when someone enters a computer name. What I'd like is to clear the output after 60 seconds so someone doesn't accidentally leave a password on their screen.

似乎无论睡眠在函数中的哪个位置(甚至之后),脚本都会暂停 60 秒,然后显示信息并且永远不会清除.

Seems as though no matter where the sleep is in the function (or even after) the script is paused for the 60 seconds, then displays the information and it never clears.

等待和清除的脚本部分:

The part of the script that waits and clears:

Start-Sleep -s 60
$WPFOutputbox.Clear()

(在下面寻找我的 #TRIED HERE 评论)

(look for my #TRIED HERE comments below)

function GETLAPS {
    Param ($computername = $WPFComputer.Text)

    try {
        $LAPSComputer = Get-AdmPwdPassword -ComputerName $computername |
                        Select-Object -ExpandProperty computername
        $LAPSDistinguished = Get-AdmPwdPassword -ComputerName $computername |
                             Select-Object -ExpandProperty distinguishedname
        $LAPSPassword = Get-AdmPwdPassword -ComputerName $computername |
                        Select-Object -ExpandProperty Password
        $LAPSExpire = Get-AdmPwdPassword -ComputerName $computername |
                      Select-Object -ExpandProperty Expirationtimestamp
    } catch {
        $ErrorMessage = $_.Exception.Message
    }
    if ($ErrorMessage -eq $null) {
        $WPFOutputBox.Foreground = "Blue"
        $WPFOutputBox.Text = "Local Admin Information for: $LAPSComputer

    Current: $LAPSPassword

    Exipiration: $LAPSExpire

    SCREEN WILL CLEAR AFTER 60 SECONDS"
        #TRIED HERE
    } else {
        $WPFOutputBox.Foreground = "Red"
        $WPFOutputBox.Text = $ErrorMessage
    }
    #TRIED HERE
}

相反,脚本将等待 60 秒来显示信息,并且永远不会清除屏幕.

Instead the script will wait 60 seconds to show information, and never clear the screen.

推荐答案

除非你创建一个单独的线程,否则我强烈建议不要使用 Start-Sleep cmdletWindows 窗体Windows Presentation Foundation 因为它显然会拖延用户界面.

Unless you create a separate thread, I would strongly advise against using the Start-Sleep cmdlet in Windows Forms or Windows Presentation Foundation as it will obviously stall the User Interface.

相反,我建议使用 (Windows.Forms.Timer) 事件.

Instead, I would recommend to use a (Windows.Forms.Timer) event.

为此,我写了一个小函数来延迟命令:

For this, I wrote a small function to delay a command:

Function Delay-Command([ScriptBlock]$Command, [Int]$Interval = 100, [String]$Id = "$Command") {
    If ($Timers -isnot "HashTable") {$Global:Timers = @{}}
    $Timer = $Timers[$Id]
    If (!$Timer) {
        $Timer = New-Object Windows.Forms.Timer
        $Timer.Add_Tick({$This.Stop()})
        $Timer.Add_Tick($Command)
        $Global:Timers[$Id] = $Timer
    }
    $Timer.Stop()
    $Timer.Interval = $Interval
    If ($Interval -gt 0) {$Timer.Start()}
}; Set-Alias Delay Delay-Command

示例

Delay {$WpFOutputBox.Text = ''} 60000

参数

-命令
要延迟的命令.

Parameters

-Command
The command to be delayed.

-间隔
执行command之前等待的时间.
如果-Interval 设置为0 或更少,command(与相关的Id)将被重置.
默认值为 100(一个小的时间间隔,让其他事件有可能开始).

-Interval
The time to wait before executing the command.
If -Interval is set to 0 or less, the command (with the related Id) will be reset.
The default is 100 (a minor time lapse to give other events the possibility to kick off).

-Id
延迟命令的 Id.可以同时执行不同Id 的多个命令.如果相同的 Id 用于尚未执行的命令,则间隔计时器将重置(或在设置为 0 时取消).
命令字符串中的默认 Id.

-Id
The Id of the delayed command. Multiple commands which different Id's can be executed simultaneously. If the same Id is used for a command that is not yet executed, the interval timer will be reset (or canceled when set to 0).
The default Id in the command string.

这篇关于Start-Sleep 暂停整个功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆