ConvertFrom-StringData

將包含一或多個索引鍵和值組的字串轉換成哈希表。

Syntax

ConvertFrom-StringData
                [-StringData] <String>
                [[-Delimiter] <Char>]
                [<CommonParameters>]

Description

Cmdlet 會將 ConvertFrom-StringData 包含一或多個索引鍵和值組的字串轉換成哈希表。 由於每個機碼/值組都必須位於個別行上,因此這裡字串通常用來做為輸入格式。 根據預設,索引鍵必須以等號 (=) 字元分隔值

Cmdlet ConvertFrom-StringData 會被視為可在腳本或函式之 DATA 區段中使用的安全 Cmdlet。 在 DATA 區段中使用時,字串的內容必須符合 DATA 區段的規則。 如需詳細資訊,請參閱 about_Data_Sections

ConvertFrom-StringData 支援傳統機器翻譯工具允許的逸出字元序列。 也就是說,Cmdlet 可以使用 Regex.Unescape 方法,將反斜杠 (\) 解譯為字串數據中的逸出字元,而不是通常表示腳本中行尾的 PowerShell 反引號字元 (`)。 在 here-string 內,反引號字元無法運作。 您也可以使用前面的反斜杠來逸出結果中的常值反斜杠,如下所示: \\。 未逸出的反斜杠字元,例如檔案路徑中常用的反斜杠字元,可以在結果中轉譯為不合法的逸出序列。

PowerShell 7 會新增 Delimiter 參數。

範例

範例 1:將單引號的 here-string 轉換成哈希表

此範例會將使用者訊息的單引號字串轉換成哈希表。 在單引號字串中,不會以變數取代值,而且不會評估表達式。 Cmdlet 會將 ConvertFrom-StringData 變數中的 $Here 值轉換成哈希表。

$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
ConvertFrom-StringData -StringData $Here

Name                           Value
----                           -----
Msg3                           The specified variable does not exist.
Msg2                           Credentials are required for this command.
Msg1                           The string parameter is required.

範例 2:使用不同的分隔符轉換字串數據

此範例示範如何將使用不同字元做為分隔符的字串數據進行轉換。 在此範例中,字串數據使用管道字元 (|) 作為分隔符。

$StringData = @'
color|red
model|coupe
year|1965
condition|mint
'@
$carData = ConvertFrom-StringData -StringData $StringData -Delimiter '|'
$carData

Name                           Value
----                           -----
condition                      mint
model                          coupe
color                          red
year                           1965

範例 3:轉換包含批注的 here-string

此範例會將包含批注和多個索引鍵/值組的 here-string 轉換成哈希表。

ConvertFrom-StringData -StringData @'
Name = Disks.ps1

# Category is optional.

Category = Storage
Cost = Free
'@

Name                           Value
----                           -----
Cost                           Free
Category                       Storage
Name                           Disks.ps1

StringData 參數的值是 here-string,而不是包含 here-string 的變數。 任一格式都有效。 here-string 包含其中一個字串的相關批注。 ConvertFrom-StringData 忽略單行批注,但 # 字元必須是該行的第一個非空格符。 會忽略 之後 # 行中的所有字元。

範例 4:將字串轉換成哈希表

這個範例會將一般雙引號字串 (不是 here-string) 轉換成哈希表,並將它儲存在變數中 $A

$A = ConvertFrom-StringData -StringData "Top = Red `n Bottom = Blue"
$A

Name             Value
----             -----
Bottom           Blue
Top              Red

若要滿足每個索引鍵/值組必須位於個別行的條件,字串會使用 PowerShell 換行符 (`n) 來分隔配對。

範例 5:在腳本的 DATA 區段中使用 ConvertFrom-StringData

此範例顯示 ConvertFrom-StringData 文稿之 DATA 區段中所使用的命令。 DATA段下方的語句會向用戶顯示文字。

$TextMsgs = DATA {
ConvertFrom-StringData @'
Text001 = The $Notebook variable contains the name of the user's system notebook.
Text002 = The $MyNotebook variable contains the name of the user's private notebook.
'@
}
$TextMsgs

Name             Value
----             -----
Text001          The $Notebook variable contains the name of the user's system notebook.
Text002          The $MyNotebook variable contains the name of the user's private notebook.

因為文字包含變數名稱,所以它必須以單引號字串括住,以便以常值方式解譯變數,而不會展開變數。 DATA 區段中不允許變數。

範例 6:使用管線運算元傳遞字串

這個範例示範您可以使用管線運算子 (|) 將字串傳送至 ConvertFrom-StringData。 變數的值 $Here 會透過管道傳送至 ConvertFrom-StringData 變數,以及變數中 $Hash 的結果。

$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
$Hash = $Here | ConvertFrom-StringData
$Hash

Name     Value
----     -----
Msg3     The specified variable does not exist.
Msg2     Credentials are required for this command.
Msg1     The string parameter is required.

範例 7:使用逸出字元新增行並傳回字元

這個範例示範如何使用逸出字元在源數據中建立新的行和傳回字元。 逸出序列 \n 是用來在結果哈希表中與名稱或專案相關聯的文本區塊內建立新行。

ConvertFrom-StringData @"
Vincentio = Heaven doth with us as we with torches do,\nNot light them for themselves; for if our virtues\nDid not go forth of us, 'twere all alike\nAs if we had them not.
Angelo = Let there be some more test made of my metal,\nBefore so noble and so great a figure\nBe stamp'd upon it.
"@ | Format-List

Name  : Angelo
Value : Let there be some more test made of my metal,
        Before so noble and so great a figure
        Be stamp'd upon it.

Name  : Vincentio
Value : Heaven doth with us as we with torches do,
        Not light them for themselves; for if our virtues
        Did not go forth of us, 'twere all alike
        As if we had them not.

範例 8:使用反斜杠逸出字元正確轉譯檔案路徑

這個範例示範如何使用字串數據中的反斜杠逸出字元,讓檔案路徑在產生的 ConvertFrom-StringData 哈希表中正確轉譯。 雙反斜杠可確保常值反斜杠字元在哈希表輸出中正確轉譯。

ConvertFrom-StringData "Message=Look in c:\\Windows\\System32"

Name                           Value
----                           -----
Message                        Look in c:\Windows\System32

參數

-Delimiter

用來分隔 索引鍵所轉換字串中值 數據的字元。 預設分隔符元是等號 (=) 字元。 此參數已在PowerShell 7中新增。

Type:Char
Position:1
Default value:'='
Required:False
Accept pipeline input:False
Accept wildcard characters:False

-StringData

指定要轉換的字串。 您可以使用此參數,或使用管線將字串傳送至 ConvertFrom-StringData。 參數名稱是選擇性的。

此參數的值必須是包含一或多個索引鍵/值組的字串。 每個索引鍵/值組都必須位於個別的行上,或每一組都必須以換行符分隔(`n)。

您可以在字串中包含批註,但批注不能與索引鍵/值組位於同一行。 ConvertFrom-StringData 忽略單行批注。 字元 # 必須是行上的第一個非空格符。 會忽略 之後 # 行中的所有字元。 批注不會包含在哈希表中。

here-string 是包含一或多行的字串。 此處字串內的引號會以字面方式解譯為字串數據的一部分。 如需詳細資訊,請參閱 about_Quoting_Rules

Type:String
Position:0
Default value:None
Required:True
Accept pipeline input:True
Accept wildcard characters:False

輸入

String

您可以使用管線將包含索引鍵/值組的字串傳送至此 Cmdlet。

輸出

Hashtable

此 Cmdlet 會傳回它從索引鍵/值組建立的哈希表。

備註

here-string 是由一或多行所組成的字串,其中引號會以字面方式解譯。

此 Cmdlet 在以多個口語語言顯示使用者訊息的腳本中很有用。 您可以使用字典樣式哈希表來隔離文字字串與程式代碼,例如在資源檔中,以及格式化文字字串以用於翻譯工具。