powershell - PowerShell:$ input功能的值更改

标签 powershell

我发现当我编写以下函数时:

function test {
    Write-Host ($input | Measure-Object).Count
    Write-Host ($input | Measure-Object).Count
}

带有示例输入:
dir | test

它写在控制台上:
18
0

我认为这是因为通往Measure-Object的第一个管道会覆盖$ input。我知道一种解决方法,可以制作一个新数组并将其传递给周围:
function test {
    $inp = @($input)
    Write-Host ($inp | Measure-Object).Count
    Write-Host ($inp | Measure-Object).Count
}

但是我不喜欢它,因为我要引入一个新变量。有没有一种在不使$ input受影响的情况下传递到cmdlet的方法?

最佳答案

试试这个:

function test {    
 Write-Host ($input | Measure-Object).Count
 $input.reset()
 Write-Host ($input | Measure-Object).Count
}

reading about $input enumerator

关于powershell - PowerShell:$ input功能的值更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13141077/

相关文章:

wpf - 如何使用Powershell在WPF中显示rtf文件?

powershell - 从 Windows Server 2008 上的脚本打开 url

powershell - Powershell错误: “There is not enough memory or disk to complete the operation”

powershell - Invoke-WebRequest 返回某些地址的意外结果

powershell - 将控制台输出写入文件 - 文件意外为空

powershell - Nuget.exe 安装 — Microsoft.CSharp 已经具有 System.Dynamic.Runtime 的依赖项

powershell - 如何确定 Windows 终端是否正在执行我的 PowerShell 脚本?

Powershell,按文件名末尾的版本号对文件列表进行排序?

arrays - 将 PowerShell 数组传递给函数时如何展开它

powershell - 使用 Powershell 搜索 CSV 文件以查找特定值,而无需指定列名称