如何编写接受管道输入的 PowerShell 脚本?

编程入门 行业动态 更新时间:2024-10-28 09:14:53
本文介绍了如何编写接受管道输入的 PowerShell 脚本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试编写一个可以获取管道输入的 PowerShell 脚本(并且预计会这样做),但是尝试类似

I am trying to write a PowerShell script that can get pipeline input (and is expected to do so), but trying something like

ForEach-Object { # do something }

从命令行使用脚本时实际上不起作用,如下所示:

doesn't actually work when using the script from the commandline as follows:

1..20 | .\test.ps1

有办法吗?

注意:我了解函数和过滤器.这不是我要找的.

Note: I know about functions and filters. This is not what I am looking for.

推荐答案

这可行,而且可能还有其他方法:

This works and there are probably other ways to do it:

foreach ($i in $input) { $i }

17:12:42 PS>1..20 |.\cmd-input.ps1123-- 剪断 --181920

17:12:42 PS>1..20 | .\cmd-input.ps1 1 2 3 -- snip -- 18 19 20

搜索powershell $input 变量",您会找到更多信息和示例.一对夫妇在这里:PowerShell 函数和过滤器 PowerShell Pro!(请参阅使用 PowerShell 特殊变量$input""部分)脚本、函数和脚本块都可以访问 $input 变量,该变量为传入管道中的元素提供了一个枚举器."或$input gotchas « Dmitry 的 PowerBlog PowerShell 及其他...基本上是枚举器中的 $input,它提供对您拥有的管道的访问."

Search for "powershell $input variable" and you will find more information and examples. A couple are here: PowerShell Functions and Filters PowerShell Pro! (see the section on "Using the PowerShell Special Variable "$input"") "Scripts, functions, and script blocks all have access to the $input variable, which provides an enumerator over the elements in the incoming pipeline. " or $input gotchas « Dmitry’s PowerBlog PowerShell and beyond "... basically $input in an enumerator which provides access to the pipeline you have."

对于 PS 命令行,而不是 DOS 命令行 Windows 命令处理器.

For the PS command line, not the DOS command line Windows Command Processor.

更多推荐

如何编写接受管道输入的 PowerShell 脚本?

本文发布于:2023-07-08 05:22:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1072505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:脚本   管道   PowerShell

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!