bitshift
bitshift

Reputation: 6852

Getting errors about converting from UInt64 when doing division?

Im trying to get the free diskspace using win32_logicaldisk on a remote computer and I get an error about converting from a UInt64. Not sure why this is failing. I also want the number formatted using digit grouping with commas.

# 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

The error I get is:

> 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

Upvotes: 1

Views: 214

Answers (1)

Drumor
Drumor

Reputation: 421

I don't understand a part of the code.

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

Why you specify | Select-Object $disk.FreeSpace if you specify after in the variable $space?

$space = $disk.FreeSpace / $ConvertToGB

if you run your code without | Select-Object $disk.FreeSpace, anything works:

# 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

Have a good day (:

Upvotes: 1

Related Questions