请考虑以下Powershell代码段:
[Uint64] $Memory = 1GB
[string] $MemoryFromString = "1GB"
[Uint64] $ConvertedMemory = [Convert]::ToUInt64($MemoryFromString)
Run Code Online (Sandbox Code Playgroud)
第3行失败了:
Exception calling "ToUInt64" with "1" argument(s): "Input string was not in a correct format."
At line:1 char:1
+ [Uint64]$ConvertedMemory = [Convert]::ToUInt64($MemoryFromString)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : FormatException
Run Code Online (Sandbox Code Playgroud)
如果我查看以下内容$Memory:
PS C:\> $Memory
1073741824
Run Code Online (Sandbox Code Playgroud)
这很好.
那么,如何在Powershell中将值"1GB"从字符串转换为UInt64?