Share via


ConvertFrom-StringData

將包含一或多個索引鍵與值組的字串轉換成雜湊表。

Syntax

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

Description

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

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

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

範例

範例 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:轉換包含批註的這裡字串

這個範例會將包含批注和多個索引鍵/值組的 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 忽略單行批注,但 # 字元必須是該行的第一個非空格符。 忽略 之後 # 這一行的所有字元。

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

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

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

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

為了滿足每個機碼/值組必須位於個別行的條件,字串會使用 PowerShell 換行符 ('n) 來分隔配對。

範例 4:在腳本的 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 區段中不允許變數。

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

這個範例顯示您可以使用管線運算子 (|) 將字串傳送至 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.

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

此範例示範如何使用逸出字元來建立新行,並在源數據中傳回字元。 逸出序列 \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.

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

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

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

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

參數

-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

您可以使用管線將包含索引鍵/值群組的字串傳送至 ConvertFrom-StringData

輸出

Hashtable

此 Cmdlet 會傳回它從機碼/值組建立的哈希表。

備註

here-string 是由一或多行組成的字串,其中的引號會解譯為常值。

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