将冒号分隔的字符串转换为PowerShell字典

时间:2016-07-20 00:35:45

标签: powershell powershell-v2.0

我正在尝试将冒号分隔的字符串转换为PowerShell字典。以下是字符串。

$inputkeyvalues = "Appsetting:true|environment:prod"

我在$inputkeyvalues变量中有两个键值对,它们由管道分隔符分隔。 第一个是:Appsetting:true 第二个是:环境:刺激

我正在尝试转换为PowerShell字典。最终输出应该是类似的,

Key            Value
-----         -----
Appsetting     true
environment    prod

$Dictionary= New-Object "System.Collections.Generic.Dictionary``2[System.String,System.String]"

有人可以建议我解决这个问题。提前谢谢。

2 个答案:

答案 0 :(得分:3)

使用hashtable

$inputkeyvalues = "Appsetting:true|environment:prod"

# Create hashtable
$Dictionary = @{}
# Split input string into pairs
$inputkeyvalues.Split('|') |ForEach-Object {
    # Split each pair into key and value
    $key,$value = $_.Split(':')
    # Populate $Dictionary
    $Dictionary[$key] = $value
}

答案 1 :(得分:0)

family=binomial(link=logit)
相关问题