none
Multiple logical disks as custom objects RRS feed

  • General discussion

  • Hello All,

    I have been working with PowerShell for a few months now and my goal is to help my colleagues perform the same tasks a little faster.

    As such I took on the responsibility of creating a little GUI tool to find some details about a remote computer.

    I have created as advanced function with a custom object.

    I am limited to PowerShell V2 as this is the version used in the company.

    the function is as below:

    <#
    .Synopsis
       Short description
    .DESCRIPTION
       Long description
    .EXAMPLE
       Example of how to use this cmdlet
    .EXAMPLE
       Another example of how to use this cmdlet
    #>
    function Get-PartitionInfo
    {
        [CmdletBinding()]
        Param
        (
            # Computer Name
            [Parameter(Mandatory=$true,ValueFromPipelineByPropertyName=$true,Position=0)]
            [string[]]$ComputerName
        )

        Process
        {
            Write-Verbose "Testing Machine"

            if(Test-Connection $computername -Count 1 -Quiet)
            {
                Try
                {
                    Write-Verbose "Gathering Data"

                    $partitions = Get-WmiObject -Class win32_logicalDisk -ComputerName $computername -ErrorAction Stop

                        $myobj = @{

                          'Device ID' = $partitions.DeviceID
                            'Access' = $partitions.Access
                            'Compressed' = $partitions.Compressed
                            'Drive Type' = $partitions.DriveType
                            'File System' = $partitions.FileSystem
                            'Free Space' = $partitions.FreeSpace
                            'Media Type' = $partitions.MediaType
                            'Size' = $partitions.Size
                            'Name' = $partitions.Name
                        }

                        Write-Output "Partition information"
                        Write-Output (New-Object -TypeName PSObject -Property $myobj)
                }
                Catch
                {
                    Write-Output "Machine not online or PowerShell is unable to read it"
                    Write-Output "$($Error[0])"
                }
            }
            else
            {
                Write-Output "Machine not online"
            }
        }

    }

    Please excuse the fact that is is not complete, after I get it to work it will be polished.

    My problem is as follows:

    The output of this function is correct but, it is as below:

    Partition information


    Drive Type  : {3, 5, 3}
    Compressed  : {False, $null, False}
    Device ID   : {C:, D:, E:}
    Size        : {167772155904, $null, 5356126208}
    Name        : {C:, D:, E:}
    Free Space  : {71496396800, $null, 5356109824}
    Access      : {0, $null, 0}
    Media Type  : {12, 11, 12}
    File System : {NTFS, $null, FAT32}

    What I am trying to do is to separate the partitions but have not had any success.

    Searched high and low for a solution to list custom objects one by one.

    Anyone have any ideea if this can be done?

    Thursday, April 23, 2015 7:31 AM

All replies

  • First read:


    PS C:\> help about_Pipelines

    then read:


    PS C:\> help ForEach-Object


    -- Bill Stewart [Bill_Stewart]

    Thursday, April 23, 2015 12:58 PM
    Moderator
  • You have to enumerate the results of the WMI.

    You are also sort of off when it comes to how to develop output with PowerShell.

    You are tryingtoo do too many things that you do not understand and they are all conflicting. YOu cannot nis Write-Outpur statements into a colleciotn or it will not output correctly. Study the basics of how PowerShell does output

    Here is how to get the output objects.  Test it manually with a number of computers to see how the output changes.  Try to understand why.

    $computer=$env:computername
    if(Test-Connection $computer -Count 1 -Quiet){
        Get-WmiObject -Class win32_logicalDisk -ComputerName $computer |
            Select Access,Compressed,DriveType,FileSystem,FreeSpace,MediaType,Size,Name
    }else{
        Write-Host 'Machine not online or PowerShell is unable to read it'
    } 

    Once you understand why I did it this way and why it works you will have the basics needed to understand how to generalize.

    Try not to write or use code that you do notunderstand.  Test and ask question.  Build the knowledge base from the ground up.


    \_(ツ)_/

    Thursday, April 23, 2015 1:00 PM
  • Thank you for your help, I see the error in my method; not reading before taking on the challenge.

    Friday, April 24, 2015 6:07 AM
  • Thank you for your help, I see the error in my method; not reading before taking on the challenge.

    PowerShell makes acquiring information and presenting it very simple once you understand how it is wired.   Most things that are needed administratively are very simple to write. Whenever you are writing more than a few lines you should ask yourself if there is an easier way. PowerShell has been designed so that things that Admins need to do are most complete.

    Look into the "pipeline".  It is a very powerful concept. The ability to feed the results of one command into another gives PowerShell much of its power. 

    The following is a repetitive pattern:

    <get data> | <convert data> | <output data>

    get-process -computer w8test|?{$_.WorkingSet64 -gt 100Mb} |select MAchineName, Name, path

    Now save it for Excel:

    get-process -computer w8test|?{$_.WorkingSet64 -gt 100Mb} |select MAchineName, Name, path | Export-Csv procs.csv

    Many things are just additive.


    \_(ツ)_/

    Friday, April 24, 2015 9:18 AM