getSQLinfo.vbs 获得SQL数据/日志空间使用情况的脚本

2019-01-16 05:29:42于丽


    'Clean Data
    UsedDataSpace = Left(UsedDataSpace,4)
    FreeDataSpace = Left(FreeDataSpace,4)
    TotalDataSpace = Left(TotalDataSpace,4)

    'Print Result on Screen
    Wscript.echo "Used Space(MB) = " & UsedDataSpace 
    Wscript.Echo "Free Space(MB) = " & FreeDataSpace
    Wscript.Echo "Total Space(MB) = " & TotalDataSpace

    'Write on File
    ofile.WriteLine "Used DATA Space(MB) = " & UsedDataSpace
    ofile.WriteLine "Free DATA Space(MB) = " & FreeDataSpace
    ofile.WriteLine "Total DATA Space(MB) = " & TotalDataSpace

      objRecordSet.MoveNext
   loop
   end if
objRecordSet.Close
objConnection.Close 
set objConnection = nothing
set objRecordSet = nothing
end sub

Sub CheckSQLLOG
Const adOpenDynamic = 1, adLockOptimistic = 3
Dim strQuery
Dim objConnection, objRecordSet 
Dim strQueryResult, strQueryResult2
Dim UsedLogSpace, TotalLogSpace, FreeLogSpace
Set objConnection = CreateObject("ADODB.Connection")
Set objRecordSet = CreateObject("ADODB.Recordset")

objConnection.Open _
   "Provider=SQLOLEDB.1;Server=192.168.8.10;User ID=sa;Password=lcx;Database=master;"

strQuery = "DBCC SQLPERF(LOGSPACE)"
   objRecordSet.Open strQuery, objConnection, adOpenDynamic, adLockOptimistic
   if objRecordSet.eof Then
       'nothing returned
    wscript.echo "ERROR!!!"
   Else 


   Do Until objRecordSet.eof 
    If objRecordSet.Fields("Database Name") = "master" Then

   
     strQueryResult = objRecordSet.Fields("Log Size (MB)")
     strQueryResult2 = objRecordSet.Fields("Log Space USed (%)")
     UsedLogSpace = (strQueryResult * strQueryResult2) / 100
     TotalLogSpace = strQueryResult
     FreeLogSpace = TotalLogSpace - UsedLogSpace