Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.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_Wait_Pipeline - Fatal编程技术网

Powershell 如何通过管道获取内容-等等?

Powershell 如何通过管道获取内容-等等?,powershell,wait,pipeline,Powershell,Wait,Pipeline,我想通过管道将Get Content$file-Wait的输出传输到自定义PowerShell脚本。脚本如下所示 $lines = ($input | Out-String) -replace "`r", "" -split "`n" foreach ($line in $lines) { #Process $line Write-Host $line } 基本上,我们的想法是接收输入,将其格式化,然后在输出输出到控制台之前对其进行处理 问题是当我像cat$file-Wai

我想通过管道将
Get Content$file-Wait
的输出传输到自定义PowerShell脚本。脚本如下所示

$lines = ($input | Out-String) -replace "`r", "" -split "`n"

foreach ($line in $lines) {
    #Process $line

    Write-Host $line
}
基本上,我们的想法是接收输入,将其格式化,然后在输出输出到控制台之前对其进行处理

问题是当我像
cat$file-Wait | MyScript
那样调用脚本时,没有任何东西被发送到脚本中。如果我执行了
cat$file-Wait
cat$file | MyScript
,则一切正常。但是,将管道和wait参数结合起来是行不通的


是否需要使用一些语法来处理
-Wait
参数?我尝试使用
输出字符串流
,但也不起作用。

我看不到任何方法可以满足您的要求-等待会启动一个永不结束的循环,停止的唯一方法是手动终止它。因为它总是卡在循环中,所以启动循环后尝试执行的任何操作都不会被处理。

问题在于$input

如果您这样做:

Get-Content $file -Wait | Get-Member -InputObject $input

您将获得:

Get-Member : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
Out-String : A positional parameter cannot be found that accepts argument 'System.Collections.ArrayList+ArrayListEnumeratorSimple'.
At line:1 char:52
+ get-content '.\netstat anob.txt' -wait | Out-String <<<<  $input
    + CategoryInfo          : InvalidArgument: (:) [Out-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutStringCommand
如果Get成员无法读取通过管道传输的对象,那么您就知道该对象(或管道传输)存在严重问题

让我们尝试将$input管道化到Out字符串,就像您在脚本中所做的那样:

Get-Content $file -Wait | Out-String $input
您将获得:

Get-Member : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
Out-String : A positional parameter cannot be found that accepts argument 'System.Collections.ArrayList+ArrayListEnumeratorSimple'.
At line:1 char:52
+ get-content '.\netstat anob.txt' -wait | Out-String <<<<  $input
    + CategoryInfo          : InvalidArgument: (:) [Out-String], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.OutStringCommand
Out字符串:找不到接受参数“System.Collections.ArrayList+ArrayListEnumeratorSimple”的位置参数。
第1行字符:52

+获取内容'.\netstat anob.txt'-wait | Out String如果脚本接受管道输入,则这是可能的。当您通过管道连接到其他cmdlet(如
selectstring
)时,您可以看到它。例如,将script.ps1定义为:

process { Write-Host "line: $input" }
然后跑

1..200 | foreach { add-content -Path test.txt -Value "$_"; start-sleep 1 }
在一个PowerShell会话和

gc test.txt -wait | .\script.ps1

在另一行中,您可以看到每一行都通过管道传输到脚本。

问题在于这一行:

Write-Host $line

您应该改用写输出。写入输出将对象发送到管道,将主机直接写入主机(控制台)。

您意识到您这样做了
$lines=
,然后
ForEach($linein$input)
使第一行完全无用?另外,您是否真的需要一个字符串数组,将其作为多行字符串输出,删除CRs,然后将其拆分回一个字符串数组?“这有什么意义吗?”TheMadTechnician对此表示抱歉。有一个打字错误,
$input
应该是
$line
。这样做的目的是将数组管道化到
输出字符串
格式化输入。好的,但是其他一些cmdlet可以将
-Wait
的输出作为管道输入。例如,我可以执行
cat$file-Wait |选择String-Pattern“test”
将输出过滤为仅匹配“test”的行。我们是否假设
Select String
有特殊的代码来处理这种特殊类型?您也可以使用过滤器而不是带有进程块的函数。过滤器是这样的场景的别名,在输入到达时一次处理一个。