ConvertFrom-StringData
將包含一個或多個鍵值對的字串轉換為雜湊表。
語法
Default (預設值)
ConvertFrom-StringData
[-StringData] <String>
[[-Delimiter] <Char>]
[<CommonParameters>]
Description
ConvertFrom-StringData指令小程式將包含一個或多個鍵值對的字串轉換成雜湊表。 由於每對鍵值必須在獨立行上,因此常用 here-string 作為輸入格式。 預設情況下, 鍵 必須與 值 間隔一個等號(=)字元。
ConvertFrom-StringData指令長被視為安全的指令碼,可用於data腳本或函式的某個區段。 當用於某 data 個區段時,字串的內容必須符合該 data 區段的規則。 如需詳細資訊,請參閱 about_Data_Sections。
ConvertFrom-StringData 支援傳統機器翻譯工具所允許的跳脫字元序列。 也就是說,cmdlet 可以透過 Regex.Unescape 方法,將字串資料中的反斜線\()解讀為轉義字元,而非 PowerShell 中通常用來表示行尾的反向刻劃字元`()。
在 here-string 裡,backtick 字元無法運作。 你也可以在結果中保留字面上的反斜線,方法是用前置反斜線跳脫,例如: \\。 未逃脫的反斜線字元,例如常見於檔案路徑的字元,可能會在結果中呈現為非法的逃脫序列。
PowerShell 7 新增了 分隔符 參數。
範例
範例 1:將單引號的 here 字串轉換為雜湊表
此範例將使用者訊息的單引號 here 字串轉換成雜湊表。 單引號字串不會用變數替換值,表達式也不會被評估。
指令 ConvertFrom-StringData 本子將變數中的 $Here 值轉換成雜湊表。
$Here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable doesn't exist.
'@
ConvertFrom-StringData -StringData $Here
Name Value
---- -----
Msg3 The specified variable doesn't exist.
Msg2 Credentials are required for this command.
Msg1 The string parameter is required.
範例 2:使用不同的分隔符轉換字串資料
這個範例展示了如何轉換使用不同字元作為分隔符的字串資料。 在此範例中,字串資料使用 pipe 字元(|)作為分隔符。
$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-字串
此範例將包含註解與多個鍵值對的 here-字串轉換成雜湊表。
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指令。
該區段下方 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 doesn't exist.
'@
$Hash = $Here | ConvertFrom-StringData
$Hash
Name Value
---- -----
Msg3 The specified variable doesn't 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
Didn't 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 中加入的。
參數屬性
| 類型: | Char |
| 預設值: | = |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 1 |
| 必要: | False |
| 來自管線的值: | False |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
-StringData
指定要轉換的字串。 你可以使用這個參數,或將字串管道接到 ConvertFrom-StringData。 參數名稱是選擇性的。
此參數的值必須是包含一個或多個鍵值對的字串。 每對鍵值對必須位於獨立行上,或每對必須以換行字元`n()分隔。
你可以在字串中加入註解,但註解不能和鍵值對在同一行。
ConvertFrom-StringData 忽略單行評論。 雜湊字元(#)必須是該行中第一個非空白字元。 雜湊字元(#)之後的行中所有字元都被忽略。 這些註解不會包含在雜湊表中。
here-string 是由一條或多條線組成的串。 here-字串中的引號會被字面上解釋為字串資料的一部分。 如需詳細資訊,請參閱 about_Quoting_Rules。
參數屬性
| 類型: | String |
| 預設值: | None |
| 支援萬用字元: | False |
| 不要顯示: | False |
參數集
(All)
| Position: | 0 |
| 必要: | True |
| 來自管線的值: | True |
| 來自管線按屬性名稱的值: | False |
| 來自剩餘引數的值: | False |
CommonParameters
此 cmdlet 支援常見參數:-Debug、-ErrorAction、-ErrorVariable、-InformationAction、-InformationVariable、-OutBuffer、-OutVariable、-PipelineVariable、-ProgressAction、-Verbose、-WarningAction 和 -WarningVariable。 如需詳細資訊,請參閱 about_CommonParameters。
輸入
String
你可以將包含鍵值對的字串管道輸入這個 cmdlet。
輸出
Hashtable
此指令令會回傳一個由鍵值對建立的雜湊表。
備註
here-string 是由一條或多行組成的字串,在這些行內引號會被字面解釋。
此指令檔可用於顯示多種口語語言使用者訊息的腳本。 你可以使用字典式雜湊表來從程式碼中分離文字字串,例如資源檔案中的文字,並格式化文字字串以便翻譯工具使用。