Powershell 在进行除法时从UInt64转换时出错?
我试图在远程计算机上使用win32_logicaldisk获取可用磁盘空间,但从UInt64转换时出错。我不知道为什么会失败。我还需要使用带逗号的数字分组格式化数字Powershell 在进行除法时从UInt64转换时出错?,powershell,Powershell,我试图在远程计算机上使用win32_logicaldisk获取可用磁盘空间,但从UInt64转换时出错。我不知道为什么会失败。我还需要使用带逗号的数字分组格式化数字 # Find free disk space on another computer $ConvertToGB = (1024 * 1024 * 1024) $disk = get-WmiObject win32_logicaldisk -Computername "MyServerName" -Filter "DeviceID='
# 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
我一部分代码都不懂
$disk = get-WmiObject win32_logicaldisk -Computername "MyServerName" -Filter "DeviceID='D:'" | Select-Object $disk.FreeSpace
为什么指定
|如果在变量$space
中指定after,请选择对象$disk.FreeSpace
$space = $disk.FreeSpace / $ConvertToGB
如果在没有
|的情况下运行代码,请选择对象$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
祝您度过愉快的一天(:
|选择对象$disk.FreeSpace
->|选择对象FreeSpace
或删除;1024*1024*1024
->1gb
)。