Hi,
I have two scripts. One gets me drive space for a list of servers, and the other will add a header to the top of the text file created by the first script. If I run them as individual scripts, it works fine. When I try to add the script that writes the header into the script that generates the disk report, the output file is blank. Here are the two scripts, then combined
Now the header script
Now combined. This produces a blank DriveReport.txt
I have two scripts. One gets me drive space for a list of servers, and the other will add a header to the top of the text file created by the first script. If I run them as individual scripts, it works fine. When I try to add the script that writes the header into the script that generates the disk report, the output file is blank. Here are the two scripts, then combined
Code:
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Dim oFSO, oFile, sText, objTextFile, strComputer
Dim pctFreeSpace,strFreeSpace,strusedSpace
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objTextFile = objFSO.CreateTextFile(strReport)
'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const strReport = "DriveReport.txt"
Const sFile = "Servers.txt"
'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do While Not oFile.AtEndOfStream
sText = oFile.ReadLine
If Trim(sText) <> "" Then
strComputer = sText
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = ""
' txt = vbcrlf & "==============================================================" & vbcrlf
' txt = txt & vbCrLf & "Server" & vbTab & vbtab & "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)"
' txt = txt & vbcrlf & "==============================================================" & vbcrlf
For Each objItem in colItems
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strusedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & sText & vbTab & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & vbtab & pctFreeSpace & vbCrLf
Next
objTextFile.Write(txt)
End If
Loop
objTextFile.Close
oFile.Close
Else
WScript.Echo "There was no servers.txt file found. " & vbCrLf & "Please create it with a list of the servers you want to monitor."
End If
'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================
Now the header script
Code:
Const ForReading = 1
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForReading)
strContents = objFile.ReadAll
objFile.Close
strFirstLine = "Server" & vbTab & vbtab & "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)"
strNewContents = strFirstLine & vbCrLf & strContents
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForWriting)
objFile.WriteLine strNewContents
objFile.Close
Now combined. This produces a blank DriveReport.txt
Code:
'==========================================================================
Option Explicit
On Error Resume Next
'==========================================================================
' VARIABLE DECLARATIONS
'==========================================================================
Dim objShell, objNetwork, objFSO
Dim objWMIService, objItem, colItems
Dim strDriveType, strDiskSize, txt
Dim oFSO, oFile, sText, objTextFile, strComputer
Dim pctFreeSpace,strFreeSpace,strusedSpace
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set objFSO = CreateObject("Scripting.FilesystemObject")
Set objTextFile = objFSO.CreateTextFile(strReport)
'==========================================================================
' STATIC VARIABLE ASSIGNMENTS
'==========================================================================
Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const strReport = "DriveReport.txt"
Const sFile = "Servers.txt"
'==========================================================================
' MAIN SCRIPT CODE
'==========================================================================
If oFSO.FileExists(sFile) Then
Set oFile = oFSO.OpenTextFile(sFile, 1)
Do While Not oFile.AtEndOfStream
sText = oFile.ReadLine
If Trim(sText) <> "" Then
strComputer = sText
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_LogicalDisk WHERE DriveType=3")
txt = ""
' txt = vbcrlf & "==============================================================" & vbcrlf
' txt = txt & vbCrLf & "Server" & vbTab & vbtab & "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)"
' txt = txt & vbcrlf & "==============================================================" & vbcrlf
For Each objItem in colItems
pctFreeSpace = INT((objItem.FreeSpace / objItem.Size) * 1000)/10
strDiskSize = Int(objItem.Size /1073741824) & "Gb"
strFreeSpace = Int(objItem.FreeSpace /1073741824) & "Gb"
strusedSpace = Int((objItem.Size-objItem.FreeSpace)/1073741824) & "Gb"
txt = txt & sText & vbTab & objItem.Name & vbtab & strDiskSize & vbtab & strUsedSpace & vbTab & strFreeSpace & vbtab & pctFreeSpace & vbCrLf
Next
objTextFile.Write(txt)
End If
Loop
objTextFile.Close
oFile.Close
Else
WScript.Echo "There was no servers.txt file found. " & vbCrLf & "Please create it with a list of the servers you want to monitor."
End If
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForReading)
strContents = objFile.ReadAll
objFile.Close
strFirstLine = "Server" & vbTab & vbtab & "Drive" & vbtab & "Size" & vbtab & "Used" & vbtab & "Free" & vbtab & "Free(%)"
strNewContents = strFirstLine & vbCrLf & strContents
Set objFile = objFSO.OpenTextFile("DriveReport.txt", ForWriting)
objFile.WriteLine strNewContents
objFile.Close
'==========================================================================
' SUBS AND FUNCTIONS
'==========================================================================