Test page
Option Explicit
On Error Resume Next
Dim objFSO, objLocator, strSourceDir, strInputFile, strOutputFile, strWarningFile, objOutputFile
Dim objWarningFile, objInputFile, strInputLine, arrInputLine, strDomain, strServerName, strWriteLine
Dim strUserName, strPassword, objWMIService, colDisks, objDisk, intFreeSpace, intTotalSpace
Dim pctFreeSpace, strWarning, strHeader, strMonth, strDay, strDate
Const ForRead = 1
Const wbemImpersonationLevelImpersonate = 3
Const wbemAuthenticationLevelPktPrivacy = 6
Set objFSO = WScript.CreateObject("Scripting.FileSystemObject")
Set objLocator = CreateObject("WbemScripting.SWbemLocator")
strSourceDir = objFSO.GetParentFolderName(WScript.ScriptFullName)
strInputFile = strSourceDir & "\Input_File\Input_File.txt"
strMonth = DatePart("M", Date)
strDay = DatePart("D", Date)
If strMonth < 10 Then strMonth = "0" & strMonth
If strDay < 10 Then strDay = "0" & strDay
strDate = DatePart("YYYY", Date) & strMonth & StrDay
strOutputFile = strSourceDir & "\Output_Files\Disk_Space_Report_" & strDate & ".csv"
strWarningFile = strSourceDir & "\Output_Files\Disk_Space_Warning_Report_" & strDate & ".csv"
Set objOutputFile = objFSO.CreateTextFile(strOutputFile, True)
Set objWarningFile = objFSO.CreateTextFile(strWarningFile, True)
strHeader = "Server,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),,Drive,Size (GB),Free (GB),Free (%),"
objOutputFile.WriteLine strHeader
objWarningFile.WriteLine strHeader
Set objInputFile = objFSO.OpenTextFile(strInputFile, ForRead)
Do Until objInputFile.AtEndOfStream
strInputLine = objInputFile.ReadLine
If Instr (strInputLine, "#***#") Then
Else
ReDim arrInputLine(0)
arrInputLine = Split(strInputLine, ",")
strDomain = UCase(arrInputLine(0))
strServerName = UCase(arrInputLine(1))
strWriteLine = strServerName
Select Case strDomain
Case "DOMAIN1"
strUserName = strDomain & "\User1"
strPassword = "Password1"
Case "DOMAIN2"
strUserName = strDomain & "\User2"
strPassword = "Password2"
End Select
Set objWMIService = objLocator.ConnectServer(strServerName, "root\cimv2", strUserName, strPassword)
objWMIService.Security_.ImpersonationLevel = wbemImpersonationLevelImpersonate
objWMIService.Security_.AuthenticationLevel = wbemAuthenticationLevelPktPrivacy
Set colDisks = objWMIService.ExecQuery("Select * from Win32_LogicalDisk Where DriveType = 3")
For Each objDisk in colDisks
intFreeSpace = Round(objDisk.FreeSpace / 1073741824, 2)
intTotalSpace = Round(objDisk.Size / 1073741824, 2)
pctFreeSpace = intFreeSpace / intTotalSpace
If pctFreeSpace <= 0.25 Then
strWarning = True
strWriteLine = strWriteLine & "," & objDisk.DeviceID & "," & intTotalSpace & " GB," & intFreeSpace & " GB," & FormatPercent(pctFreeSpace) & ",WARNING"
Else
strWriteLine = strWriteLine & "," & objDisk.DeviceID & "," & intTotalSpace & " GB," & intFreeSpace & " GB," & FormatPercent(pctFreeSpace) & ","
End If
Next
If strWarning = True Then
objWarningFile.WriteLine strWriteLine
Else
objOutputFile.WriteLine strWriteLine
End If
strWriteLine = ""
strWarning = ""
colDisks = ""
objWMIService = ""
End If
Loop
Incollato da <http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/Server/2003_Server/Q_23339163.html>
|