Get-WmiObject Win32_LogicalDisk: The format of strings for computername etc


This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Richard Siddaway Richard Siddaway 1 year, 3 months ago.

  • Author
  • #27480
    Profile photo of Mel VW
    Mel VW

    What is the format suppose to be for the strings which represent computer names and the partition?

    The reason why I am asking is that my free space is always returning zero on the powershell session? This is not correct, because the real free space is 60 gigs.

    This is the code used

    $ip = ""
    $partition = "DeviceID ='E'"
    $description = "backup_server"

    $ConvertToGB = (1024 * 1024 * 1024)

    $disk = Get-WmiObject Win32_LogicalDisk -ComputerName $ip -Filter $partition | Select-Object Size,FreeSpace

    $ip + $partition + $description + ([math]::round(($disk.Size / $ConvertToGB),2)) + "GB" + "," + ([math]::round((($disk.FreeSpace / $ConvertToGB)),2)) + "GB"

    Can someone please advise if the something is needed to be added to the strings for the computer name and partition name? Or perhaps special characters or extra commas or quotes etc???

  • #27482
    Profile photo of Max Kozlov
    Max Kozlov

    I advice You do not write such a long code if you get any error ;^)
    If You launch just small sample – Get-WmiObject Win32_LogicalDisk -ComputerName $ip, you will see that DeviceID is "E:" but not "E", do not get $null in $disk variable and do not get FreeSize=0Gb

  • #27486
    Profile photo of Richard Siddaway
    Richard Siddaway

    AS Noted above the DeviceId MUST include the :

    DeviceId is the key property for the class. The correct usage is shown on the Path and __Relpath properties

  • #27487
    Profile photo of Richard Siddaway
    Richard Siddaway

    Looking at you code you can make some improvements to take advantage of PowerShell's builtin features

    $computername = $env:COMPUTERNAME
    $partition = 'E:'
    $description = 'backup_server'

    Get-WmiObject -Class Win32_LogicalDisk -ComputerName $computername |
    select PSComputerName,
    @{Name='Partition'; Expression={$_.DeviceId}},
    @{Name='Description'; Expression={$description}},
    @{Name='Size(GB)';Expression={[math]::Round(($_.Size / 1GB), 2)}},
    @{Name='FreeSpace(GB)';Expression={[math]::Round(($_.FreeSpace / 1GB), 2)}}

    Output an object rather than a string
    The computername you can get from the PSComputerName property that is added to all remote WMI results. You can perform the calculations directly in select-object. Use 1GB (powershell understand KB, MB, GB, TB, PB) as the divisor

You must be logged in to reply to this topic.