Winter Scripting Games 2013 - Practice Event #1

Sizing up the Disks

You have been asked to create a Windows PowerShell advanced function named Get-DiskSizeInfo. It must accept one or more computer names, and use WMI or CIM to query each computer. For each computer, it must display the percentage of free space, drive letter, total size in gigabytes, and free space in gigabytes. If a specified computer cannot be contacted, the function must log the computer name to C:\Errors.txt and display no error message.

Minimum Requirements:

Optional Criteria:


Function Get-DiskSizeInfo { 
        Check the Disk(s) Size and remaining freespace. 
        .PARAMETER ComputerName 
        Specify the computername(s) 
        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage) 
        Get-DiskSizeInfo -ComputerName SERVER01,SERVER02 
        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage) 
        on the Computers SERVER01 and SERVER02 
        Get-Content Computers.txt | Get-DiskSizeInfo 
        Get the drive(s), Disk(s) space, and the FreeSpace (GB and Percentage) 
        for each computers listed in Computers.txt 
        NAME  : Get-DiskSizeInfo 
        AUTHOR: Francois-Xavier Cat 
        EMAIL : 
        DATE  : 2013/02/05  
        [string[]]$ComputerName = $env:COMPUTERNAME 
    BEGIN {# Setup 
    PROCESS { 
        Foreach ($Computer in $ComputerName) { 
            Write-Verbose -Message "ComputerName: $Computer - Getting Disk(s) information..." 
            try { 
                # Set all the parameters required for our query 
                $params = @{'ComputerName'=$Computer; 
                $TryIsOK = $True 
                # Run the query against the current $Computer     
                $Disks = Get-WmiObject @params 
            Catch { 
                "$Computer" | Out-File -Append -FilePath c:\Errors.txt 
                $TryIsOK = $False 
            if ($TryIsOK) { 
                Write-Verbose -Message "ComputerName: $Computer - Formating information for each disk(s)" 
                foreach ($disk in $Disks) { 
                    # Prepare the Information output 
                    Write-Verbose -Message "ComputerName: $Computer - $($Disk.deviceid)" 
                    $output =         @{'ComputerName'=$computer; 
                                    'FreeSpace(GB)'=("{0:N2}" -f($disk.freespace/1GB)); 
                                    'Size(GB)'=("{0:N2}" -f($disk.size/1GB)); 
                                    'PercentFree'=("{0:P2}" -f(($disk.Freespace/1GB) / ($disk.Size/1GB)))} 
                    # Create a new PowerShell object for the output 
                    $object = New-Object -TypeName PSObject -Property $output 
                    # Output the disk information 
                    Write-Output -InputObject $object 
                }#foreach ($disk in $disks) 
            }#if ($TryIsOK) 
        }#Foreach ($Computer in $ComputerName) 
    END {# Cleanup 
}#Function Get-DiskSizeInfo