Solved

Basic Script to extract System Drive Available disk space on REMOTE SERVERS

Posted on 2013-02-04
8
472 Views
Last Modified: 2013-02-05
Hi Guys,

I need a script which could do the following:

- Take Server Names from a Text File (Approx 2000+ Servers),
- Check the Total Space and Available Space only on C Drive
- Extract the results in CSV in a nice format.

Thank you.
0
Comment
Question by:japji
  • 4
  • 3
8 Comments
 
LVL 53

Expert Comment

by:Bill Prew
ID: 38853322
You can do this with a single line right at a command prompt (or in a bat file).  Give this a try and see if it does what you are looking for:

wmic /node:@"computers.txt" LogicalDisk Where "DeviceID='C:'" Get DeviceID,FreeSpace,Size /Format:csv>"report.csv"

Open in new window

~bp
0
 

Author Comment

by:japji
ID: 38853524
Hi BP,

The command works like a charm... however I would request you to just modify it slightly as below:

1. Prepare a Batch File
2. The Free Space and Size should spit the results either in MB or GB.

Thanks mate.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38853578
You should be able to use my script here:
http://www.experts-exchange.com/A_4379.html

to do the job.

Regards,

Rob.
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

Author Comment

by:japji
ID: 38853723
Hi Rob,

The script actually works perfectly fine however and it lists the Available Space on C Drive.

However my requirement is to extract the Total Space and Available Space.

Any chance you could modify the script please.

thanks mate.
0
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 38853815
Sure, I haven't tested it, but that should be easy enough.

Regards,

Rob,

strInputFile = "computers.txt"
strOutputFile = "CDriveSpace.csv"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const WMITimeOutInSeconds = 10
Const wbemFlagReturnImmediately = &h10
Const wbemFlagForwardOnly = &h20

Set objInputFile = objFSO.OpenTextFile(strInputFile, intForReading, False)
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
objOutputFile.WriteLine """Computer"",""C Drive Total Space (MB)"",""C Drive Free Space (MB)"""

While Not objInputFile.AtEndOfStream
   strComputer = objInputFile.ReadLine
   If Ping(strComputer) = True Then
      strReturn = TestWMIConnection(strComputer, WMITimeOutInSeconds)

      If strReturn = "success" Then

         Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
         ' The wbemFlagReturnImmediately flag is the default ExecQuery behavior and is semi-synchronous. The important optimization is the addition of the wbemFlagForwardOnly flag.
         ' Combining wbemFlagReturnImmediately with wbemFlagForwardOnly results in a forward-only enumerator. A forward-only enumerator performs much faster than the default enumerator,
         ' because WMI doesn't maintain references to objects in the SWbemObjectSet.
         ' Source: http://msdn.microsoft.com/en-us/library/ms974547.aspx
         Set colItems = objWMIService.ExecQuery("SELECT Size,FreeSpace FROM Win32_LogicalDisk WHERE DeviceID = 'C:'", "WQL", wbemFlagReturnImmediately + wbemFlagForwardOnly)
         For Each objItem In colItems
            dblFreeSpace = Round(objItem.FreeSpace / 1024 / 1024, 3)
            dblTotalSpace = Round(objItem.Size / 1024 / 1024, 3)
         Next
         objOutputFile.WriteLine """" & strComputer & """,""" & dblTotalSpace & """,""" & dblFreeSpace & """"

      ElseIf strReturn = "failed" Then
         objOutputFile.WriteLine """" & strComputer & """,""WMI ERROR"""

      Else
         objOutputFile.WriteLine """" & strComputer & """,""WMI TIME OUT"""

      End If

   Else
      objOutputFile.WriteLine """" & strComputer & """,""OFFLINE"""

   End If
Wend

objInputFile.Close
objOutputFile.Close
WScript.Echo "Script complete. Please see " & strOutputFile

Function Ping(strComputer)
   Dim objShell, boolCode
   Set objShell = CreateObject("WScript.Shell")
   boolCode = objShell.Run("Ping -n 1 -w 300 " & strComputer, 0, True)
   If boolCode = 0 Then
      Ping = True
   Else
      Ping = False
   End If
End Function

Function TestWMIConnection(strComputer, intTimeOutInSeconds)
   ' Function written by Rob Sampson - 12 Jan 2011
   ' Experts-Exchange volunteer: http://www.experts-exchange.com/M_3820065.html
   ' Return strings from this function are in lower case, and consist of:
   ' "success": WMI Connection successful
   ' "failed": WMI Connection failed
   ' "time out": WMI Connection attempt timed out

   Set objFSO = CreateObject("Scripting.FileSystemObject")
   strTempScript = Replace(WScript.ScriptFullName, WScript.ScriptName, "") & "TempWMITestToBeDeleted.vbs"

   Set objTempFile = objFSO.CreateTextFile(strTempScript, True)
   objTempFile.WriteLine "On Error Resume Next"
   objTempFile.WriteLine "Set objWMIService = GetObject(""winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2"")"
   objTempFile.WriteLine "If Err.Number = 0 Then"
   objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""success"""
   objTempFile.WriteLine "Else"
   objTempFile.WriteLine vbTab & "WScript.StdOut.Write ""failed"""
   objTempFile.WriteLine "End If"
   objTempFile.Close

   Set objShell = CreateObject("WScript.Shell")
   Set objExec = objShell.Exec("wscript " & objFSO.GetFile(strTempScript).ShortPath)
   intSeconds = 0
   While objExec.Status = 0 And intSeconds <= intTimeOutInSeconds
      WScript.Sleep 1000
      intSeconds = intSeconds + 1
   Wend
   If objExec.Status = 1 Then
      strReturn = objExec.StdOut.ReadAll
   Else
      On Error Resume Next
      objExec.Terminate
      Err.Clear
      On Error GoTo 0
      strReturn = "time out"
   End If
   objFSO.DeleteFile strTempScript, True

   TestWMIConnection = LCase(strReturn)
End Function

Open in new window

0
 

Author Comment

by:japji
ID: 38857920
Fantastic Solution Rob.

Cheers mate.
0
 

Author Closing Comment

by:japji
ID: 38857921
Fantastic Solution Rob.

Cheers mate.
0
 
LVL 65

Expert Comment

by:RobSampson
ID: 38857979
Great!  Thanks for the grade.

Rob.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Launch Process with NOT as Administrator 15 90
Bash Script to Analyze Oracle Schemas 11 102
how to pick specific file from ftp 13 56
VBScript Issues 8 30
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
Over the years I have built up my own little library of code snippets that I refer to when programming or writing a script.  Many of these have come from the web or adaptations from snippets I find on the Web.  Periodically I add to them when I come…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question