Imports System.Management
Public Class Form1
Dim strFreespace As String
Dim D_Freespace As Double
Dim strTotalspace As String
Dim D_Totalspace As Double
Private Sub Button1_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs _
) Handles Button1.Click
CalculateFreeUsed("wabdkp59")
End Sub
Private Sub CalculateFreeUsed(ByVal srvname As String)
Dim msg As String
Try
' Connection credentials to the remote computer -
' not needed if the logged in account has access
Dim oConn As New ConnectionOptions()
oConn.Username = "mum-users\dsakpal"
oConn.Password = "*****"
Dim strNameSpace As String = "\\"
If srvname <> "" Then
strNameSpace += srvname
Else
strNameSpace += "."
End If
strNameSpace += "\root\cimv2"
Dim oMs As New System.Management.ManagementScope(strNameSpace, oConn)
'get Fixed disk stats
Dim oQuery As New System.Management.ObjectQuery("select FreeSpace,Size,Name from Win32_LogicalDisk where DriveType=3")
'Execute the query
Dim oSearcher As New ManagementObjectSearcher(oMs, oQuery)
'Get the results
Dim oReturnCollection As ManagementObjectCollection = oSearcher.[Get]()
'loop through found drives and write out info
For Each oReturn As ManagementObject In oReturnCollection
' Free Space in bytes
D_Freespace = System.Convert.ToDouble(oReturn("FreeSpace"))
' Free Space in GB
strFreespace = (((D_Freespace / 1024) / 1024) / 1024).ToString("0.00")
' Size in bytes
D_Totalspace = System.Convert.ToDouble(oReturn("Size"))
' Size in GB
strTotalspace = (((D_Totalspace / 1024) / 1024) / 1024).ToString("0.00")
msg = "Drive: {0}" & ControlChars.NewLine
msg = msg & "Total space: {1} GB" & ControlChars.NewLine
msg = msg & "Free Space: {2} GB" & ControlChars.NewLine
msg = String.Format(msg, oReturn("Name").ToString(), strTotalspace, strFreespace)
MessageBox.Show(msg)
Next
Catch
msg = "Failed to obtain Server Information."
MessageBox.Show(msg, "Server Error", MessageBoxButtons.OK, MessageBoxIcon.[Error])
End Try
End Sub
End Class
2008-10-13
Get drive information of Remote Server using WMI
To use this code you need to add reference to System.Management namespace.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment