2013-07-27 23 views
2
PS C:\squid\sbin> .\squid.exe -v 
Squid Cache: Version 2.7.STABLE8 
configure options: --enable-win32-service --enable-storeio='ufs aufs null coss' --enable-default-hostsfile=none --enable 
-removal-policies='heap lru' --enable-snmp --enable-htcp --disable-wccp --disable-wccpv2 --enable-useragent-log --enable 
-referer-log --enable-cache-digests --enable-auth='basic ntlm digest negotiate' --enable-basic-auth-helpers='LDAP NCSA m 
swin_sspi squid_radius_auth' --enable-negotiate-auth-helpers=mswin_sspi --enable-ntlm-auth-helpers='mswin_sspi fakeauth' 
--enable-external-acl-helpers='mswin_ad_group mswin_lm_group ldap_group' --enable-large-cache-files --enable-digest-aut 
h-helpers='password LDAP eDirectory' --enable-forw-via-db --enable-follow-x-forwarded-for --enable-arp-acl --prefix=c:/s 
quid 
Compiled as Windows System Service. 
PS C:\squid\sbin> .\squid.exe -v|Select-String Squid 

squid.exe -v将输出其版本信息,其中包含关键字“Squid”。PowerShell:在标准输出中选择字符串?

我想让powershell告诉我关键字“Squid”是否存在于输出中。所以我使用.\squid.exe -v|Select-String Squid,但它什么都不输出。

什么是正确的做法?我正在使用PS 3.0。

回答

3

你正在做正确的方式:)

的问题是不是你的代码,但鱿鱼端口本身。它做一些奇怪的事情将文本写入控制台,PowerShell和cmd无法通过stdout/stderr流来捕获文本。我猜测,而不是使用stdout/stderr api它可能直接或在操纵控制台上的字符。我尝试将stderr重定向到标准输出(2>&1),但那也不起作用。

它配备了一个更改日志文本文件,我想你可以解析,而不是...

编辑 -

或者你可以用这个缺憾,但维修的解决办法凑控制台文本:

function Get-ConsoleText { 
    if ($host.Name -eq 'ConsoleHost') { 
     $text_builder = new-object system.text.stringbuilder 
     $buffer_width = $host.ui.rawui.BufferSize.Width 
     $buffer_height = $host.ui.rawui.CursorPosition.Y 
     $rec = new-object System.Management.Automation.Host.Rectangle 0,0,($buffer_width -2), $buffer_height 
     $buffer = $host.ui.rawui.GetBufferContents($rec) 

     $console_out = @() 
     for($i = 0; $i -lt $buffer_height; $i++) { 
      $text_builder = new-object system.text.stringbuilder 
      for($j = 0; $j -lt $buffer_width; $j++) { 
       $cell = $buffer[$i,$j] 
       $text_builder.Append($cell.Character) | Out-Null 
      } 
      $console_out += $text_builder.ToString() 
     } 
     return $console_out 
    } 
} 

cls; .\squid.exe -v; Get-ConsoleText | 
    ForEach-Object { 
     if ($_ -match 'Version (.+)') {$matches[1]} 
    } 
+0

谢谢。但是定义一个函数太麻烦了。我可能会等待一个更好的答案。 – Gqqnbig

+1

@LoveRight我认为你唯一的选择是修改squid src/main.c,使其写入标准输出并重新编译:) –

相关问题