• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 306
  • Last Modified:

Logon script to record the disk space to a results file.

Hi,

I want a login script that can record all the drives space to a file with the machine name.
Is there any way to get this...

Regards
Sharath
0
bsharath
Asked:
bsharath
  • 13
  • 9
1 Solution
 
davecorunCommented:
I would take a look at the TechNet ScriptCenter:
http://www.microsoft.com/technet/scriptcenter/default.mspx

You can use their "Find a Script" resource, or download the Script Repository.  You're basically looking for a script that uses WMI (Windows Management Instrumentation) to grab the current disk space.  Very easy if you find one of their templates.

// Dave
0
 
RobSampsonCommented:
Try this...it currently writes to the same folder as the script, which could work for you, but if you want to make it a different, that can be done...

'==============
Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails
Dim strLogFileName, objFSO, objLogFile, boolDataExists, strLastLine, intCount, intDriveCount
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8

If WScript.Arguments.Count < 1 Then
      strComputer = InputBox("Please enter the computer that you wish to get the free space from:", "Free Space", "NTFP")
Else
      strComputer = WScript.Arguments(0)
End If

strLogFileName = strComputer & "_HardDiskSpaceLog.csv"

strDetails = strDetails & """" & Now & """," & strComputer

Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colDisks = objWMIService.ExecQuery _
    ("Select * from Win32_LogicalDisk Where DriveType = 3")

intDriveCount = 0
For Each objDisk in ColDisks
      intDriveCount = intDriveCount + 1
Next

If objFSO.FileExists(strLogFileName) = False Then
      strDetails = "Date,Computer"
      boolDataExists = False
      For intCount = 1 To intDriveCount
            strDetails = strDetails & "," & Chr(intcount + 66) & " Total (GB)," & Chr(intcount + 66) & " Free," & Chr(intcount + 66) & " % Free"
            '",D Total (GB),D Free,D % Free,E Total (GB),E Free,E % Free"
      Next
      For intCount = 1 To intDriveCount
            strDetails = strDetails & "," & Chr(intcount + 66) & " Diff"
            '",D Diff,E Diff"
      Next
      strDetails = strDetails & VbCrLf
Else
      strDetails = VbCrLf
      boolDataExists = True
End If

strDetails = strDetails & """" & Now & """," & strComputer

For Each objDisk in colDisks
    intFreeSpace = objDisk.FreeSpace
    intTotalSpace = objDisk.Size
    pctFreeSpace = intFreeSpace / intTotalSpace
    strDetails = strDetails & "," & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & "," & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & "," & FormatPercent(pctFreeSpace)
Next

Set objDisk = Nothing
Set colDisks = Nothing
Set objWMIService = Nothing

If boolDataExists = True Then
      Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForReading, False)
      While Not objLogFile.AtEndOfStream
            strLastLine = objLogFile.ReadLine
      Wend
      objLogFile.Close
      Set objLogFile = Nothing
      For intCount = 1 To intDriveCount
            If intCount = 1 Then
                  strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3) - Split(strDetails, ",")(3), 2)
            Else
                  strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3 + (intCount - 1) * 3) - Split(strDetails, ",")(3 + (intCount - 1) * 3), 2)
            End If
      Next
End If

Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForAppending, True)
objLogFile.Write strDetails
objLogFile.Close
Set objLogFile = Nothing
Set objFSO = Nothing

'MsgBox "Done"
'==============

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob this works fine.Can you change it not asking the machine name .So that i can use it for taking the machine names from a file or using it as a login script.
Its creating a file for each machine .Can i get all data in a single file.
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
RobSampsonCommented:
Sure, to use it as a login script, run the code below as is.

To use it on a file of computer names, drag a text file onto the script:

'================
'==============
Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails
Dim strLogFileName, objFSO, objInputFile, objLogFile, boolDataExists, strLastLine, intCount, intDriveCount, strFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8

strFile = ""

If WScript.Arguments.Count < 1 Then
      strComputer = "."
Else
      strFile = WScript.Arguments(0)
End If

If strFile = "" Then
      Get_Free_Space_Details(strComputer)
Else
      Set objInputFile = objFSO.OpenTextFile(strFile, intForReading)
      While Not objInputFile.AtEndOfStream
            strComputer = objInputFile.ReadLine
            Get_Free_Space_Details(strComputer)
      Wend
End If

Set objFSO = Nothing

'MsgBox "Done"
'==============

Sub Get_Free_Space_Details(strComputer)

      Dim objNetwork, strCompName, strLogFolder
      strLogFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
      If strComputer = "." Then
            Set objNetwork = CreateObject("WScript.Network")
            strCompName = objNetwork.ComputerName
            strLogFileName = strLogFolder & strCompName & "_HardDiskSpaceLog.csv"
            strDetails = strDetails & """" & Now & """," & strCompName
      Else
            strLogFileName = strLogFolder & strComputer & "_HardDiskSpaceLog.csv"
            strDetails = strDetails & """" & Now & """," & strComputer
      End If
      
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      Set colDisks = objWMIService.ExecQuery _
          ("Select * from Win32_LogicalDisk Where DriveType = 3")

      intDriveCount = 0
      For Each objDisk in ColDisks
            intDriveCount = intDriveCount + 1
      Next
      
      If objFSO.FileExists(strLogFileName) = False Then
            strDetails = "Date,Computer"
            boolDataExists = False
            For intCount = 1 To intDriveCount
                  strDetails = strDetails & "," & Chr(intcount + 66) & " Total (GB)," & Chr(intcount + 66) & " Free," & Chr(intcount + 66) & " % Free"
                  '",D Total (GB),D Free,D % Free,E Total (GB),E Free,E % Free"
            Next
            For intCount = 1 To intDriveCount
                  strDetails = strDetails & "," & Chr(intcount + 66) & " Diff"
                  '",D Diff,E Diff"
            Next
            strDetails = strDetails & VbCrLf
      Else
            strDetails = VbCrLf
            boolDataExists = True
      End If
      
      If strComputer = "." Then
            strDetails = strDetails & """" & Now & """," & strCompName
      Else
            strDetails = strDetails & """" & Now & """," & strCompName
      End If
      
      For Each objDisk in colDisks
          intFreeSpace = objDisk.FreeSpace
          intTotalSpace = objDisk.Size
          pctFreeSpace = intFreeSpace / intTotalSpace
          strDetails = strDetails & "," & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & "," & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & "," & FormatPercent(pctFreeSpace)
      Next
      
      Set objDisk = Nothing
      Set colDisks = Nothing
      Set objWMIService = Nothing
      
      If boolDataExists = True Then
            Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForReading, False)
            While Not objLogFile.AtEndOfStream
                  strLastLine = objLogFile.ReadLine
            Wend
            objLogFile.Close
            Set objLogFile = Nothing
            For intCount = 1 To intDriveCount
                  If intCount = 1 Then
                        strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3) - Split(strDetails, ",")(3), 2)
                  Else
                        strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3 + (intCount - 1) * 3) - Split(strDetails, ",")(3 + (intCount - 1) * 3), 2)
                  End If
            Next
      End If
      
      Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForAppending, True)
      objLogFile.Write strDetails
      objLogFile.Close
      Set objLogFile = Nothing
      
End Sub
'================

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob for every machine i get a new file created...
Can i get all machines in 1 file...
0
 
RobSampsonCommented:
Sharath, you cannot have a lot of machines write to the same file (well, you *can*, but it becomes very error prone).....

The best thing to do would be to have another script, that opens all of the CSV files from that folder, and adds each file as a new sheet to a new Excel file, if that's what you need.....

Regards,

Rob.
0
 
bsharathAuthor Commented:
Ok Rob...How can i write all the csv files to 1 file in different sheets...
0
 
bsharathAuthor Commented:
ROb can we get the results to 1 single txt file.So that later i can copy to excel and sort or delimit.
0
 
RobSampsonCommented:
Sharath, here is a slightly revised version that can accept a file of computer names dragged onto it, and it outputs to a CSV file for each computer.

'====================
Option Explicit

Dim strComputer, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace, pctFreeSpace, strDetails
Dim strLogFileName, objFSO, objInputFile, objLogFile, boolDataExists, strLastLine, intCount, intDriveCount, strFile
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const intForReading = 1
Const intForAppending = 8

strFile = ""

If WScript.Arguments.Count < 1 Then
      strComputer = "."
Else
      strFile = WScript.Arguments(0)
End If

If strFile = "" Then
      Get_Free_Space_Details(strComputer)
Else
      Set objInputFile = objFSO.OpenTextFile(strFile, intForReading)
      While Not objInputFile.AtEndOfStream
            strComputer = objInputFile.ReadLine
            If Ping(strComputer) = True Then Get_Free_Space_Details(strComputer)
      Wend
End If

Set objFSO = Nothing

'MsgBox "Done"
'==============

