[英]pipe loop to out-gridview and clear grid on every loop?
我只是想知道是否可以像在控制台中一樣清除每個循環上的 Out-Gridview:
while (1) { ps | select -first 5; sleep 1; clear-host }
不幸的是,這並不能每次都清除 out-gridview:
& { while (1) { ps | select -first 5; sleep 1; clear-host } } | out-gridview
Clear-Host
清除主機的顯示,即控制台 window在常規 PowerShell 控制台中的內容。
相比之下, Out-GridView
是一個單獨的 GUI window ,在其上 PowerShell 在顯示后不提供任何編程顯示。
值得注意的是,在顯示初始數據后,您既不能清除也不能刷新窗口的內容。
此功能的最佳近似值是關閉舊的 window 並在每次迭代中使用新數據打開一個新的 - 但請注意,這會在視覺上造成破壞。
在最簡單的情況下,將Out-GridView
移動到循環中並使用-Wait
調用它,這需要您在每次迭代中手動關閉它,但是:
# NOTE: Doesn't move to the next iteration until you manually close the window.
while (1) { ps | select -first 5 | Out-GridView -Wait }
這個答案顯示了如何實現自動關閉的Out-GridView
window,但這是一項不平凡的工作 - 睡眠時間短至1
秒,在視覺上會造成太大的破壞。
最終,您正在尋找的是 Unix watch
實用程序的 GUI 版本(或者,更具體地, top
實用程序)。
但是,由於您不希望與Out-GridView
window 進行交互,因此在這種情況下使用Out-GridView
幾乎沒有什么好處。
相反,您可以生成一個新的控制台window 使用Clear-Host
定期在同一屏幕中顯示 output position:
以下定義了幫助程序 function Watch-Output
以促進這一點:
# Simple helper function that opens a new console window and runs
# the given command periodically, clearing the screen beforehand every time.
function Watch-Output ([scriptblock] $ScriptBlock, [double] $timeout = 1) {
$watchCmd = @"
while (1) {
Clear-Host
& { $($ScriptBlock -replace '"', '\"') } | Out-Host
Start-Sleep $timeout
}
"@ #'
Start-Process powershell.exe "-command $watchCmd"
}
# Invoke with the command of interest and a timeout.
Watch-Output -ScriptBlock { ps | Select -First 5 } -Timeout 1
請注意,每次刷新 window 內容時,它仍然會閃爍。 避免這種情況需要付出更多的努力。
PowerShellCookbook
模塊提供了復雜的Watch-Command
cmdlet,它不僅可以避免閃爍,還可以提供其他功能。
最大的警告是 - 從版本1.3.6
開始 - 該模塊有幾個 cmdlet 與內置的( Format-Hex
、 Get-Clipboard
、 New-SelfSignedCertificate
、 Send-MailMessage
、 Set-Clipboard
)沖突,並且唯一的導入模塊的方法是允許模塊的命令覆蓋內置命令( Import-Module PowerShellCookbook -AllowClobber
)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.