About Me

My photo
HANUMANGARH, RAJASTHAN, India

Saturday, 22 February 2025

Extract Disk Info from Remote host

 # Define the remote hostname or IP address

$remoteHost = Get-Content "C:\temp\RemoteServer.txt"


# Define the output CSV file path

$outputCsv = "C:\temp\RemoteDiskReport.csv"


# Initialize an array to store disk information

$diskReport = @()


# Use Invoke-Command to run the script block on the remote host

$remoteDiskInfo = Invoke-Command -ComputerName $remoteHost -ScriptBlock {

    # Get all physical disks

    $disks = Get-Disk


    # Initialize an array to store disk information

    $diskInfo = @()


    foreach ($disk in $disks) {

        # Get disk number and size

        $diskNumber = $disk.Number

        $diskSizeGB = [math]::Round($disk.Size / 1GB, 2)

        $diskStatus = $disk.HealthStatus

        $diskName = $disk.FriendlyName


        # Get all partitions on the disk

        $partitions = Get-Partition -DiskNumber $diskNumber


        # Initialize variables to track allocated and unallocated space

        $allocatedSpaceGB = 0

        $unallocatedSpaceGB = $diskSizeGB


        # Loop through partitions to calculate allocated space and get drive letters

        foreach ($partition in $partitions) {

            $driveLetter = $partition.DriveLetter

            $sizeGB = [math]::Round($partition.Size / 1GB, 2)

            $freeSpaceGB = [math]::Round((Get-Volume -Partition $partition).SizeRemaining / 1GB, 2)


            # Add partition details to the report

            $diskInfo += [PSCustomObject]@{

                Hostname           = $env:COMPUTERNAME

                DiskNumber        = $diskNumber

                DiskName          = $diskName

                DiskStatus        = $diskStatus

                DiskSizeGB        = $diskSizeGB

                DriveLetter       = if ($driveLetter) { $driveLetter } else { "N/A" }

                AllocatedSpaceGB  = $sizeGB

                FreeSpaceGB       = $freeSpaceGB

                UnallocatedSpaceGB = 0

            }


            # Update allocated and unallocated space

            $allocatedSpaceGB += $sizeGB

            $unallocatedSpaceGB -= $sizeGB

        }


        # Add unallocated space details to the report

        if ($unallocatedSpaceGB -gt 0) {

            $diskInfo += [PSCustomObject]@{

                Hostname           = $env:COMPUTERNAME

                DiskNumber        = $diskNumber

                DiskName          = $diskName

                DiskStatus        = $diskStatus

                DiskSizeGB        = $diskSizeGB

                DriveLetter       = "Unallocated"

                AllocatedSpaceGB  = 0

                FreeSpaceGB       = 0

                UnallocatedSpaceGB = $unallocatedSpaceGB

            }

        }

    }


    # Return the disk information

    return $diskInfo

}


# Add the remote disk information to the report

$diskReport += $remoteDiskInfo


# Export the report to a CSV file

$diskReport | Export-Csv -Path $outputCsv -NoTypeInformation


Write-Host "Remote disk report saved to $outputCsv"


$diskInfo | Export-Csv -Path $outputCsv -NoTypeInformation

No comments:

Post a Comment

Extract Disk Info from Remote host

 # Define the remote hostname or IP address $remoteHost = Get-Content "C:\temp\RemoteServer.txt" # Define the output CSV file path...