Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to query disk space on remote server

Tags:

vb.net

wmi

I am trying to nail down free space on a remote server by querying all the drives and then looping until I find the drive I am seeking.
Is there a better way to do this?

Dim oConn As New ConnectionOptions
Dim sNameSpace As String = "\\mnb-content2\root\cimv2"
Dim oMS As New ManagementScope(sNameSpace, oConn)

Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")
Dim oSearcher As ManagementObjectSearcher = New ManagementObjectSearcher(oMS, oQuery)

Dim oReturnCollection As ManagementObjectCollection = oSearcher.Get()
Dim oReturn As ManagementObject

For Each oReturn In oReturnCollection
    'Disk name
    Console.WriteLine("Name : " + oReturn("Name").ToString())
    'Free Space in bytes
    Dim sFreespace As String = oReturn("FreeSpace").ToString()
    If Left(oReturn("Name").ToString(), 1) = "Y" Then
        Console.WriteLine(sFreespace)
    End If
Next
like image 221
Jake Hackl Avatar asked Sep 11 '08 14:09

Jake Hackl


1 Answers

Why not just make your WMI query only pull back where name='Y'?

So:

Dim oQuery As System.Management.ObjectQuery = New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3 AND name='Y'")
like image 58
EBGreen Avatar answered Sep 24 '22 12:09

EBGreen