Solved

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

Posted on 2013-02-04
8
465 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 51

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
 

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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Over the years I've spent many an hour playing on hardened, DMZ'd servers, with only a sub-set of the usual GNU toy's to keep me company; frequently I've needed to save and send log or data extracts from these server back to my PC, or to others, and…
Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now