在powershell中,是否有方法将输出通过管道传输到另一个脚本?

在powershell中,是否有方法将输出通过管道传输到另一个脚本?,powershell,piping,Powershell,Piping,我有一个脚本,可以将输出写入日志文件和控制台。我正在运行添加WindowsFeatures命令。。。我想获取此命令的输出并将其传输到脚本。可能吗?绝对可能。您只需要在param语句中包含CmdletBinding属性。然后,向其中一个参数添加一个属性,该属性详细说明管道输入绑定到参数的方式。例如,将其放在c:\temp\get-extension.ps1中: [CmdletBinding()] Param( [parameter(Mandatory=$true, Valu

我有一个脚本,可以将输出写入日志文件和控制台。我正在运行添加WindowsFeatures命令。。。我想获取此命令的输出并将其传输到脚本。可能吗?

绝对可能。您只需要在param语句中包含CmdletBinding属性。然后,向其中一个参数添加一个属性,该属性详细说明管道输入绑定到参数的方式。例如,将其放在c:\temp\get-extension.ps1中:

[CmdletBinding()]
Param(
[parameter(Mandatory=$true,
            ValueFromPipeline=$true)][System.IO.FileInfo[]]$file
)

process {
  $file.Extension
}
然后,您可以执行以下操作:

dir -File| C:\temp\get-extension.ps1
更新以解决最新的评论:我猜将参数类型设置为
[object[]]$stuff
而不是
[fileinfo[]]
,然后

$stuff | out-file c:\logs\logfile.txt  #or wherever you want

在流程块中,您可以接近。

绝对可以。您只需要在param语句中包含CmdletBinding属性。然后,向其中一个参数添加一个属性,该属性详细说明管道输入绑定到参数的方式。例如,将其放在c:\temp\get-extension.ps1中:

[CmdletBinding()]
Param(
[parameter(Mandatory=$true,
            ValueFromPipeline=$true)][System.IO.FileInfo[]]$file
)

process {
  $file.Extension
}
然后,您可以执行以下操作:

dir -File| C:\temp\get-extension.ps1
更新以解决最新的评论:我猜将参数类型设置为
[object[]]$stuff
而不是
[fileinfo[]]
,然后

$stuff | out-file c:\logs\logfile.txt  #or wherever you want

进程块中的将使您接近。

您想要日志文件的输出还是脚本的输出?我想要将命令add windowsfeature piping的输出添加到此脚本,该命令将写入日志文件您想要日志文件的输出还是脚本的输出?我想要将命令add windowsfeature piping的输出添加到此脚本,这会写入日志文件,但当我添加windowsfeatures | c:\..ps1时,我只会在日志文件中看到microsoft.Windows.ServerManager.Commands.FeatureOperationResult。我没有看到此命令的输出是否起作用:$stuff.ToString()|输出文件c:\logs\logfile.txt但当我添加windowsfeatures | c:\..ps1时,我只在日志文件中看到microsoft.Windows.ServerManager.Commands.FeatureOperationResult。我没有看到此命令的输出是否起作用:$stuff.ToString()|输出文件c:\logs\logfile.txt