powershell - 在 Powershell foreach 循环中检索数组索引?

标签 powershell

我有一个使用 foreach 关键字的循环:

foreach ($ln in Get-Content "c:\ATextFile.txt" )

是否可以在迭代过程中找出 $ln 引用的数组索引?或者我是否需要为每次循环迭代创建和增加一个单独的计数变量?

最佳答案

是的,要么使用 for 循环(类似于 (gc c:\ATextFile.txt).count 将是上限)或外部计数器。

相关答案(对于 C#,但基本上都使用相同的可枚举概念):How do you get the index of the current iteration of a foreach loop?

关于powershell - 在 Powershell foreach 循环中检索数组索引?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7183078/

相关文章:

windows - 如果通过命令提示符而不是在脚本中运行,则命令有效..?

powershell - 如何使用 Powershell Commandlet 在应用服务环境中创建 Azure WebApp

excel - 从 Windows 服务应用程序运行 Excel 宏时出错

azure - 使用 Powershell 删除 Azure DevOps 上的分支

正则表达式性能

azure - 如何查看 Azure 中每个位置的每个资源的成本?

powershell - 我是否需要try/catch和-ErrorAction?

Powershell - 只打印引号之间的文本?

rest - Invoke-RestMethod - 忽略自签名证书

sharepoint - Get-SPTimerJob cmdlet错误列出了SharePoint计时器作业?