What do you all think about this method? No intermediate files needed!
'
' Capturues Free and Percent Free Space for all local drives.
'
' ********************************************************************************
Const ForAppending = 8
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
'''''''''''''''''''''' Total and Free Space Capture
Set Drives = FileSystemObject.Drives
For Each DiskDrive in Drives
If DiskDrive.IsReady And DiskDrive.DriveType = 2 Then
driveLetr = DiskDrive.DriveLetter
totalspace = DiskDrive.TotalSize
freespace = DiskDrive.FreeSpace
totalmb = Int((totalspace/1024)/1024)
freemb = Int((freespace/1024)/1024)
'D:\Program Files\Microsoft SQL Server\90\Tools\Binn
RunCmd """for /f ""tokens=* delims=s"" %f in ('dir /ad " & driveLetr & ":\') do @""sqlcmd.exe"" -S DBHostName -Q ""set nocount on; insert into dbatools.dbo.diskfreeimport values('%f');"""""
End IF
Next
'' Destroy all objects to free memory
Set strComputer = Nothing
Set objWMIService = Nothing
Set FileSystemObject = Nothing
Set Drives= Nothing
Function Run (ByVal cmd)
Dim sh: Set sh = CreateObject("WScript.Shell")
Dim wsx: Set wsx = Sh.Exec(cmd)
If wsx.ProcessID = 0 And wsx.Status = 1 Then
' (The Win98 version of VBScript does not detect WshShell.Exec errors)
Err.Raise vbObjectError,,"WshShell.Exec failed."
End If
Do
Dim Status: Status = wsx.Status
'WScript.StdOut.Write wsx.StdOut.ReadAll()
'WScript.StdErr.Write wsx.StdErr.ReadAll()
If Status <> 0 Then Exit Do
'WScript.Sleep 10
Loop
Run = wsx.ExitCode
Set wsx = Nothing
Set sh = Nothing
End Function
' Runs an internal command interpreter command.
Function RunCmd (ByVal cmd)
RunCmd = Run("%ComSpec% /s /c " & cmd)
End Function