Monday, August 31, 2009

How do I check the disk space of a remote computer within my domain?

Hey, Programming Guy! How do I check the disk space of a remote computer within my domain?
- Leonardo

 


Leonardo, you can use the WMIService's Win32_LogicalDisk. Say, if you want to check the size of the c:, you can use the following query:

Select * from Win32_LogicalDisk Where DeviceID = 'C:'


Thus, if you want to check the drive c's diskspace remotely, the following code should be convenient for you:

    Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objWbemLocator.ConnectServer(strComputer, "root\cimv2",strUser,strPass)
    Set colOSName = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")
    For Each objItem in colOSName
      WScript.Echo strComputer & ": " & objItem.Caption
    Next

Or, you can wrap everything together to become a command. Save the following code into a file with filename: disspace.wsf:
<?xml version="1.0"?>
<package>
<job>
<script language="vbscript">
<![CDATA[
  syntax = false
  If WScript.arguments.count = 3 Then
    strComputer = WScript.arguments(0)
    strUser = WScript.arguments(1)
    strPass = WScript.arguments(2)
    syntax = True
  ElseIf WScript.arguments.count = 1 Then
    strComputer = WScript.arguments(0)
    WScript.StdOut.write "User name:"
    strUser = WScript.StdIn.ReadLine
    WScript.StdOut.write "Password:"
    strPass = WScript.StdIn.ReadLine
    syntax = True
  ElseIf WScript.arguments.count = 0 Then
    WScript.StdOut.Write "Computer Name or IP:"
    strComputer = WScript.StdIn.ReadLine
    WScript.StdOut.write "User name:"
    strUser = WScript.StdIn.ReadLine
    WScript.StdOut.write "Password:"
    strPass = WScript.StdIn.ReadLine
    syntax = True
  End If
  If syntax Then
    Set objWbemLocator = CreateObject("WbemScripting.SWbemLocator")
    Set objWMIService = objWbemLocator.ConnectServer(strComputer, "root\cimv2",strUser,strPass)
    Set colOSName = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DeviceID = 'C:'")
    For Each objItem in colOSName
      WScript.Echo strComputer & ": " & objItem.Caption
    Next
  Else
    WScript.Echo "diskspace.wsf [ip] [user] [password]"
  End If
]]>
</script>
</job>
</package>

1 comment:

Anonymous said...

I think the same way you would check any computers size. Just use the remote access software to navigate to the C drive and check the properties!