Home > Blockchain >  Get the hostname of the server in get-wmiobject -class "Win32_LogicalDisk"
Get the hostname of the server in get-wmiobject -class "Win32_LogicalDisk"

Time:12-05

I am using the following to get if the drive has less than specified space:

$pc = Get-Content "C:\Users\user\Desktop\computers.txt"
$disks = get-wmiobject -class "Win32_LogicalDisk" -namespace "root\CIMV2" -computername $pc 
$results = 
foreach ($disk in $disks)
{ 
    if ($disk.Size -gt 0)
    {
        $size = [math]::round($disk.Size/1GB, 0)
        $free = [math]::round($disk.FreeSpace/1GB, 0)
        [PSCustomObject]@{
            Drive = $disk.Name
            "Free" = "{1:P0}" -f $free, ($free/$size)
        }
    }
}
$results  | Where-Object{( $_."Drive" -ne "Z:") -and  ($_."Drive" -ne "Y:") } | Where-Object{ $_."Free" -le 80 }  | Out-GridView

How to make this print the server name next to the disks found? NOTE- I cannot use any other method except the above.

CodePudding user response:

You would just need to include the PSComputerName property to the output objects:

$results = foreach ($disk in $disks)
{ 
    if ($disk.Size -gt 0)
    {
        $size = [math]::round($disk.Size / 1GB, 0)
        $free = [math]::round($disk.FreeSpace / 1GB, 0)

        [PSCustomObject]@{
            ComputerName = $disk.PSComputerName
            Drive        = $disk.Name
            Free         = "{0:P0}" -f ($free / $size)
        }
    }
}
  • Related