在进行分割时获取有关从UInt64转换的错误?

时间:2016-04-25 17:49:27

标签: powershell

我试图在远程计算机上使用win32_logicaldisk获取可用磁盘空间,但是从UInt64转换时出现错误。不知道为什么会失败。我还想要使用逗号数字分组格式化数字。

# Find free disk space on another computer
$ConvertToGB = (1024 * 1024 * 1024)
$disk = get-WmiObject win32_logicaldisk -Computername "MyServerName" -Filter "DeviceID='D:'" | Select-Object $disk.FreeSpace
$space = $disk.FreeSpace / $ConvertToGB
Write-Output "{0:N}" -f $space

我得到的错误是:

> Select-Object : Cannot convert System.UInt64 to one of the following
> types {System.String, System.Management.Automation.ScriptBlock}. At
> line:14 char:91
> + ... viceID='D:'" | Select-Object $disk.FreeSpace
> +                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : InvalidArgument: (:) [Select-Object], NotSupportedException
>     + FullyQualifiedErrorId : DictionaryKeyUnknownType,Microsoft.PowerShell.Commands.SelectObjectCommand
> Cannot find an overload for "ToInt32" and the argument count: "0". At
> line:15 char:1
> + $space = $disk.FreeSpace.ToInt32() / $ConvertToGB
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>     + CategoryInfo          : NotSpecified: (:) [], MethodException
>     + FullyQualifiedErrorId : MethodCountCouldNotFindBest

1 个答案:

答案 0 :(得分:1)

我不明白代码的一部分。

$disk = get-WmiObject win32_logicaldisk -Computername "MyServerName" -Filter "DeviceID='D:'" | Select-Object $disk.FreeSpace

如果您在变量| Select-Object $disk.FreeSpace中指定了之后,为什么要指定$space

$space = $disk.FreeSpace / $ConvertToGB

如果您在没有| Select-Object $disk.FreeSpace的情况下运行代码,则任何工作都可以:

# Find free disk space on another computer
$ConvertToGB = (1024 * 1024 * 1024)
$disk = get-WmiObject win32_logicaldisk -Computername "MyServerName" -Filter "DeviceID='C:'"
$space = $disk.FreeSpace / $ConvertToGB
Write-Output "{0:N}" -f $space

祝你有个美好的一天(