【发布时间】:2019-08-28 12:50:57
【问题描述】:
我有一个 PowerShell 脚本,该脚本具有在有人输入计算机名称时提取 LAPS 信息的功能。我想要的是在 60 秒后清除输出,以免有人不小心在屏幕上留下密码。
似乎无论在函数中的什么位置(甚至之后),脚本都会暂停 60 秒,然后显示信息并且永远不会清除。
等待和清除的脚本部分:
Start-Sleep -s 60
$WPFOutputbox.Clear()
(在下方查找我的#TRIED HERE cmets)
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 秒以显示信息,并且永远不会清除屏幕。
【问题讨论】:
-
$WpFOutputBox.Text = "" ?
-
这是一个 WPF 窗口,以更友好的帮助台方式显示输出。不过它会在 powershell 上运行。
-
尝试将文本设置为空字符串,而不是 Clear()。 $WpFOutputBox.Text = ""
-
这不是主要问题,而是在显示信息之前等待 60 秒。我可以用其他命令清屏就好了。
-
我想说,你需要定义一段 C#/.Net 代码,然后使用类似 [System.Threading.Tasks.Task]::Run(delegate) 的东西在后台启动它。或者在 C# 中制作自己的自动关闭对话框窗口,使用 Add-Type 加载此 DLL。或者,在本地主机上运行整个输出任务。
标签: powershell