我正在嘗試將此字串設定為我的密鑰:
a -long " string that - has. double and single quotes and dashes and dots
該字串是 String Builder ToString() 方法的產物。
Hashtable 的初始化如下:$myHashtable = @{ }
這是錯誤:無法將值“stringAbove”轉換為型別“System.Int32”。錯誤:“輸入字串的格式不正確。”
我嘗試用反引號轉義雙引號。但仍然得到同樣的錯誤。
$resultFromToString = $myBuilder.ToString()
$myHashtable[$resultFromToString] = @{
One = $one;
Two = $two;
}
uj5u.com熱心網友回復:
錯誤訊息暗示$myHashtable包含一個陣列,而不是一個哈希表。
- 要確定 的實際型別
$myHashtable,請執行$myHashtable.GetType()或Get-Member -InputObject $myHashtable
此示例表明您的字串沒有問題,因為具有任何值的字串 - 甚至''- 都可以用作哈希表鍵:
$myHashtable = @{} # Initialize.
$myHashtable['a - long string that has " and '' quotes and - and .'] = 'foo'
$myHashtable # Output
輸出:
Name Value
---- -----
a - long string that has " an… foo
至于你嘗試了什么:
相比之下,如果$myHashtable是一個陣列(無論其元素的型別如何),您的癥狀就會出現,帶有任何字串值(不能轉換為整數),因為只有整數可以用作陣列索引:
$myHashtable = @{}, @{} # !! ARRRAY (of hashtables)
$myHashtable['some key'] = 'foo' # !! FAILS
錯誤輸出:
Cannot convert value "some key" to type "System.Int32". Error: "Input string was not in a correct format."
請注意,正如錯誤訊息所暗示的那樣,PowerShell 會自動嘗試將字串索引轉換為整數,因此類似以下的操作確實有效,也許令人驚訝:
# Same as:
# $myHashtable[0] = 'foo'
# because PowerShell automatically converts to [int]
$myHashtable[' -0 '] = 'foo'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462231.html
標籤:电源外壳