Sub Get_Free_Space_Details(strComputer)

      Dim objNetwork, strCompName, strLogFolder
      strLogFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "")
      If strComputer = "." Then
            Set objNetwork = CreateObject("WScript.Network")
            strCompName = objNetwork.ComputerName
            strLogFileName = strLogFolder & strCompName & "_HardDiskSpaceLog.csv"
            strDetails = strDetails & """" & Now & """,""" & strCompName & """"
      Else
            strLogFileName = strLogFolder & strComputer & "_HardDiskSpaceLog.csv"
            strDetails = strDetails & """" & Now & """,""" & strComputer & """"
      End If
     
      On Error Resume Next
      Set objWMIService = GetObject("winmgmts:" _
          & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
      If Err.Number = 0 Then
            Err.Clear
            On Error GoTo 0
            Set colDisks = objWMIService.ExecQuery _
                ("Select * from Win32_LogicalDisk Where DriveType = 3")
      
            intDriveCount = 0
            For Each objDisk in ColDisks
                  intDriveCount = intDriveCount + 1
            Next
            
            If objFSO.FileExists(strLogFileName) = False Then
                  strDetails = "Date,Computer"
                  boolDataExists = False
                  For intCount = 1 To intDriveCount
                        strDetails = strDetails & "," & Chr(intcount + 66) & " Total (GB)," & Chr(intcount + 66) & " Free," & Chr(intcount + 66) & " % Free"
                        '",D Total (GB),D Free,D % Free,E Total (GB),E Free,E % Free"
                  Next
                  For intCount = 1 To intDriveCount
                        strDetails = strDetails & "," & Chr(intcount + 66) & " Diff"
                        '",D Diff,E Diff"
                  Next
                  strDetails = strDetails & VbCrLf
            Else
                  strDetails = VbCrLf
                  boolDataExists = True
            End If
            
            If strComputer = "." Then
                  strDetails = strDetails & """" & Now & """," & strCompName
            Else
                  strDetails = strDetails & """" & Now & """," & strComputer
            End If
            
            For Each objDisk in colDisks
                intFreeSpace = objDisk.FreeSpace
                intTotalSpace = objDisk.Size
                pctFreeSpace = intFreeSpace / intTotalSpace
                strDetails = strDetails & "," & Round(intTotalSpace / (1024 * 1024 * 1024), 2) & "," & Round(intFreeSpace / (1024 * 1024 * 1024), 2) & "," & FormatPercent(pctFreeSpace)
            Next
            
            Set objDisk = Nothing
            Set colDisks = Nothing
            Set objWMIService = Nothing
            
            If boolDataExists = True Then
                  Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForReading, False)
                  While Not objLogFile.AtEndOfStream
                        strLastLine = objLogFile.ReadLine
                  Wend
                  objLogFile.Close
                  Set objLogFile = Nothing
                  For intCount = 1 To intDriveCount
                        If intCount = 1 Then
                              strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3) - Split(strDetails, ",")(3), 2)
                        Else
                              strDetails = strDetails & "," & FormatNumber(Split(strLastLine, ",")(3 + (intCount - 1) * 3) - Split(strDetails, ",")(3 + (intCount - 1) * 3), 2)
                        End If
                        Next
                  End If
            
            Set objLogFile = objFSO.OpenTextFile(strLogFileName, intForAppending, True)
            objLogFile.Write strDetails
            objLogFile.Close
            Set objLogFile = Nothing
      End If
End Sub

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
'====================




Then, you can use this script to combine all of the CSV files into one XLS file:

'======================
Set objExcel = CreateObject("Excel.Application")
intNewWBSheets = objExcel.Application.SheetsInNewWorkbook
objExcel.Application.SheetsInNewWorkbook = 1
Set NewWB = objExcel.Workbooks.Add
objExcel.Visible = True
objExcel.ScreenUpdating = False

strCSVFilesFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strCSVFilesFolder).Files
      If Right(LCase(objFile.Name), 4) = LCase(".csv") Then
            NewWB.Sheets.Add , NewWB.Sheets(NewWB.Sheets.Count)
            Set CSVFile = objExcel.Workbooks.Open(objFile.Path)
          CSVFile.Sheets(1).Cells.Copy NewWB.Sheets(NewWB.Sheets.Count).Range("A1")
            CSVFile.Close
            NewWB.Sheets(NewWB.Sheets.Count).Name = Left(objFile.Name, InStr(objFile.Name, "_") - 1)
      End If
Next

NewWB.Sheets(1).Delete

objExcel.Application.SheetsInNewWorkbook = intNewWBSheets

