2013-07-27 25 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,使其寫入標準輸出並重新編譯:) –

相關問題