当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 在PowerShell中使用GetEnumerator读取数据

作者:精品下载站 日期:2024-12-15 00:30:47 浏览:13 分类:玩电脑

在PowerShell中使用GetEnumerator读取数据


PowerShell中的GetEnumerator用于迭代Hashtable或数组来读取集合中的数据。

GetEnumerator 不会修改集合数据。在 PowerShell 中使用 foreach 或 foreach-object 迭代从 GetEnumerator 读取的数据。

在本文中,我们将讨论如何在PowerShell中使用GetEnumerator来循环PowerShell中的HashTable或数组

在PowerShell中使用GetEnumerator读取HashTable中的数据

哈希表将项目存储在键值对中。使用 PowerShell 中的 GetEnumerator 循环遍历 HashTable 项,并将项传递给 foreach 或 foreach-object 以读取键值对。

$employees = @{1 = "Tom"; 2 = "Gary" ; 3 = "Thomas"}

# Display keys and values of HashTable using GetEnumerator
$employees.GetEnumerator()

# Iterate through the Hashtable Items using GetEnumerator

$employees.GetEnumerator() | ForEach-Object {
  Write-Output "Key = $($_.key) , Value = $($_.Value)"
}  

在上面的 PowerShell 脚本中,$employees 变量包含具有键值对的哈希表。

PowerShell 中的 GetEnumerator 迭代 HashTable 并显示 HashTable 的键和值。

要读取 HashTable 的所有项目,请使用 GetEnumerator 并将 HashTable 输出对象通过管道传输到 ForEach-Object。 ForEach-Object 循环遍历对象并打印哈希表的键和值。

上述用于迭代 HashTable 的 PowerShell 脚本的输出是:

[玩转系统] 在PowerShell中使用GetEnumerator读取数据

PowerShell 中的 GetEnumerator 读取数组

PowerShell 数组是一种存储多个项目集合的数据结构。要读取集合中的数据,请在 PowerShell 中使用 GetEnumerator。它遍历数组。

$emp = @("Tom","Gary","Thomas") 

# Use the GetEnumerator to iterate throgh an array, display name
$emp.GetEnumerator()

# Display name of the employee
$emp.GetEnumerator() | ForEach-Object { Write-Output "Name = $_" }

在上面的 PowerShell 脚本中,$emp 是一个存储字符串数据类型元素的数组。

使用 PowerShell 中的 GetEnumerator 迭代数组,并将数组对象的输出通过管道传输到 ForEach-Object。它显示员工的姓名。

在 PowerShell 中使用 GetEnumerator 循环遍历数组的上述 PowerShell 脚本的输出是:

PS C:\> $emp = @("Tom","Gary","Thomas")                                                                                                                                                      PS C:\>                                                                                                                                                                                      
PS C:\> $emp                                                                                                                                                                                 
Tom
Gary
Thomas

PS C:\> $emp.GetEnumerator()                                                                                                                                                                 Tom
Gary
Thomas

                                                                                                                                                                                   
PS C:\> $emp.GetEnumerator() | ForEach-Object { Write-Output "Name = $_" }                                                                                                                   Name = Tom
Name = Gary
Name = Thomas

PS C:\>      

结论

我希望上面的文章可以帮助您了解 PowerShell 中的 GetEnumerator 来迭代 HashTable 或 Array。

您可以在 ShellGeek 主页上找到有关 PowerShell Active Directory 命令和 PowerShell 基础知识的更多主题。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