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

[玩转系统] PowerShell 哈希表

作者:精品下载站 日期:2024-12-15 00:31:10 浏览:16 分类:玩电脑

PowerShell 哈希表


PowerShell 中的哈希表是一种在哈希表中存储键值对的数据结构。 PowerShell 哈希表中的键和值可以包含字符串、整数或任何对象类型数据。

哈希表类似于数组,但数据存储为键值对,并且允许使用键访问值。键是唯一标识符,值是与键关联的数据。

在本文中,我们将讨论 PowerShell 中的哈希表以及如何使用 PowerShell HashTables。

如何在 PowerShell 中创建哈希表

PowerShell 中的哈希表可以使用不同的方式创建。

使用@{}表示法创建哈希表

可以使用 @{} 表示法声明 PowerShell 哈希表。

语法

$stage1Server  = @{}

让我们在 PowerShell 中创建一个哈希表来存储给定项目的服务器配置。

$stage1Server = @{
"Server" = "IT-101"
"Port" = "80"
"Username" = "admin"
"DBServer" = "DB-CORP1"
"DBUser" = "dbadmin"
"DbTimeOut" = 120
}

上述在 PowerShell 中创建哈希表的 PowerShell 的输出是:

[玩转系统] PowerShell 哈希表

使用 New-Object cmdlet 创建哈希表

使用 PowerShell New-Object cmdlet,您可以创建哈希表。 New-Object cmdlet 创建 System.Collections.Hashtable 类的新实例来创建哈希表。

$ServerConfig = New-Object -TypeName System.Collections.Hashtable 

使用 [ordered] 属性创建哈希表

您可以使用[ordered]属性在PowerShell中创建哈希表,有序字典中的键始终按顺序出现。

[ordered] 属性创建 System.Collections.OrderedDictionary 类的实例。

$serverConfig = [ordered]@{
    "Server" = "DB-102"
    "User" = "admin"
    "Password" = "secret"
}

哈希表属性和方法

PowerShell 中的 Hashtable 有多个属性和方法来执行该操作。

PowerShell 有一个 Get-Member cmdlet,用于获取对象的属性和方法。

# Declare hashtable
$serverConfig = @{}

# Get the properties and methods for hashtable object
$serverConfig | Get-Member

上述用于获取哈希表对象可用的属性和方法的 PowerShell 脚本的输出是:

[玩转系统] PowerShell 哈希表

在 PowerShell 中将项目添加到哈希表

PowerShell 哈希表对象具有 Add 方法,用于将键值对作为元素添加到哈希表中。

# Create hashtable
$serverConfig = @{}  

# Add value to hashtable
$serverConfig.Add('Server','DB-102') 
$serverConfig.Add('Subnet','255.255.255.0')

# Display the Hashtable
$serverConfig 

在上面的 PowerShell 脚本中,我们创建了哈希表 $serverConfig。使用哈希表对象的Add()方法添加键值对。

$serverConfig 哈希表存储数据的键值对。

上述 $serverConfig 哈希表的输出是:

# Create hashtable
$serverConfig = @{}  

# Add value to hashtable
$serverConfig.Add('Server','DB-102') 
$serverConfig.Add('Subnet','255.255.255.0')

# Display the Hashtable
$serverConfig 
                                                                                                                                                                  
Name                           Value
----                           -----
Server                         DB-102
Subnet                         255.255.255.0

$serverConfig 哈希表包含不同数据类型(如字符串和字母数字)的键值对数据。

PowerShell 提供了另一种方法将所有键值对添加到 Hashtable,而不是逐一添加。

# Add the key-value pairs to hashtable all at once
$stage1Server = @{
"Server" = "IT-101"
"Port" = "80"
"Username" = "admin"
"DBServer" = "DB-CORP1"
"DBUser" = "dbadmin"
"DbTimeOut" = 120
}

在上面的 PowerShell 脚本中,使用 @{} 表示法创建 $stage1Server 哈希表,并一次性添加所有键值对。 等号 (=) 用于将键与其值分开。

上述哈希表的输出是:

PS D:\> $stage1Server                                                                                                                                                                        
Name                           Value
----                           -----
Port                           80
DbTimeOut                      120
Server                         IT-101
DBServer                       DB-CORP1
Username                       admin
DBUser                         dbadmin

从哈希表访问项目

PowerShell 提供了多种访问哈希表中的项目的方法。

使用方括号[]表示法从哈希表中检索项目。您必须提供特定的密钥才能访问与该密钥关联的值或数据。

让我们考虑上面创建的 $stage1Server 哈希表,并使用 [] (方括号)来访问该项目。

$stage1Server["Server"]

在上面的 PowerShell 脚本中,哈希表 $stage1Server 对象使用 [] 来访问与特定键“Server”关联的值。

上述脚本的输出从 PowerShell 中的哈希表返回服务器值。

PS D:\> $stage1Server["Server"]                                                                                                                                                              IT-101

获取哈希表中元素的另一种方法是使用 Item 参数化属性,该属性将特定键作为输入并检索与键关联的值。

