Quantcast
Channel: The Official Scripting Guys Forum! forum
Viewing all articles
Browse latest Browse all 15028

Custom types on function return values

$
0
0

Hello,

I have the following function

function Get-Space
{<#
.Synopsis
    Returns disk space details on computer 
.Example
    Get-Space $env:ComputerName
    Get space for the computer you are currently on
.Example
    Get-Space 'Computer1', 'Computer2'
    Pass a csv list of computers directly into the command
.Example
    Get-Space -ComputerName (Get-Content c:\serverlist.txt)
    Get a list of computers from a file and then use that list for getting space for the servers.
.Parameter ComputerName
    A string, or an array of strings, containg the list of computers that you want to get space for.
.Parameter DiskType
    Type of disk you are looking for.  3 is default as this is local/san disk.  2 = external drives.
.Notes
    NAME:  Get-Space
    AUTHOR: Nathan Heaivilin
    LASTEDIT: 7/16/2014
.Description
    The Get-Space function provides the user with a method for quering local 
    and/or remote machines to get Disk Name, Label, BlockSize, Capacity (GB), Free Space (GB), 
    and % FreeSpace.  This will not work in untrusted domains.
 #>
    [CmdletBinding()]
    param (
        [Parameter(Position=0, Mandatory=$true, HelpMessage = 'Physical Machine Name to get for space.  Also accepts an arrary of computers names')]
        [string[]]$ComputerName,
        [Parameter(Mandatory=$false, HelpMessage = 'DiskType (Default is 3)')]
        [ValidateSet (2,3)]
        [int]$DiskType = 3
    )

    $OutPut = @()

	foreach ($disk in  (Get-WmiObject -ComputerName $ComputerName -Class win32_Volume |
			Where-Object {$_.DriveType -eq $DiskType -and $_.Name -like "*$SearchName*"} |
 Select-Object SystemName, Name, Label, Capacity, FreeSpace
            ))
            {
			    $tmp = New-Object -TypeName PSObject

               
                $tmp | Add-Member -Name ComputerName -MemberType NoteProperty -Value ([string]$disk.SystemName)
                $tmp | Add-Member -Name DriveName -MemberType NoteProperty -Value ([string]$disk.Name)
                $tmp | Add-Member -Name DriveLabel -MemberType NoteProperty -Value ([string]$disk.Label) 
                $tmp | Add-Member -Name Capacity_GB -MemberType NoteProperty -Value ("{0:N3}" -f ([Int64]$disk.Capacity/1073741824)) 
                $tmp | Add-Member -Name FreeSpace_GB -MemberType NoteProperty -Value ("{0:N3}" -f ([Int64]$disk.FreeSpace/1073741824)) 
                $tmp | Add-Member -Name FreeSpace% -MemberType NoteProperty -Value ("{0:P}" -f ([int64]$disk.FreeSpace/[int64]$disk.Capacity)) 
                $OutPut += $tmp
            }
           $OutPut
}

Now, the function works just fine and returns the space data as needed.  However, when I go to sort the values by either FreeSpace_GB or FreeSpace%, the sort doens't work correctly.

So then I do a 

Get-Space $Env:ComputerName | gm

and what I notice is this:

Name         MemberType   Definition                                                              
----         ----------   ----------                                                              
Equals       Method       bool Equals(System.Object obj)                                           
GetHashCode  Method       int GetHashCode()                                                        
GetType      Method       type GetType()                                                          
ToString     Method       string ToString()                                                       
Capacity_GB  NoteProperty System.String Capacity_GB=0.487                                          
ComputerName NoteProperty System.String ComputerName=P054SQLCL0601                                 
DriveLabel   NoteProperty System.String DriveLabel=BDEDrive                                        
DriveName    NoteProperty System.String DriveName=\\?\Volume{e2908b85-cbdf-11e3-9027-806e6f6e6963}\
FreeSpace%   NoteProperty System.String FreeSpace%=90.60 %                                         
FreeSpace_GB NoteProperty System.String FreeSpace_GB=0.442       

so, it appears that the FreeSpace_GB and FreeSpace% are being registered as System.String values, rather than say a double.  I have tried various ideas for chaning this, but I haven't had any luck.

Does anyone have any input on why the values aren't getting sorted correctly, and what I can do to correct it?  

get-space $env:ComputerName | Sort-Object -property FreeSpace%

Thank you very much for your help.

Nathan



Viewing all articles
Browse latest Browse all 15028

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>