0

I want script that check free space of C drive in multiple computers via set list of IP's.. from text. file

I want the script for windows 7 environment.. and the script should check the free space of C drive, and if is less than 10gb, then it will show me that ip...

I tried with fsutil but this work just in local machine, and I have a lot computers.

Hope some can help me with that.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Tomer Cohen
  • 15
  • 1
  • 8

1 Answers1

1

Create the following files:

computers.txt

computername1
10.40.1.60

You can specify computers name or their ips.

CheckDiskFree.vbs

'
' Check drive c free space
'
On Error Resume Next

Const MIN_FREE = 10 ' Gb
Const ForAppending = 8
Const HARD_DISK = 3
Const ForReading = 1
CONST ForWriting = 2 

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set SrvList = objFSO.OpenTextFile("computers.txt", ForReading)
Set oShell = CreateObject("WScript.Shell")
Set ReportFile = objFSO.OpenTextFile ("FreeSpaceReport.csv", ForAppending, True)

'
' Report headers
'

ReportFile.writeline "Computer" & vbTAB & "Drive C Free (Gb)" & vbTAB & "Status"

'
' Loop
'

Do Until SrvList.AtEndOfStream

  StrComputer = SrvList.Readline
  wscript.echo now & vbTAB & StrComputer

  If Not IsConnectible(strComputer, "", "") Then

    ReportFile.writeline(strComputer & vbTAB & " no available")

  Else

    Set objWMIService = GetObject("winmgmts:" _ 
        & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 

    Set colDisks = objWMIService.ExecQuery _ 
        ("Select * from Win32_LogicalDisk Where DriveType = " & HARD_DISK & " AND DeviceID = 'C:' ") 

    For Each objDisk in colDisks 
        FreeGB = objDisk.FreeSpace / (1024 * 1024 * 1024)
        strStatus = "ok"      
        If FreeGB < MIN_FREE Then strStatus = "Low disk"
        ReportFile.writeline(strComputer & vbTAB & Round(FreeGB,2) & vbTAB & strStatus)
    Next 

  End If    

Loop

'
Wscript.Quit 

Function WMIDateStringToDate(dtmBootup)

  WMIDateStringToDate = CDate(Mid(dtmBootup, 7, 2) & "/" & _
         Mid(dtmBootup, 5, 2) & "/" & Left(dtmBootup, 4) _
         & " " & Mid (dtmBootup, 9, 2) & ":" & _
         Mid(dtmBootup, 11, 2) & ":" & Mid(dtmBootup, _
         13, 2))

End Function


Function IsConnectible(sHost, iPings, iTO) 
' Returns True or False based on the output from ping.exe 
' 
' Author: Alex Angelopoulos/Torgeir Bakken 
' Works an "all" WSH versions 
' sHost is a hostname or IP 
' iPings is number of ping attempts 
' iTO is timeout in milliseconds 
' if values are set to "", then defaults below used 


  Const OpenAsASCII      =  0 
  Const FailIfNotExist  =  0 
  Const ForReading      =  1 
  Dim sTempFile, fFile

  If iPings = "" Then iPings = 2 
  If iTO = "" Then iTO = 750 


  sTempFile = objFSO.GetSpecialFolder(2).ShortPath & "\" & objFSO.GetTempName

  oShell.Run "%comspec% /c ping.exe -n " & iPings & " -w " & iTO & " " & sHost & ">" & sTempFile, 0 , True 
  Set fFile = objFSO.OpenTextFile(sTempFile, ForReading, FailIfNotExist, OpenAsASCII) 

  Select Case InStr(fFile.ReadAll, "TTL=")
    Case 0 IsConnectible = False 
    Case Else IsConnectible = True 
  End Select 

  fFile.Close
  objFSO.DeleteFile(sTempFile) 

End Function

To run script you must execute the following command: cscript CheckDiskFree.vbs The script will create FreeSpaceReport.csv with the results.

JoSerra
  • 361
  • 2
  • 9
  • hi.. thanks for your respond. but I got this error after your code... http://oi68.tinypic.com/svrigx.jpg – Tomer Cohen Sep 10 '17 at 04:55
  • Is there any line in computers.txt file ? What about FreeSpaceReport.csv content ? – JoSerra Sep 10 '17 at 07:04
  • Hi @Tomer Script has no syntax errors. Have you revised your script file content ? Perhaps your editor changed any character ? – JoSerra Sep 10 '17 at 12:53
  • I check again work great.. But if that possible to change the long of gb to this format eg: 25.8gb . instead 25.58965778gb and also make some space from the end ip until the start gb? thanks – Tomer Cohen Sep 11 '17 at 08:09
  • Hi @Tomer : I edited script for your requirement. Only this line was changed `ReportFile.writeline(strComputer & vbTAB & Round(FreeGB,2) & vbTAB & strStatus)` The report file FreeSpaceReport.csv is TABULAR delimeted file. You can use Excel / Access to import that file and adjust each column size to your need. – JoSerra Sep 11 '17 at 08:52