Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 510
  • Last Modified:

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

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
japji
Asked:
japji
  • 4
  • 3
1 Solution
 
Bill PrewCommented:
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
 
japjiAuthor Commented:
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
 
RobSampsonCommented:
You should be able to use my script here:
http://www.experts-exchange.com/A_4379.html

to do the job.

Regards,

Rob.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
japjiAuthor Commented:
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
 
RobSampsonCommented:
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
 
japjiAuthor Commented:
Fantastic Solution Rob.

Cheers mate.
0
 
japjiAuthor Commented:
Fantastic Solution Rob.

Cheers mate.
0
 
RobSampsonCommented:
Great!  Thanks for the grade.

Rob.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now