objExcel.ScreenUpdating = True

MsgBox "Done"
'======================

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob this works great...Any way to update in 1 single txt file.Just curious to know if there is a way...
0
 
RobSampsonCommented:
That would be OK to use one text file if you're sure that you would be the only person writing to it, but if it's used as a login script, then you should not have multiple computers write to the same file, because you will get inconsistent results, and most likely write errors.  Each computer can continue to append to the same text file, because that computer cannot write to the same file twice, at the same time.

Generally, it's best to keep each process writing to it's own exclusive file, so that you avoid such errors.

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob if each machines results write in a new txt file...
The other time you gave me a script to convert all txt files to a xls file.I can use that to get all the txt files results to 1 csv or xls file
0
 
RobSampsonCommented:
I'm a little confused.  I have provided two scripts, one which you use as normal to gather information, which writes information to a unique CSV file for that computer.  The other script is then run later, to combine all of the CSV files into ton XLS file.  This script (in theory) should be able to be used to combine any number of CSV files into one XLS file.

Regards,

Rob.
0
 
bsharathAuthor Commented:
Rob yes the csv to xls file works fine but gets all the csv files to each sheet.But searching for machines with space would be difficult.thas why i asked you if a txt file results is possible.So that you had given me a way long ago to combine txt files to xls.Which gets all data from the txt files to xls.
this will easy the filtering or sorting process.
0
 
bsharathAuthor Commented:
0
 
RobSampsonCommented:
Sharath, try this VBS file to combine all of the Hard Drive Space CSVs into one sheet....

'=============
Set objExcel = CreateObject("Excel.Application")
Const xlToLeft = -4159
Const xlUp = -4162
Const xlPasteValues = -4163
intNewWBSheets = objExcel.Application.SheetsInNewWorkbook
objExcel.Application.SheetsInNewWorkbook = 1
Set NewWB = objExcel.Workbooks.Add
objExcel.Visible = True
'objExcel.ScreenUpdating = False

strCSVFilesFolder = Replace(WScript.ScriptFullName, WScript.ScriptName, "")

Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objFile In objFSO.GetFolder(strCSVFilesFolder).Files
      If Right(LCase(objFile.Name), 4) = LCase(".csv") Then
            Set CSVFile = objExcel.Workbooks.Open(objFile.Path)
          intLastRow = CSVFile.Sheets(1).Cells(65536, "A").End(xlUp).Row
          intLastCol = CSVFile.Sheets(1).Cells(1, 256).End(xlToLeft).Column
            If Trim(NewWB.Sheets(NewWB.Sheets.Count).Range("A1").Value) = "" Then
                  CSVFile.Sheets(1).Range("A1", CSVFile.Sheets(1).Cells(1, intLastCol)).Copy NewWB.Sheets(NewWB.Sheets.Count).Range("A1")
            End If
          CSVFile.Sheets(1).Range("A2", CSVFile.Sheets(1).Cells(intLastRow, intLastCol)).Copy NewWB.Sheets(NewWB.Sheets.Count).Range("A" & NewWB.Sheets(NewWB.Sheets.Count).Cells(65536, "A").End(xlUp).Row + 1)
            CSVFile.Close
            NewWB.Sheets(NewWB.Sheets.Count).Name = "Results"
      End If
Next

objExcel.Application.SheetsInNewWorkbook = intNewWBSheets

objExcel.ScreenUpdating = True

MsgBox "Done"
'=============

Regards,

Rob.
0
 
bsharathAuthor Commented:
Thanks a lot Rob for the excellent help you are a "Sage"  
0
 
RobSampsonCommented:
Thanks Sharath.....you give me a lot to think about!  ;-)

Rob.
0
 
bsharathAuthor Commented:
Rob can i use the same script as login script.Without any changes...
0
 
RobSampsonCommented:
I'm pretty sure you could.  If the script doesn't have any parameters, it will run against the local computer, so as long as the user had the rights to write to the same location as the script, then that should be fine.

BTW, I just clocked over 1,000,000 EE points the other day, so in seven months, that not bad!

Regards,

Rob.
0
 
bsharathAuthor Commented:
Ya Rob .... You deserve much more than this....Wishing you more to help members who need help from you.... Congrats Rob....
0
 
bsharathAuthor Commented:
Hi Rob
can you please help with this. Its a related code from this page
http://www.experts-exchange.com/Programming/Languages/Scripting/Q_27462949.html
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

  • 13
  • 9
Tackle projects and never again get stuck behind a technical roadblock.
Join Now