server disk space
-
Tuesday, April 10, 2012 6:47 PM
Hi
Have got the following code working to get me disk space of whatever server name I enter in the box
Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim query As Management.ManagementObjectSearcher Dim queryCollection As Management.ManagementObjectCollection Dim management_object1 As Management.ManagementObject Dim msc As Management.ManagementScope Dim strComputer As String = tbServer.Text If (strComputer = System.Environment.MachineName) Then msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2") Else Dim co As New Management.ConnectionOptions co.Username = strUserName co.Password = strPwd msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2") End If Dim query_command As String = "SELECT * FROM Win32_LogicalDisk" Dim select_query As Management.SelectQuery = New Management.SelectQuery(query_command) Try query = New Management.ManagementObjectSearcher(msc, select_query) queryCollection = query.Get() Dim strDisk As String = "" For Each management_object1 In queryCollection strDisk = "Drive Letter: " & management_object1("DeviceID") & vbCrLf & "File System: " & management_object1("FileSystem") & vbCrLf & "Total Space: " & ((CType(management_object1("Size"), String) / 1024) / 1024) / 1024 & " GB" & vbCrLf & "Free Space: " & ((CType(management_object1("FreeSpace"), String) / 1024) / 1024) / 1024 & " GB" MessageBox.Show(strDisk) Next management_object1 Catch ex As Exception MessageBox.Show(ex.ToString()) End Try End Sub End ClassPlease can someone help me amend it to achieve the following:-
- so instead of me entering a server name to check it reads them from a servers.txt file and repeats to get disk space of all the servers in this text file
- instead of outputting to the screen, it appends it to a text file or .csv file with the date next to it
Many thanks
Darren Rose
All Replies
-
Wednesday, April 11, 2012 7:52 AMModerator
Hi Darren,
Welcome to the MSDN Forum.
You can read a text file and write the result like this way:
Dim strComputers As String() = File.ReadAllLines("Your file path") For each computername as string in strComputers 'Your code to get the free space File.AppendAllText("Your csv file path","your data string") NextI hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Edited by Mike FengMicrosoft Contingent Staff, Moderator Wednesday, April 11, 2012 7:57 AM
- Marked As Answer by wingers Friday, April 13, 2012 12:59 PM
-
Wednesday, April 11, 2012 12:51 PM
Excellent, thanks Mike
Modified it as follows and it works fine:-
Imports System.IO Public Class Form1 Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim query As Management.ManagementObjectSearcher Dim queryCollection As Management.ManagementObjectCollection Dim management_object1 As Management.ManagementObject Dim msc As Management.ManagementScope 'Dim strComputer As String = tbServer.Text Dim strComputers As String() = File.ReadAllLines("servers.txt") For Each strComputer As String In strComputers If (strComputer = System.Environment.MachineName) Then msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2") Else ' Dim co As New Management.ConnectionOptions ' co.Username = strUserName ' co.Password = strPwd ' original line was - msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2", co) - removed ,co as not needed for DMG app msc = New Management.ManagementScope("\\" & strComputer & "\root\cimv2") End If Dim query_command As String = "SELECT * FROM Win32_LogicalDisk" Dim select_query As Management.SelectQuery = New Management.SelectQuery(query_command) Try query = New Management.ManagementObjectSearcher(msc, select_query) queryCollection = query.Get() Dim strDisk As String = "" For Each management_object1 In queryCollection strDisk = "Server Name: " & strComputer & vbCrLf & "Drive Letter: " & management_object1("DeviceID") & vbCrLf & "File System: " & management_object1("FileSystem") & vbCrLf & "Total Space: " & ((CType(management_object1("Size"), String) / 1024) / 1024) / 1024 & " GB" & vbCrLf & "Free Space: " & ((CType(management_object1("FreeSpace"), String) / 1024) / 1024) / 1024 & " GB" MessageBox.Show(strDisk) File.AppendAllText("diskspace.csv", strDisk) Next management_object1 Catch ex As Exception MessageBox.Show(ex.ToString()) End Try Next End Sub End Class
Just one thing to sort - it is reporting disk space for drive A: and my cd drive which I don't want - how can I exclude "empty" or "non existent" drives please?Darren Rose
-
Wednesday, April 11, 2012 3:05 PM
and is there anyway to get it to only show 2 decimal places for the free space i.e. 126.95GB rather than 126.443434356664GB?
Thanks
Darren Rose
-
Thursday, April 12, 2012 4:01 AMModerator
Hi Wingers,
>>how can I exclude "empty" or "non existent" drives please?
Please check the drive type first: http://msdn.microsoft.com/EN-US/library/system.io.drivetype.aspx
>> is there anyway to get it to only show 2 decimal places for the free space
You can use the Format method: http://msdn.microsoft.com/en-us/library/system.string.format.aspx
For example:
Dim num As Double = 15846.12354 Console.WriteLine(num.ToString("0.00"))I hope this will be helpful.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
- Marked As Answer by wingers Friday, April 13, 2012 1:00 PM
-
Thursday, April 12, 2012 3:48 PM
Excellent thanks Mike
Couldn't work out how to get drive type working, but as all drives I want to check are NTFS I added this:-
If management_object1("FileSystem") = "NTFS" ThenAnd amended my output by adding
ToString("0.00")to end of my free disk and total space line to get the two decimal places only
Thanks again!
Darren Rose
-
Friday, April 13, 2012 2:24 AMModerator
Hi Wingers,
Please try this code to retrieve the remote drive space:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click Dim freeBytesForUser As Long Dim totalBytes As Long Dim freeBytes As Long If GetDiskFreeSpaceEx("\\msdn_mikefeng\D$", freeBytesForUser, totalBytes, freeBytes) Then Console.WriteLine(freeBytesForUser) Console.WriteLine(totalBytes) Console.WriteLine(freeBytes) End If End Sub <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _ Friend Shared Function GetDiskFreeSpaceEx(drive As String, ByRef freeBytesForUser As Long, ByRef totalBytes As Long, ByRef freeBytes As Long) As Boolean End FunctionPlease make sure your current loggon user has enough permission to access the remote server.
Best regards,
Mike Feng
MSDN Community Support | Feedback to us
Please remember to mark the replies as answers if they help and unmark them if they provide no help.

