ConvertFrom-String 返回符号而不是文本 [英] ConvertFrom-String returns symbol not text
问题描述
使用学生文本名称 Infinity 执行以下代码将返回无穷大符号而不是实际文本.任何人都可以解释为什么会出现这种情况以及可以采取哪些措施来纠正.
Performing the following code with student text name Infinity returns the infinity symbol and not the actual text. Can anyone explain why this is the case and what can be done to rectify.
$person="Infinity,One"
$personsplit=$person | ConvertFrom-String -Delimiter "," -PropertyNames firstname,surname
$personsplit
返回以下输出
名字现在是一个符号而不是文本数据?
the first name is now a symbol and not text data?
期望看到输出
first name surname
--------- -------
Infinity One
但是,它返回以下内容
firstname surname
--------- -------
∞ One
对于任何有兴趣的人,我目前正在运行以下版本的 powershell
For anyone interested Im currently running the following version of powershell
PS C:\WINDOWS\system32> $psversiontable
Name Value
---- -----
PSVersion 5.1.17763.503
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.17763.503
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
推荐答案
您似乎发现了 ConvertFrom-String
尝试自动转换其值的方式的错误/怪癖创建,并且在您的情况下似乎采用字符串 'Infinity' 而转换为 [single]::PositiveInfinity
.
It looks like you've found a bug/quirk in the way that ConvertFrom-String
attempts to automatically cast the values that it creates, and in your case seems to take the string 'Infinity' and instead cast is as [single]::PositiveInfinity
.
另一种更安全的解决方案是不使用 ConvertFrom-String
而是执行以下操作:
An alternative safer solution would be to not use ConvertFrom-String
and do something like this instead:
$person="Infinity,One"
$personsplit = $person -split ','
[pscustomobject]@{
firstname = $personsplit[0]
lastname = $personsplit[1]
}
如果您不介意将它们放在不同的变量中,您也可以这样做:
If you don't mind them being in separate variables you could also do this:
$firstname,$lastname = $person -split ','
这篇关于ConvertFrom-String 返回符号而不是文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!