ConvertFrom-StringData
將含有一組或多組索引鍵/值的字串轉換成雜湊表。
語法
ConvertFrom-StringData [-StringData] <string> [<CommonParameters>]
描述
ConvertFrom-StringData Cmdlet 會將含有一組或多組索引鍵/值的字串轉換成雜湊表。因為每組索引鍵/值必須在不同行,通常會使用 here-string 當做輸入格式。
ConvertFrom-StringData Cmdlet 被認為是安全 Cmdlet,可用於指令碼或函數的 DATA 區段。用於 DATA 區段時,字串的內容必須符合 DATA 區段的規則。如需詳細資訊,請參閱 about_Data_Sections。
參數
-StringData <string>
指定要轉換的字串。您可以直接使用此參數,或經由管道將字串輸出至 ConvertFrom-StringData。參數名稱為選擇性。
這個參數的值必須是括在單引號內的字串 (單引號字串) 或是括在雙引號內的字串 (雙引號字串),或者是包含一組或多組索引鍵/值的 here-string。每一組索引鍵/值都必須在不同行,或者以新行字元 (`n) 分隔。
您可以在字串內加上註解,但是註解不能與索引鍵/值組位於同一行。註解不會包含在雜湊表中。
here-string 是由一行或多行組成的字串,當中的引號會被逐字解譯。如需詳細資訊,請參閱 about_Quoting_Rules。
必要? |
true |
位置? |
1 |
預設值 |
|
接受管線輸入? |
true (ByValue) |
接受萬用字元? |
false |
<CommonParameters>
這個 Cmdlet 支援一般參數:-Verbose、-Debug、-ErrorAction、-ErrorVariable、-OutBuffer 和 -OutVariable。如需詳細資訊,請參閱 about_Commonparameters.
輸入和輸出
輸入型別是可經由管道輸出至 Cmdlet 的物件型別。傳回型別則是 Cmdlet 所傳回的物件型別。
輸入 |
System.String 您可經由管道將包含索引鍵/值組的字串輸出至 ConvertFrom-StringData。 |
輸出 |
System.Collections.Hashtable ConvertFrom-StringData 會傳回它從索引鍵/值組所建立的雜湊表。 |
附註
here-string 是由一行或多行組成的字串,當中的引號會被逐字解譯。如需詳細資訊,請參閱 about_Quoting_Rules。
ConvertFrom-StringData 針對以多國語言顯示使用者訊息的指令碼很有用。您可以使用字典式雜湊表來隔離像資源檔這樣的檔案中的文字字串與程式碼,並且格式化文字字串,以便在翻譯工具中使用。
範例 1
C:\PS>$here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
C:\PS> 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.
描述
-----------
這些命令會將以單引號括住的使用者訊息 here-string 轉換成雜湊表。在以單引號括住的字串中,變數不會被值取代,運算式也不會被評估。
第一個命令會建立 here-string,並將它儲存在 $here 變數中。
第二個命令會使用 ConvertFrom-StringData Cmdlet 將 $here 變數中的 here-string 轉換成雜湊表。
範例 2
C:\PS>$p = @"
ISE = Windows PowerShell Integrated Scripting Environment
"@
C:\PS> $p | get-member
TypeName: System.String
Name MemberType Definition
---- ---------- ----------
Clone Method System.Object Clone()
...
C:\PS> $hash = convertfrom-stringdata -stringdata $p
C:\PS> $hash | get-member
TypeName: System.Collections.Hashtable
Name MemberType Definition
---- ---------- ----------
Add Method System.Void Add(Object key, Object
...
描述
-----------
這些命令示範 ConvertFrom-StringData 實際上將 here-string 轉換成雜湊表。
第一個命令會建立以雙引號括住的 here-string (其中含有一組索引鍵/值),然後將其儲存到 $p 變數。
第二個命令會使用管線運算子 (|) 將 $p 變數傳送給 Get-Member Cmdlet。結果會顯示 $p 是字串 (System.String)。
第三個命令會使用 ConvertFrom-StringData Cmdlet 將 $p 變數中的 here-string 轉換成雜湊表,然後將結果儲存到 $hash 變數。
最後一個命令會使用管線運算子 (|) 將 $hash 變數傳送給 Get-Member Cmdlet。結果會顯示 $hash 變數的內容是雜湊表 (System.Collections.Hashtable)。
範例 3
C:\PS>convertfrom-stringdata -stringdata @'
Name = Disks.ps1
# Category is optional.
Category = Storage
Cost = Free
'@
Name Value
---- -----
Cost Free
Category Storage
Name Disks.ps1
描述
-----------
這個命令會將包含多組索引鍵/值而且以單引號括住的 here-string 轉換成雜湊表。
在這個命令中,StringData 參數的值是 here-string,而不是含有 here-string 的變數。不過,這兩種格式都有效。
here-string 內包含有關其中一個字串的註解。只要註解與索引鍵/值組位於不同行,註解在字串中就是有效的。
範例 4
C:\PS>$a = convertfrom-stringdata -stringdata "Top = Red `n Bottom = Blue"
C:\PS> "Top = " + $a.Top
Top = Red
C:\PS> "Bottom = " + $a.Bottom
Bottom = Blue
描述
-----------
這個範例會將以一般雙引號括住的字串 (非 here-string) 轉換成雜湊表,然後將其儲存到 $a 變數。
為了滿足每組索引鍵/值必須在不同行的條件,這個範例會以 Windows PowerShell 新行字元 (`n) 來進行分隔。
結果為輸出的雜湊表。其他命令則會顯示輸出。
範例 5
C:\PS>$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.
'@
}
C:\PS> $TextMsgs.Text001
The $Notebook variable contains the name of the user's system notebook.
C:\PS> $TextMsgs.Text002
The $MyNotebook variable contains the name of the user's private notebook.
描述
-----------
這個範例會顯示指令碼的 DATA 區段中所使用的 ConvertFrom-StringData 命令。在 DATA 區段下的陳述式會向使用者顯示文字。
因為文字內含變數名稱,所以必須置於以單引號括住的字串內,如此變數就會被逐字解譯,而不被帶入變數值。DATA 區段中不允許有變數。
範例 6
C:\PS>$here = @'
Msg1 = The string parameter is required.
Msg2 = Credentials are required for this command.
Msg3 = The specified variable does not exist.
'@
C:\PS> $hash = $here | convertfrom-stringdata
C:\PS> $hash
Name Value
---- -----
Msg3 The specified variable does not exist.
Msg2 Credentials are required for this command.
Msg1 The string parameter is required.
描述
-----------
這個範例示範您可以用管線運算子 (|) 將字串傳送給 ConvertFrom-StringData。
第一個命令會將 here-string 儲存到 $here 變數。第二個命令會使用管線運算子 (|) 將 $here 變數傳送給 ConvertFrom-StringData,然後將結果儲存到 $hash 變數。
最後一個命令會顯示 $hash 變數的內容。
請參閱
概念
about_Data_Sections
about_Quoting_Rules
about_Script_Internationalization