Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 管道循环到out gridview并清除每个循环上的网格?_Powershell_Out Gridview - Fatal编程技术网

Powershell 管道循环到out gridview并清除每个循环上的网格?

Powershell 管道循环到out gridview并清除每个循环上的网格?,powershell,out-gridview,Powershell,Out Gridview,我只是想知道是否可以像在控制台中一样清除每个循环上的Gridview: while (1) { ps | select -first 5; sleep 1; clear-host } 不幸的是,这并不是每次都清除gridview: & { while (1) { ps | select -first 5; sleep 1; clear-host } } | out-gridview 清除主机清除主机的显示,主机是常规PowerShell控制台中控制台窗口的内容 相比之下,Out G

我只是想知道是否可以像在控制台中一样清除每个循环上的Gridview:

while (1) { ps | select -first 5; sleep 1; clear-host }
不幸的是,这并不是每次都清除gridview:

 & { while (1) { ps | select -first 5; sleep 1; clear-host } } | out-gridview

清除主机
清除主机的显示,主机是常规PowerShell控制台中控制台窗口的内容

相比之下,
Out GridView
是一个单独的GUI窗口,PowerShell在该窗口上显示时不提供任何编程显示。
值得注意的是,在使用初始数据显示窗口后,您既不能清除也不能刷新窗口的内容

此功能的最佳近似方法是关闭旧窗口,并在每次迭代中打开一个包含新数据的新窗口,但请注意,这将在视觉上造成破坏

在最简单的情况下,将
移出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
窗口,但这是一项不平凡的工作-睡眠时间短至
1
秒,会对视觉造成太大干扰

最后,您需要的是Unix实用程序的GUI版本(或者更具体地说,是该实用程序)

但是,由于您不希望与
Out-GridView
窗口进行交互,在这种情况下使用
Out-GridView
没有什么好处

相反,您可以创建一个新的控制台窗口,该窗口使用
清除主机
定期在相同的屏幕位置显示输出:

以下定义了帮助器函数
监视输出
,以便于:

# 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
请注意,每次刷新窗口内容时,此选项仍将闪烁。 要避免这一点,需要付出更大的努力

提供了复杂的
Watch命令
cmdlet,它不仅避免了闪烁,而且还提供了其他功能。

最大的警告是-从版本
1.3.6
-模块有几个与内置cmdlet冲突的cmdlet(
格式化十六进制
获取剪贴板
新自签名证书
发送邮件
,设置剪贴板),导入模块的唯一方法是允许模块的命令覆盖内置命令(
导入模块PowerShellCookbook-AllowClobber
)。

所有
*-Host
cmdlet直接进入控制台主机屏幕。这意味着您没有向控制台以外的任何对象发送任何清除命令。。。而且,
OutGridView
不是一个控制台应用程序-它是一个GUI应用程序。如果你想清除它。。。关闭它,然后重新打开它。