question

AngelaClaudio-6862 avatar image
0 Votes"
AngelaClaudio-6862 asked RichMatheisen-8856 commented

Method call failed


Hi everyone i ran this code :

""""$Space= Get-PSDrive | Where-object {$.Provider -like "FileSystem" -and $.Free -gt 0
$Capacity= $Space.used + $Space.free
$OccupationRate = $Space.used / $Capacity""""

and it returns me this error:

Method call failed. [System.Object []] does not contain a named method
'op_Division'.
In line: 11 characters: 1
+ $ OccupationRate = $ Space.Used / $ Capacity
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (op_Division: String) [], RuntimeException
+ Fully Qualified Error ID: MethodNotFound

Method call failed. [System.Object []] does not contain a named method
'op_Division'.
In line: 11 characters: 1
+ $ OccupationRate = $ Space.Used / $ Capacity
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo: InvalidOperation: (op_Division: String) [], RuntimeException
+ Fully Qualified Error ID: MethodNotFound

Method call failed. [System.Object []] does not contain a named method
'op_Division'.


What's the problem? I was following a video I checked all the syntax is correct.

windows-server-powershell
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If the video was in the Internet it must be correct! Right? :-)

I think this is more of what you wanted:


 Get-PSDrive | Where-object {$_.Provider.Name -like "FileSystem" -and $_.Free -gt 0} |
     ForEach-Object{
         [PSCustomObject]@{
             Capacity = $_.used + $_.free
             OccupationRate = $_.used / ($_.used + $_.free)
         }
     }
0 Votes 0 ·

1 Answer

Soccan avatar image
1 Vote"
Soccan answered Soccan commented

I think the problem is that you are having more than one psdrive returned, that gives you an array and from there on it is a downhill battle, you need to modify your code to loop through the array in some way, maybe a foreach or a foreach-object. I think the guy in your video had a system with only one drive...

· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

To be honest I think Get-PSDrive is no good for this, because it returns TEMP as an extra drive and for me that equals it showing "C:" twice. Maybe Get-Volume would be better?

1 Vote 1 ·