PS D:\> $stage1Server.Item("UserName")                                                                                                                                                       admin
PS D:\>  

您可以使用键访问哈希表中的项目。在以下 PowerShell 脚本中,要获取端口号,请使用哈希表对象及其键。

PS D:\> $stage1Server.Port                                                                                                                                                                   80
PS D:\>    

在 PowerShell 中循环遍历哈希表

PowerShell 提供了多种循环哈希表以访问哈希表元素的方法。

使用 Foreach 循环遍历哈希表

Powershell 有一个 foreach 语句来迭代项目集合。您可以使用 foreach 来遍历哈希表集合。

让我们使用 $stage1Server 哈希表通过 foreach 访问元素。

foreach($key in $stage1Server.Keys) {
    Write-Host "$($key) is $($stage1Server[$key])"
}

上面的 PowerShell 脚本使用 foreach 语句来迭代哈希表 $stage1Server 中的哈希表键。它使用方括号符号[]来访问与键关联的值。

上述用于循环哈希表的 PowerShell 脚本的输出是:

PS C:\> foreach($key in $stage1Server.Keys) {
    Write-Host "$($key) is $($stage1Server[$key])"
}

Port is 80
DbTimeOut is 120
Server is IT-101
DBServer is DB-CORP1
Username is admin
DBUser is dbadmin

PS C:\> 

使用 ForEach-Object 迭代哈希表

使用 PowerShell 中的 Foreach-object,您可以循环遍历哈希表。

让我们考虑 $stage1Server 哈希表来了解如何使用 Foreach-object 进行迭代。

$stage1Server.keys|ForEach-Object {
    Write-Host "$($_) is $($stage1Server[$_])"
}

在上面的 PowerShell 脚本中,$stage1Server.Keys 通过管道传输到 ForEach-Object 命令进行迭代。在脚本块中,它使用 Write-Host 命令显示与哈希表中的关键项关联的值。

上述脚本的输出是:

Port is 80
DbTimeOut is 120
Server is IT-101
DBServer is DB-CORP1
Username is admin
DBUser is dbadmin

使用 GetEnumerator 循环遍历哈希表

PowerShell 哈希表对象具有可用于枚举集合的 GetEnumerator() 方法。

在以下脚本中,$stage1Server 哈希表使用 GetEnumerator() 方法并将输出通过管道传输到 foreach-object 以迭代枚举器对象。在脚本块中,它使用键和值来检索哈希表中的项目。

$stage1Server.GetEnumerator()|ForEach-Object {
    Write-Host "$($_.Key) : $($_.Value)"
}

上述 PowerShell 脚本的输出是:

Port : 80
DbTimeOut : 120
Server : IT-101
DBServer : DB-CORP1
Username : admin
DBUser : dbadmin

在 PowerShell 中修改哈希表

您可以使用不同的方式修改哈希表中的项目。

修改哈希表中特定键的值的简单方法是使用方括号表示法[]来指定键并分配值。它将覆盖与哈希表中的键关联的现有值。

$stage1Server["Server"] = "IT-101A"

输出 :

PS D:\> $stage1Server["Server"] = "IT-101A"                                                                                                                                                  PS D:\> $stage1Server                                                                                                                                                                        
Name                           Value
----                           -----
Port                           80
DbTimeOut                      120
Server                         IT-101A
DBServer                       DB-CORP1
Username                       admin
DBUser                         dbadmin

更改或更新哈希表中的值的另一种方法是使用 Item 参数化属性。

哈希表对象的 Item 参数化属性具有 {get; set;} 检索并设置值。

$stage1Server.Item("Server") = "IT-101-South" 

输出 :

PS D:\> $stage1Server.Item("Server") = "IT-101-South"                                                                                                                                        PS D:\> $stage1Server                                                                                                                                                                        
Name                           Value
----                           -----
Port                           80
DbTimeOut                      120
Server                         IT-101-South
DBServer                       DB-CORP1
Username                       admin
DBUser                         dbadmin

删除哈希表中的项目

PowerShell 哈希表对象具有 Remove() 方法,该方法将键作为输入参数,从哈希表中删除键值对。

$stage1Server = @{
"Server" = "IT-101"
"Port" = "80"
"Username" = "admin"
"DBServer" = "DB-CORP1"
"DBUser" = "dbadmin"
"DbTimeOut" = 120
}
# Remove element from the hashtable
$stage1Server.Remove("DBUser")

上述 PowerShell 脚本的输出从哈希表中删除键值对。

PS D:\> $stage1Server.Remove("DBUser")                                                                                                                                                       PS D:\> $stage1Server                                                                                                                                                                        
Name                           Value
----                           -----
Port                           80
DbTimeOut                      120
Server                         IT-101-South
DBServer                       DB-CORP1
Username                       admin

结论

PowerShell 中的哈希表是一种非常强大的数据结构,可用于轻松地添加、修改、删除和检索哈希表中的项目。

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

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

取消回复欢迎 发表评论:

关灯