This is just a simple and easy script, which is retrieving disk info from either local or remote PC.
I have defined a couple of parameters, some of them are mandatory, some of them not, with parameter options to validate set, which means that while you are writing the parameter, it will give you a pop-up to choose which options for that parameter you do have.
For $server parameter, it also accepts multiple computer names, separated by a comma (‘,’)

Param(
[parameter(
mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Server Name")]
[ValidateNotNullorEmpty()]
[string[]]$server,

[parameter(
mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Enter size in MB or GB")]
[ValidateNotNullorEmpty()]
[ValidateSet("GB","MB")]
[string]$sizeIn,

[parameter(
mandatory=$False,
ValueFromPipeline=$True,
HelpMessage="Retrieve all device types or just device type 3")]
[ValidateSet("YES","NO")]
[string]$all,

[parameter(
mandatory=$false,
ValueFromPipeline=$True,
HelpMessage="Retrieve volume info from remote server")]
[ValidateSet("YES","NO")]
[string]$remote
)

Since we have also an option to retrieve disk info from a remote computer, we are using New-PSSession cmdlet for a creation of a new session.
To retrieve disk info, we are using Get-CimInstance cmdlet, but not Get-WMIObject, because Get-WMIObject is not capable of doing that.
But before we have started establishing a new PSSession, we are making sure that WinRM is enabled, as this is a pre-requisite.
Good thing is that it is enough for only a destination computer to have WinRM enabled.

if($remote -eq ‘YES’)
{
Write-Output "We are trying to collect disk info from remote server"

    Try 
    {

        $result = Invoke-Command -ComputerName $server -ScriptBlock {Get-Service | Where-Object {($psitem.Name -eq "WinRM") -and ($psitem.Status -eq "Running")}} -ErrorAction Stop
        if($PSBoundParameters['Verbose']) {Write-Verbose "WinRM is Running. Please continue"}
        $winRM = $True
    }
    Catch
    {

        if($PSBoundParameters['Verbose']) {Write-Verbose "WinRM is not running"; Write-Error $PSItem.ToString()}
        $winRM = $False
        Write-Verbose $PSItem
        Write-Verbose "Something went wrong";
        return $False
    }

    if($winRm)
    {
        try
        {
            $session = New-PSSession -ComputerName $server
            $getDisksAll = Invoke-Command -Session $session -ScriptBlock {Get-CimInstance Win32_logicaldisk}
        }
        Catch
        {
            Write-Verbose $PSItem
            Write-Verbose "Something went wrong"
            return $False
        }

I am using switch condition, to pick up whatever you define for -sizeIn parameter:

switch ($sizeIn)

{
    "GB";
    {
        $getDiskInfo = $getDisksAll | Select-Object SystemName,DeviceID,VolumeSerialNumber,FileSystem, `
        @{name='Free Space';expression={$PSItem.freespace / 1GB -as [int]}}, `
        @{name='Disk Size';expression={$PSItem.size / 1GB -as [int]}}, `
        @{name=“Drive Type”;expression={$hash.item([int]$PSItem.DriveType)}}

    }

    "MB"
    {
        $getDiskInfo = $getDisksAll | Select-Object SystemName,DeviceID,VolumeSerialNumber,FileSystem, `
        @{name='Free Space';expression={$PSItem.freespace / 1GB -as [int]}}, `
        @{name='Disk Size';expression={$PSItem.size / 1GB -as [int]}}, `
        @{name=“Drive Type”;expression={$hash.item([int]$PSItem.DriveType)}}
    }
}

For $all parameter, in case that you have defined ‘YES’, it will give you all disk types, unless you defined this parameter as ‘NO’. In that case, it will show you only disk type 3, which is Fixed Local Disk

if($all -eq ‘YES’)
{
Write-Output “We are showing all disk types”
$getDiskInfo

} else {

    Write-Output “We are showing ONLY Fixed Logical Disks”

    $getDiskInfo | Where-Object {$PSItem.'Drive Type' -eq "Fixed Local Disk"}

}

To run the script, just go into the script’s directory and type this line:

.\Get-DiskInfo.ps1 -server localhost -sizeIn GB -all YES -remote NO

Here is the complete script:

<#
.Synopsis
Gets Volume info from either local or remote PC
.DESCRIPTION
Long description
.EXAMPLE
This script can check either local or remote PC for hard disk and volume info.
You may choose if you want to get results in MB(MegaBytes) or GB(gigaBytes)
Also, you may choose if you want all volume types, or just device type 3
.EXAMPLE
Gets you disk info for local server, size in GB and all disk types
.\Get-DiskInfo.ps1 -server Server01 -sizeIn GB -all YES -remote NO

Gets you disk info for a remote server, size in GB and all disk types
.\Get-DiskInfo.ps1 -server Server02 -sizeIn GB -all YES -remote YES

Gets you disk info for a remote server, size in MB and only disk type 3
.\Get-DiskInfo.ps1 -server Server02 -sizeIn MB -all NO -remote YES
.INPUTS
Inputs to this cmdlet (if any)
.OUTPUTS
Output from this cmdlet (if any)
.NOTES
General notes
.COMPONENT
The component this cmdlet belongs to
.ROLE
The role this cmdlet belongs to
.FUNCTIONALITY
The functionality that best describes this cmdlet
#>
Param(
[parameter(
mandatory=$True,
ValueFromPipeline=$True,
HelpMessage="Server Name")]
[ValidateNotNullorEmpty()]
[string[]]$server,

[parameter(
    mandatory=$True,
    ValueFromPipeline=$True,
    HelpMessage="Enter size in MB or GB")]
    [ValidateNotNullorEmpty()]
    [ValidateSet("GB","MB")]
[string]$sizeIn,

[parameter(
    mandatory=$False,
    ValueFromPipeline=$True,
    HelpMessage=“Retrieve all device types or just device type 3”)]
    [ValidateSet(“YES”,”NO”)]
[string]$all,

[parameter(
    mandatory=$false,
    ValueFromPipeline=$True,
    HelpMessage=“Retrieve volume info from remote server”)]
    [ValidateSet(“YES”,”NO”)]
[string]$remote
)

Begin
{
Write-Output “We are starting with the script”
}

Process
{
    if($remote -eq 'YES')
    {
        Write-Output “We are trying to collect disk info from remote server”
        Try 
        {
            $result = Invoke-Command -ComputerName $server -ScriptBlock {Get-Service | Where-Object {($psitem.Name -eq “WinRM”) -and ($psitem.Status -eq “Running”)}} -ErrorAction Stop
            if($PSBoundParameters['Verbose']) {Write-Verbose “WinRM is Running. Please continue”}
            $winRM = $True
        }
        Catch
        {
            if($PSBoundParameters['Verbose']) {Write-Verbose “WinRM is not running”; Write-Error $PSItem.ToString()}
            $winRM = $False
            Write-Verbose $PSItem
            Write-Verbose “Something went wrong”
            return $False
        }
        if($winRm)
        {
            try
            {
                $session = New-PSSession -ComputerName $server
                $getDisksAll = Invoke-Command -Session $session -ScriptBlock {Get-CimInstance Win32_logicaldisk}
            }
            Catch
            {
                Write-Verbose $PSItem
                Write-Verbose “Something went wrong”
                return $False
            }
        }
    } else {
        Write-Output “Getting local disk info”
        $getDisksAll = Get-CimInstance Win32_logicaldisk
    }
    $hash=@{
        2=“Removable Disk”
        3=“Fixed local Disk”
        4=“Network Disk”
        5=“Compact Disk”
    }
    switch ($sizeIn) 
    {
        “GB”
        {
            $getDiskInfo = $getDisksAll | Select-Object SystemName,DeviceID,VolumeSerialNumber,FileSystem, `
            @{name='Free Space';expression={$PSItem.freespace / 1GB -as [int]}}, `
            @{name='Disk Size';expression={$PSItem.size / 1GB -as [int]}}, `
            @{name=“Drive Type”;expression={$hash.item([int]$PSItem.DriveType)}}
        }
        “MB”
        {
            $getDiskInfo = $getDisksAll | Select-Object SystemName,DeviceID,VolumeSerialNumber,FileSystem, `
            @{name='Free Space';expression={$PSItem.freespace / 1GB -as [int]}}, `
            @{name='Disk Size';expression={$PSItem.size / 1GB -as [int]}}, `
            @{name=“Drive Type”;expression={$hash.item([int]$PSItem.DriveType)}}
        }
    }
    If($all -eq 'YES') 
    {
        Write-Output “We are showing all disk types”
        $getDiskInfo
    } else {
        Write-Output “We are showing ONLY Fixed Logical Disks”
        $getDiskInfo | Where-Object {$PSItem.'Drive Type' -eq "Fixed Local Disk"}
    }
}

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Post Navigation