2016-07-20 111 views
1

我試圖將冒號分隔字符串轉換爲PowerShell字典。以下是字符串。將冒號分隔字符串轉換爲PowerShell字典

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

我在$inputkeyvalues可變兩個關鍵值對的和那些由管分隔符隔開。 第一個是:Appsetting:true 第二個是:環境:prod

我試圖轉換爲PowerShell字典。最終的輸出應該是這樣的,

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

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

可有人請建議我爲這個可能的解決方案。提前致謝。

回答

0

使用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 
} 
+0

太謝謝你了。它按預期工作。 – mahesh

相關問題