Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/jpa/2.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:在标准输出中选择字符串?_Powershell_Powershell 3.0 - Fatal编程技术网

PowerShell:在标准输出中选择字符串?

PowerShell:在标准输出中选择字符串?,powershell,powershell-3.0,Powershell,Powershell 3.0,squid.exe-v将输出其版本信息,其中包含关键字“squid” 我希望powershell告诉我输出中是否存在关键字“Squid”。因此,我使用\squid.exe-v |选择字符串squid,但它不输出任何内容 正确的方法是什么?我使用的是PS3.0。您使用的方法是正确的:) 问题不在于代码,而在于squid端口本身。将文本写入控制台是一件奇怪的事情,PowerShell和cmd无法通过stdout/stderr流将其捕获到控制台。我猜不是使用stdout/stderrapi,而是直接在

squid.exe-v
将输出其版本信息,其中包含关键字“squid”

我希望powershell告诉我输出中是否存在关键字“Squid”。因此,我使用
\squid.exe-v |选择字符串squid
,但它不输出任何内容


正确的方法是什么?我使用的是PS3.0。

您使用的方法是正确的:)

问题不在于代码,而在于squid端口本身。将文本写入控制台是一件奇怪的事情,PowerShell和cmd无法通过stdout/stderr流将其捕获到控制台。我猜不是使用stdout/stderrapi,而是直接在控制台上操作字符或其他什么。我尝试将stderr重定向到stdout(
2>&1
),但也没有成功

它附带了一个更改日志文本文件,我想你可以直接解析它

编辑--

或者,您可以使用此笨拙但可用的解决方法来刮除控制台文本:

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

谢谢但是定义一个函数太麻烦了。我可能会等待更简洁的答案。@LoveRight我认为您唯一的其他选择是修改squid src/main.c,使其写入stdout并重新编译:)
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]}
    }