Powershell 和操作员停止工作
我在 PowerShell 中有一个调用 LogParser 的脚本。在脚本的早期,我定义了可执行文件的路径并测试其路径:
#Define path to log parser executable
$logParser = '\\hostname\logparser22\LogParser.exe'
if (! $(Test-Path $logParser) )
{
Write-Host -ForegroundColor Red "Could not access: $logParser"
return
}
然后在脚本中我调用 LogParser:
$sessionData = & $logParser "SELECT * FROM $logPath where data LIKE `'Contoso\\$user`'" -i:csv -nSkipLines:4 -headers:on -stats:off -o:csv
这个文件在 PowerShell 会话期间可以工作一段时间,但如果运行足够多的时间,它最终会停止工作。一旦我进入一个损坏的 shell,进行一些调试,下面的内容甚至不会产生在不带参数调用 LogParser 时返回的正常帮助:
& $LogParser
但是,如果我打开一个运行相同命令的新 PowerShell 会话,它会工作并调用 LogParser当不传递任何参数时,我会得到标准响应。
我归结为&不知何故被打破了。有没有人见过这个并知道修复\解决方法?
I have a script in PowerShell that calls LogParser. Early in the script I define the path to the executable and tests its path:
#Define path to log parser executable
$logParser = '\\hostname\logparser22\LogParser.exe'
if (! $(Test-Path $logParser) )
{
Write-Host -ForegroundColor Red "Could not access: $logParser"
return
}
Then later down the script I call LogParser:
$sessionData = & $logParser "SELECT * FROM $logPath where data LIKE `'Contoso\\$user`'" -i:csv -nSkipLines:4 -headers:on -stats:off -o:csv
This works file for awhile during the PowerShell session, but if run enough times it stops working eventually. Doing a little debugging once I've entered a broken shell, the below does not even produce the normal help that comes back when calling LogParser without parameters:
& $LogParser
However if I open a new PowerShell session running the SAME exact command, it works and calls LogParser and I get the standard response from it when not passing any parameters.
What I've come down to is & is broken somehow. Has anyone seen this and know of a fix\workaround?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这可能与PowerShell 如何处理大量控制台输出的缺陷,这在 LogParser 中很容易发生。据推测,这个问题已在 PowerShell 3.0 中得到修复。
This might be related to this defect in how PowerShell handles large volumes of console output, which could easily happen with the LogParser. This is supposedly fixed in PowerShell 3.0.
也许您可以尝试使用其他方式通过 Cmdlet 启动外部进程:
您应该对错误有更多解释。
Perhaps you can try using an other way to start you external process with a Cmdlet :
You should have more explanations on the error.
我遇到了这个问题。正如错误报告中提到的,Powershell V2 中的解决方法是将
[GC]::Collect()
在写入控制台的循环中。&
是调用外部.exe
的首选方式。有一篇关于如何使用参数设置外部调用的精彩文章< /a>.I had this exact problem. As mentioned in the bug report, the workaround in Powershell V2 is to put
[GC]::Collect()
in your loop that writes to the console.The
&<command>
is the preferred way of calling out to an external.exe
. There is an excellent post about how to set up your external calls with arguments.