Get-DiskReport.ps1


Description

Purpose

No synopsis provided.

Detailed Description

No detailed description provided.

Back to Top

Usage

No usage examples provided.

Back to Top

Notes

No additional notes.

Back to Top


Script

function Get-DiskReport {
    [CmdletBinding()]
    param(
        [Parameter(
            Mandatory = $false,
            Position = 0,
            ValueFromPipelineByPropertyName = $true,
            HelpMessage = "Specify one or more computer names. Defaults to the local computer."
        )]
        [string[]]
        $ComputerName = $env:COMPUTERNAME,

        [Parameter(
            Mandatory = $false,
            HelpMessage = "Specify credentials for remote connections if needed."
        )]
        [System.Management.Automation.Credential()]
        [PSCredential]
        $Credential
    )

    process {
        foreach ($computer in $ComputerName) {
            # Script block to execute on each remote computer
            $scriptBlock = {
                Get-PSDrive -PSProvider FileSystem |
                    ForEach-Object {
                        [PSCustomObject]@{
                            ComputerName = $env:COMPUTERNAME
                            Name         = $_.Name
                            UsedGB       = [math]::Round($_.Used / 1GB, 2)
                            FreeGB       = [math]::Round($_.Free / 1GB, 2)
                            TotalGB      = [math]::Round(($_.Used + $_.Free) / 1GB, 2)
                        }
                    }
            }

            try {
                # Invoke the script block on the remote computer (or locally if ComputerName=localhost)
                if ($Credential) {
                    Invoke-Command -ComputerName $computer -ScriptBlock $scriptBlock -Credential $Credential
                }
                else {
                    Invoke-Command -ComputerName $computer -ScriptBlock $scriptBlock
                }
            }
            catch {
                Write-Warning "Failed to retrieve disk info from '$computer': $_"
            }
        }
    }
}

Back to Top

Download

Please feel free to copy parts of the script or if you would like to download the entire script, simply click the download button. You can download the complete repository in a zip file by clicking the Download link in the menu bar on the left hand side of the page.


Report Issues

You can report an issue or contribute to this site on GitHub. Simply click the button below and add any relevant notes. I will attempt to respond to all issues as soon as possible.

Issue


Back to Top