Dim WShell, FSO, Logger
CreateGlobalObjects()
Main()
''''''''''''''''''''''''''
''' Global Functions
''''''''''''''''''''''''''
Sub CreateGlobalObjects()
Set WShell = CreateObject("wscript.shell")
HandleError("Cannot create object 'wscript.shell' (CreateGlobalObjects).")
Set FSO = CreateObject("Scripting.FileSystemObject")
HandleError("Cannot create object 'Scripting.FileSystemObject'(CreateGlobalObjects).")
Set Logger = new ScriptLogger
HandleError("Cannot create object 'ScriptLogger'(CreateGlobalObjects).")
End Sub
Sub HandleError(customMessage)
Dim localLogger
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Wscript.Quit 0
End If
End Sub
Function HandleErrorContinue(customMessage)
Dim localLogger
HandleErrorContinue = False
If Not (Err.number = 0) Then
Set localLogger = new ScriptLogger
localLogger.LogFormattedError(customMessage)
Err.Clear
HandleErrorContinue = True
End If
End Function
Sub ScriptExit()
Wscript.Quit 0
End Sub
Public Function GetSQLServiceName(sInstance)
If sInstance = SQL_DEFAULT Then
GetSQLServiceName = SQL_DEFAULT
Else
GetSQLServiceName = "MSSQL$" & sInstance
End If
End Function
Function EscapeWQLString (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear
EscapeWQLString = Replace(strValue, "'", "\'")
End Function
'#Include File:ConnectionString.vbs
Function BuildConnectionString(strServer, strDatabase)
ON ERROR RESUME NEXT
Err.Clear
Dim dataSource
dataSource = BuildServerName(strServer, "")
BuildConnectionString = "Data Source=" & EscapeConnStringValue(dataSource) & ";Initial Catalog=" & EscapeConnStringValue(strDatabase) & ";Integrated Security=SSPI"
End Function
Function BuildConnectionStringWithPort(ByVal strServer, ByVal strDatabase, ByVal tcpPort)
ON ERROR RESUME NEXT
Err.Clear
Dim dataSource
dataSource = strServer
If ((tcpPort <> "0") And (tcpPort <> "")) Then
dataSource = dataSource & "," & tcpPort
End If
BuildConnectionStringWithPort = "Data Source=" & EscapeConnStringValue(dataSource) & ";Initial Catalog=" & EscapeConnStringValue(strDatabase) & ";Integrated Security=SSPI"
End Function
' This function should be used to escape Connection String keywords.
Function EscapeConnStringValue (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear
EscapeConnStringValue = """" + Replace(strValue, """", """""") + """"
End Function
Function EscapeWQLString (ByVal strValue)
ON ERROR RESUME NEXT
Err.Clear
EscapeWQLString = Replace(strValue, "'", "\'")
End Function
Function GetTcpPort (ByVal strServer)
ON ERROR RESUME NEXT
Err.Clear
Dim tcpPort
tcpPort = ""
Call BuildServerName(strServer, tcpPort)
GetTcpPort = tcpPort
End Function
Function BuildServerName(ByVal strServer, ByRef tcp)
ON ERROR RESUME NEXT
Err.Clear
Dim pathArray, instanceName, computerName, ip, serverName
Dim oWMI, oQuery
ip= ""
pathArray = Split(strServer, "\")
computerName = pathArray(0)
instanceName = "MSSQLSERVER"
if (pathArray.Count > 1) Then
instanceName = pathArray(1)
End If
serverName = strServer
Set oWMI = GetObject("winmgmts:\\" & computerName & "\root\Microsoft\SqlServer\" & SQL_WMI_NAMESPACE)
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND PropertyName = 'ListenOnAllIPs'")
If oQuery.Count >0 Then
Dim isListenAll
Set isListenAll = oQuery.ItemIndex(0)
If(isListenAll.PropertyNumVal = 1) Then
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = 'IPAll' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
tcp = oQuery.ItemIndex(0).PropertyStrVal
If ((tcp <> "0") And (tcp <> "")) Then
serverName = serverName & "," & tcp
Else tcp = ""
End If
End If
Else
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName <> '' AND PropertyName = 'Enabled' AND PropertyNumVal = 1")
If (oQuery.Count > 0) Then
Dim ipAddressName
ipAddressName = oQuery.ItemIndex(0).IPAddressName
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = '"& EscapeWQLString(ipAddressName) &"' AND (PropertyName = 'TcpPort' OR PropertyName = 'TcpDynamicPorts') AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
tcp = oQuery.ItemIndex(0).PropertyStrVal
End If
Set oQuery = oWMI.ExecQuery("SELECT * FROM ServerNetworkProtocolProperty WHERE ProtocolName = 'Tcp' AND InstanceName = '"& EscapeWQLString(instanceName) &"' AND IPAddressName = '"& EscapeWQLString(ipAddressName) &"' AND PropertyName = 'IpAddress' AND PropertyStrVal <> ''")
If (oQuery.Count > 0) Then
ip = oQuery.ItemIndex(0).PropertyStrVal
End If
If ip <> "" Then
serverName = ip
End If
If ((tcp <> "0") And (tcp <> "")) Then
serverName = servername & "," & tcp
Else tcp = ""
End If
End If
End If
End If
On Error Goto 0
BuildServerName = serverName
End Function'#Include File:Common-SecondPart.vbs
Public Sub Save()
m_lNumber = Err.number
m_sSource = Err.Source
m_sDescription = Err.Description
m_sHelpContext = Err.HelpContext
m_sHelpFile = Err.helpfile
End Sub
Public Sub Raise()
Err.Raise m_lNumber, m_sSource, m_sDescription, m_sHelpFile, m_sHelpContext
End Sub
Public Sub Clear()
m_lNumber = 0
m_sSource = ""
m_sDescription = ""
m_sHelpContext = ""
m_sHelpFile = ""
End Sub
Public Default Property Get Number()
Number = m_lNumber
End Property
Public Property Get Source()
Source = m_sSource
End Property
Public Property Get Description()
Description = m_sDescription
End Property
Public Property Get HelpContext()
HelpContext = m_sHelpContext
End Property
Public Property Get HelpFile()
HelpFile = m_sHelpFile
End Property
End Class
''''''''''''''''''''''''''''''''''''
''' ScriptLogger
''''''''''''''''''''''''''''''''''''
Class ScriptLogger
Dim sourceLogEvent
Private Sub Class_Initialize()
sourceLogEvent = "Management Group: " + ManagementGroupName + ". Script: " + WScript.ScriptName
End Sub
Private Sub Class_Terminate()
End Sub
Public Property Get ErrorEventType
ErrorEventType = 1
End Property
Public Property Get WarningEventType
WarningEventType = 2
End Property
Public Property Get InfoEventType
InfoEventType = 4
End Property
Private Function LogEvent (message, eventType)
On Error Resume Next
Dim oAPI
Set oAPI = CreateObject("MOM.ScriptAPI")
Call oAPI.LogScriptEvent(sourceLogEvent, SCRIPT_EVENT_ID, eventType, message)
End Function
Public Function LogDebug(message)
if DEBUG_MODE Then
WScript.StdOut.WriteLine message
LogEvent message, Me.InfoEventType
End If
End Function
Public Function LogError(message)
if DEBUG_MODE Then
WScript.StdOut.WriteLine message
End If
LogEvent message, Me.ErrorEventType
End Function
Public Function LogWarning(message)
if DEBUG_MODE Then
WScript.StdOut.WriteLine message
End If
LogEvent message, Me.WarningEventType
End Function
Public Function LogFormattedError(customMessage)
Dim msg
If Not (Err.number = 0) Then
msg = Replace(" Error Number: #P1# " & VbCrLf & " Description: #P2# ", "#P1#", CStr(Err.number) )
msg = Replace(msg, "#P2#", Err.Description )
msg = customMessage & VbCrLf & msg & VbCrLf
'msg = msg & " [" & Me.ScriptInfo() & "]"
Me.LogError(msg)
End If
End Function
Private Function ScriptInfo()
Dim commandLineInfo : commandLineInfo = WScript.ScriptFullName
Dim argument
For Each argument In WScript.Arguments
commandLineInfo = commandLineInfo & " """ & argument & """"
Next
ScriptInfo = commandLineInfo
End Function
End Class
''''''''''''''''''''''''''''''''''''
''' Class OpsMgrAPI
''''''''''''''''''''''''''''''''''''
Class OpsMgrAPI
Dim scriptAPI
Private Sub Class_Initialize()
On Error Resume Next
Set scriptAPI = CreateObject("MOM.ScriptAPI")
HandleError("Cannot create object 'MOM.ScriptAPI' (OpsMgrAPI.Class_Initialize).")
End Sub
End Class
'#Include File:SQLADODB.vbs
''''''''''''''''''''''''''''''''''''
''' Class ADODB
''''''''''''''''''''''''''''''''''''
Class ADODB
Dim ADOConnection
Private Sub Class_Initialize()
On Error Resume Next
Set ADOConnection = CreateObject("ADODB.Connection")
HandleError("Cannot create object 'ADODB.Connection' (ADODB.Class_Initialize).")
End Sub
Public Function Open(connectionString, provider, connectionTimeout)
On Error Resume Next
Open = false
if connectionString = "" Then
Err.Raise SCRIPT_EVENT_ID, "ADODB.Open()", "Argument 'connectionString' cannot be empty." , "", 0
End If
ADOConnection.ConnectionString = connectionString
if provider = "" Then
ADOConnection.Provider = "sqloledb"
Else
ADOConnection.Provider = provider
End If
if connectionTimeout <= 0 Then
ADOConnection.ConnectionTimeout = 30
Else
ADOConnection.ConnectionTimeout = connectionTimeout
End If
HandleError("Cannot initialize ADODB connection (ADODB.Open).")
ADOConnection.Open()
if Err.number = 0 Then
Open = True
End If
End Function
Public Function ExecuteQuery(query)
On Error Resume Next
Set ExecuteQuery = ADOConnection.Execute(query)
End Function
Public Function Close()
On Error Resume Next
if Not IsNull(ADOConnection) Then
ADOConnection.Close()
HandleError("Cannot close ADODB connection (ADODB.Close).")
End If
End Function
Public Function HandleOpenConnectionErrorContinue(database, serverName, sqlInstanceName)
HandleOpenConnectionErrorContinue = true
if Err.number <> 0 Then
HandleOpenConnectionErrorContinue = false
Dim oError : Set oError = new Error
oError.Save()
Dim instanceIsRunning : instanceIsRunning = IsServiceRunning(sqlInstanceName)
On Error Resume Next
oError.Raise()
if ((Err.number and 65535) = 16389 or (Err.number and 65535) = 3661) and instanceIsRunning Then
Logger.LogError("Cannot login to database [" & serverName & "][" & sqlInstanceName & ":" & database & "] ")
Err.Clear
ElseIf (instanceIsRunning) Then
Logger.LogFormattedError("Cannot open ADODB connection. (Connection string: '" & ADOConnection.ConnectionString & "'.)")
Err.Clear
Else
Err.Clear
End If
On Error Goto 0
End If
End Function
Public Function HandleExecutionQueryErrorContinue(query, serverName, sqlInstanceName)
HandleExecutionQueryErrorContinue = true
if Err.number <> 0 Then
HandleExecutionQueryErrorContinue = false
Dim oError : Set oError = new Error
oError.Save()
Dim instanceIsRunning : instanceIsRunning = IsServiceRunning(sqlInstanceName)
On Error Resume Next
error.Raise()
if ((Err.number and 65535) = 16389 or (Err.number and 65535) = 3661) and instanceIsRunning Then
Logger.LogError("Cannot login to database [" & serverName & "][" & sqlInstanceName & ":" & ADOConnection.DefaultDatabase & "] ")
Err.Clear
ElseIf (instanceIsRunning) Then
Logger.LogFormattedError("Cannot execute query: '" & query & "'.")
Err.Clear
Else
Err.Clear
End If
On Error Goto 0
End If
End Function
Public Function IsServiceRunning(sInstance)
Dim sServiceName : sServiceName = GetSQLServiceName(sInstance)
Dim oService, sObjectString
sObjectString = "winmgmts:\\.\root\cimv2"
On Error Resume Next
Err.Clear
'We want to do our own error handling here. No WMIGetObject().
Set oService = GetObject(sObjectString & ":Win32_Service.Name='" & EscapeWQLString(sServiceName) & "'")
If Err.Number <> 0 Then
IsServiceRunning = false
Else
If oService.State = "Running" Then
IsServiceRunning = true
Else
IsServiceRunning = false
End If
End If
Set oService = Nothing
On Error GoTo 0
End Function
End Class
'#Include File:GetDiskVolumeInformation.vbs
'''''''''''''''''''''''''''''''''''''
''' Assembling Volumes Collection '''
'''''''''''''''''''''''''''''''''''''
Const WMI_LINK = "winmgmts:{impersonationLevel=impersonate}!\\{0}\root\cimv2"
Const MOUNT_POINTS_QUERY = "select Directory, Volume from Win32_Mountpoint"
Const VOLUME_QUERY = "select DeviceID, Name, Caption, Label, FileSystem, Capacity, FreeSpace from Win32_Volume"
Const WMI_ERROR = "Can't get WMI object on "
Const MOUNT_POINT_ERROR = "Can't select Mount Point data from WMI object on "
Const DISK_VOLUME_ERROR = "Can't select Disk Volume data from WMI object on "
Const FIND_BY_DEVICEID_ERROR = "Couldn't find volume by DeviceID "
'Main class which is supposed to be used in external scripts
Class DiskVolumeInformation
Public MountPoints
Public Volumes
Public Default Function Initialize(serverName)
Set MountPoints = SortMountPoints(GetMountPoints(serverName))
Set Volumes = GetVolumes(serverName, MountPoints)
Set Initialize = Me
End Function
End Class
Class MountPoint
Public Name
Public Caption
Public DeviceID
End Class
Function GetMountPoints(serverName)
On Error Resume Next
Dim MountPoints : Set MountPoints = CreateObject("Scripting.Dictionary")
Dim objWMIService : Set objWMIService = GetObject(Replace(WMI_LINK, "{0}", serverName))
HandleError(WMI_ERROR & serverName)
Dim colItems : Set colItems = objWMIService.ExecQuery(MOUNT_POINTS_QUERY)
HandleError(MOUNT_POINT_ERROR & serverName)
Dim objItem
For Each objItem In colItems
Dim point : set point = new MountPoint
Dim dirStartIndex : dirStartIndex = InStr(objItem.Directory, """") + 1
Dim dirEndIndex : dirEndIndex = InStr(dirStartIndex, objItem.Directory, """")
Dim Directory
Directory = LCase(Replace(Mid(objItem.Directory, dirStartIndex, dirEndIndex - dirStartIndex), "\\", "\"))
point.Caption = Directory
point.Name = point.DeviceID & point.Caption
MountPoints.Add point.Name, point
Next
Set GetMountPoints = MountPoints
End Function
Class DiskVolume
Public FileSystem
Public Capacity
Public FreeSpace
Public Name
Public DeviceID
Public Label
Public Paths()
Private pSize
Public Function AddPath(path)
pSize = pSize + 1
ReDim Preserve Paths(pSize)
Paths(pSize) = path
End Function
Private Sub Class_Initialize
pSize = -1
End Sub
End Class
Function GetVolumes(serverName, aMountPoints)
Dim Volumes : Set Volumes = CreateObject("Scripting.Dictionary")
Dim objWMIService : Set objWMIService = GetObject(Replace(WMI_LINK, "{0}", serverName))
HandleError(WMI_ERROR & serverName)
Dim colItems : Set colItems = objWMIService.ExecQuery(VOLUME_QUERY)
HandleError(DISK_VOLUME_ERROR & serverName)
Dim objItem
For Each objItem In colItems
Dim vol : set vol = new DiskVolume
vol.Capacity = objItem.Capacity
vol.FileSystem = objItem.FileSystem
vol.FreeSpace = objItem.FreeSpace
vol.Name = objItem.Name
vol.AddPath objItem.Caption
vol.DeviceID = objItem.DeviceID
vol.Label = objItem.Label
Volumes.Add vol.DeviceID, vol
Next
Function SearchVolumeCaption(aVolumes, aCaption)
Dim k
For Each k In aVolumes.Keys
Dim p
For p = 0 To UBound(aVolumes.Item(k).Paths)
If aVolumes.Item(k).Paths(p) = aCaption Then
SearchVolumeCaption = True
Exit Function
End If
Next
Next
SearchVolumeCaption = false
End Function
Function SearchVolumeDeviceID(aVolumes, aDeviceID)
Dim k
For Each k In aVolumes.Keys
If aVolumes.Item(k).DeviceID = aDeviceID Then
Set SearchVolumeDeviceID = aVolumes.Item(k)
Exit Function
End If
Next
Set SearchVolumeDeviceID = Nothing
End Function
Function MergeMountPointsIntoVolumeList(Volumes, MountPoints)
Dim key
For Each key in MountPoints.Keys
Dim Caption : Caption = MountPoints.Item(key).Caption
'Adding backslash in the end of the path is necessary for
'matching the path stored in volumes class, also it prevents
'invalid equality among folders with long composite names like
'"C:\Program Files" and "C:\Program"
If Right(Caption, 1) <> "\" Then
Caption = Caption & "\"
End If
'Search for Mount Points which are not listed among Volumes
Dim found : found = SearchVolumeCaption(Volumes, Caption)
If Not found Then
'Search for the equivalent Volume
Dim equalVolume : Set equalVolume = SearchVolumeDeviceID(Volumes, MountPoints.Item(key).DeviceID)
HandleError(FIND_BY_DEVICEID_ERROR & MountPoints.Item(key).DeviceID)
If Not equalVolume Is Nothing And Not IsNull(equalVolume) And Not IsEmpty(equalVolume) Then
equalVolume.AddPath Caption
End If
End If
Next
End Function
''''''''''''''''''''''''''''''''''''''''''''''''''''''
''' Sorting Mount Points descending by path length '''
''''''''''''''''''''''''''''''''''''''''''''''''''''''
Function SortMountPoints(aMountPoints)
'Convert dictionary to array
Dim MPArray()
Dim i : i = 0
Dim key
For Each key in aMountPoints.Keys
ReDim Preserve MPArray(i)
Set MPArray(i) = aMountPoints.Item(key)
i = i + 1
Next
'Sorting array
Dim SortedArray : SortedArray = SortArray(MPArray)
'Converting back to dictionary
Set SortMountPoints = CreateObject("Scripting.Dictionary")
Dim a
For Each a in SortedArray
SortMountPoints.Add a.Caption, a
Next
End Function
Function SortArray(arr)
If UBound(arr) < 1 Then
SortArray = arr
Exit Function
End If
Dim pivotIndex : pivotIndex = UBound(arr) 'Select last element as pivot
Dim pivot : Set pivot = arr(pivotIndex)
ReDim Preserve arr(UBound(arr) - 1) 'Remove pivot element
Dim longerList()
Dim shorterList()
Dim longerListSize : longerListSize = 0
Dim shorterListSize : shorterListSize = 0
'Put the elements with longer path than pivot to longerList
'and the elements with shorter path to shorterList
Dim a
For Each a in arr
If Len(a.Caption) > Len(pivot.Caption) Then
ReDim Preserve longerList(longerListSize)
Set longerList(longerListSize) = a
longerListSize = longerListSize + 1
Else
ReDim Preserve shorterList(shorterListSize)
Set shorterList(shorterListSize) = a
shorterListSize = shorterListSize + 1
End If
Next
Dim sortedLess, sortedGreater
If longerListSize > 0 Then
sortedLess = SortArray(longerList)
Else
sortedLess = longerList
End If
If shorterListSize > 0 Then
sortedGreater = SortArray(shorterList)
Else
sortedGreater = shorterList
End If
SortArray = MergeArrays(sortedLess, longerListSize, pivot, sortedGreater, shorterListSize)
End Function
Function MergeArrays(lessArray, longerListSize, pivotNode, greaterArray, shorterListSize)
Dim arraySize : arraySize = longerListSize + shorterListSize
Dim ResultArray()
ReDim ResultArray(arraySize)
Dim i
For i = 0 To UBound(ResultArray)
If i < longerListSize Then
Set ResultArray(i) = lessArray(i)
ElseIf i > longerListSize Then
Set ResultArray(i) = greaterArray(i - longerListSize - 1)
Else
Set ResultArray(i) = pivotNode
End If
Next
MergeArrays = ResultArray
End Function
'#Include File:SQLFileFreeSpaceCalculator.vbs
''''''''''''''''''''''''''''''''''''
''' Class DBFileSpaceCalculator
''''''''''''''''''''''''''''''''''''
Class DBFileSpaceCalculator
Public FreeSpaceMB
Public FreeSpacePercent
Public FreeSpaceAutoGrowMB
Public FreeSpaceAutoGrowPercent
Public AvailableSpaceMB
Private DiskVolumesInfo
FreeSpaceAutoGrowMB = 0
FreeSpaceAutoGrowPercent = 0
End Sub
Public Default Function Init(diskVolumesInformation)
Set DiskVolumesInfo = diskVolumesInformation
Set Init = Me
End Function
Private Function GetVolumeByPath(filePath)
Dim mpKey
For Each mpKey In DiskVolumesInfo.MountPoints.Keys
If InStr(LCase(filePath), LCase(DiskVolumesInfo.MountPoints.Item(mpKey).Caption)) > 0 Then
Set GetVolumeByPath = DiskVolumesInfo.Volumes.Item(DiskVolumesInfo.MountPoints.Item(mpKey).DeviceID)
Exit Function
End If
Next
End Function
Private Function GetFreeSpaceDisk(filePath)
On Error Resume Next
Dim vol : Set vol = GetVolumeByPath(filePath)
If Not IsNull(vol) And Not vol is Nothing And Not IsEmpty(vol) Then
GetFreeSpaceDisk = vol.FreeSpace / 1024 / 1024
Exit Function
End If
Dim diskName : diskName = Left(filePath, 1)
Dim disk : Set disk = FSO.GetDrive(diskName)
HandleError("Cannot get object of disk '" & diskName & "'")
GetFreeSpaceDisk = disk.FreeSpace / 1024 / 1024
HandleError("Cannot get FreeSpace of disk '" & diskName & "'")
End Function
Public Function CalculateFreeSpace(dbFilesSet)
Dim GroupAvailableSpace : GroupAvailableSpace = 0
Dim GroupUsedSpace : GroupUsedSpace = 0
Dim GroupAvailableSpaceNoGrowth : GroupAvailableSpaceNoGrowth = 0
Dim GroupUsedSpaceNoGrowth : GroupUsedSpaceNoGrowth = 0
Dim filePath : filePath = dbFilesSet("physical_name").Value
Dim fileSize : fileSize = CDbl(dbFilesSet("fileSize").Value)
Dim fileUsedSpace : fileUsedSpace = CDbl(dbFilesSet("fileUsed").Value)
Dim IsAutoGrow : IsAutoGrow = CBool(dbFilesSet("IsAutoGrow").Value = 1)
if (IsAutoGrow) Then
Dim NumberGrowths : NumberGrowths = 0
Dim GrowthSpaceDisk : GrowthSpaceDisk = 0
Dim GrowthSpaceMaxSize : GrowthSpaceMaxSize = CDbl(1.79769313486232E307)
Dim fileMaxSize : fileMaxSize = CLng(dbFilesSet("fileMaxSize").Value)
Dim fileGrowth : fileGrowth = CLng(dbFilesSet("fileGrowth").Value)
Dim isPercentGrowth : isPercentGrowth = dbFilesSet("isPercentGrowth").Value
if Not isPercentGrowth Then 'FileGrowth not in Percent
fileGrowth = fileGrowth / 128
Else
fileGrowth = fileGrowth / 100
End If
if fileMaxSize <> -1 Then 'FileMaxSize Not Unlimited
'when the FileSize approaches the MaxSize, a large FileGrowth value can cause an error
'if the file tries to grow past the MaxSize value.
'calculate actual growth capacity
if isPercentGrowth Then 'FileGrowth in Percent
NumberGrowths = Fix(Log(fileMaxSize / fileSize) / Log(1 + fileGrowth))
GrowthSpaceMaxSize = (fileSize * ((1 + fileGrowth) ^ NumberGrowths)) - fileSize
Else
GrowthSpaceMaxSize = Fix((fileMaxSize - fileSize) / fileGrowth) * fileGrowth
End If
End If
Dim FreeSpaceDisk : FreeSpaceDisk = GetFreeSpaceDisk(filePath)
'when the FileSize approaches the limits of the logical disk, a large FileGrowth
'value can cause an error if the file tries to grow past the physical limit
'calculate actual growth capacity on disk
if isPercentGrowth Then 'FileGrowth in Percent
NumberGrowths = Fix(Log((FreeSpaceDisk + fileSize) / fileSize) / Log(1 + fileGrowth))
GrowthSpaceDisk = (fileSize * ((1 + fileGrowth) ^ NumberGrowths)) - fileSize
Else
GrowthSpaceDisk = Fix(FreeSpaceDisk / fileGrowth) * fileGrowth
End If
'with auto grow the growth allowed for a file with a max size is the minimum
'of either up to the max size or up to the limits of the logical drive.
'With max size unlimited, the growth allowed is not limited except by the limits of the logical drive
Dim GrowthAllowed : GrowthAllowed = GrowthSpaceDisk
if GrowthSpaceMaxSize < GrowthSpaceDisk Then
GrowthAllowed = GrowthSpaceMaxSize
End If
End If
Me.FreeSpaceMB = GroupAvailableSpace - GroupUsedSpace
if Me.FreeSpaceMB < 0 Then
Me.FreeSpaceMB = 0
End If
Me.FreeSpacePercent = (GroupAvailableSpace - GroupUsedSpace) / GroupAvailableSpace * 100
if Me.FreeSpacePercent < 0 Then
Me.FreeSpacePercent = 0
End If
Me.FreeSpaceAutoGrowMB = GroupAvailableSpaceNoGrowth - GroupUsedSpaceNoGrowth
if Me.FreeSpaceAutoGrowMB < 0 Then
Me.FreeSpaceAutoGrowMB = 0
End If
Me.FreeSpaceAutoGrowPercent = (GroupAvailableSpaceNoGrowth - GroupUsedSpaceNoGrowth) / GroupAvailableSpaceNoGrowth * 100
if Me.FreeSpaceAutoGrowPercent < 0 Then
Me.FreeSpaceAutoGrowPercent = 0
End If
End Function
Function Main()
On Error Resume Next
Logger.LogDebug("Start...")
if WScript.Arguments.Count() <> 4 Then
WScript.Quit()
End If
Dim connectionStr : connectionStr = WScript.Arguments(0)
Dim serverName : serverName = WScript.Arguments(1)
Dim instanceName : instanceName = WScript.Arguments(2)
Dim TcpPort : TcpPort = WScript.Arguments(3)
Logger.LogDebug("connectionStr = " & connectionStr & "; ServerName = " & serverName & "; instanceName = " & instanceName)
Public Function GetSQLServiceName(sInstance)
If sInstance = "MSSQLSERVER" Then
GetSQLServiceName = "MSSQLSERVER"
Else
GetSQLServiceName = "MSSQL$" & sInstance
End If
End Function
'The function returns service or "Unknown" state
'Input:
' server - compute name
' service - system service name
'Output:
' service state or "Unknown" state
Function GetServiceState( sTargetComputer, sServiceName)
On Error Resume Next
Dim sNamespace, sQuery, oWMI, objClasses, sState
sNamespace = "winmgmts://" & sTargetComputer & "/root/cimv2"
sQuery = "SELECT State FROM Win32_Service where Name = """ & EscapeWQLString(sServiceName) & """"
Set oWMI = GetObject(sNamespace)
Set objClasses = oWMI.ExecQuery(sQuery)
if objClasses.Count >= 1 Then
sState = objClasses.ItemIndex(0).Properties_.Item("State")
End If
If Err.number <> 0 Or objClasses.Count = 0 Then
sState = "Unknown"
End If
Err.Clear
GetServiceState = sState
End Function
''' Returns -1: If DB is not in AlwaysOn
''' Returns 0: If DB is in AlwaysOn and replica allow connections is NO
''' Returns 1: If DB is in AlwaysOn and replica allow connections is YES
Function AlwaysOnReplicaAllowConnections(aDbConnection, aDatabaseID)
AlwaysOnReplicaAllowConnections = -1
Dim query : query = " SELECT columns.id, " & _
" CASE WHEN OBJECT_ID('sys.availability_replicas') IS NOT NULL THEN 1 ELSE 0 END AS HasAlwaysOn " & _
" FROM sys.syscolumns columns where name = 'replica_id' and id = OBJECT_ID('sys.databases')"
Dim result : Set result = aDbConnection.ExecuteQuery(query)
If Not result.EOF Then
If result("HasAlwaysOn").Value = 1 Then
query = " SELECT d.name, d.database_id, " & _
" CASE WHEN d.replica_id IS NULL THEN 0 ELSE 1 END AS is_replica, " & _
" ar.secondary_role_allow_connections " & _
" FROM sys.databases d " & _
" JOIN sys.availability_replicas ar on d.replica_id = ar.replica_id " & _
" JOIN sys.servers s ON s.name = ar.replica_server_name AND s.server_id = 0 /*local server*/" & _
" WHERE d.database_id = " & aDatabaseID
Set result = aDbConnection.ExecuteQuery(query)
If Not result.EOF Then
If result("is_replica").Value = 1 Then
If (result("secondary_role_allow_connections").Value <= 1) Then
AlwaysOnReplicaAllowConnections = 0
Else
AlwaysOnReplicaAllowConnections = 1
End If
End If
End If
End If
End If
End Function
Function GetAllDbFilesFreeSpace(connectionStr, serverName, instanceName, sTcpPort)
On Error Resume Next
Dim opsMgrAPI
Dim tcp : tcp = sTcpPort
Dim dbMasterConnection
Dim listDatabases
Dim query : query = "SELECT name, database_id FROM sys.databases " & _
" WHERE state = 0 AND collation_name is not null " & _
" AND source_database_id IS NULL AND is_read_only = 0"
Set opsMgrAPI = new OpsMgrAPI
Set dbMasterConnection = new ADODB
Dim serviceName , state
serviceName = GetSQLServiceName(instanceName)
state = GetServiceState(serverName, serviceName)
if (state <> "Running") And (state <> "Unknown") Then
Dim bg
Set bg = opsMgrAPI.scriptAPI.CreateTypedPropertyBag(2)
opsMgrAPI.scriptAPI.AddItem(bg)
opsMgrAPI.scriptAPI.ReturnItems()
Exit Function
End If
Dim strProv : strProv = BuildConnectionStringWithPort(connectionStr, "master", tcp)
Err.Clear
Dim res : res = dbMasterConnection.Open(strProv, "sqloledb", 3)
'get fresh tcp port and try to connect again
if 0 <> Err.number then
Err.Clear
tcp = GetTcpPort(connectionStr)
strProv = BuildConnectionStringWithPort(connectionStr, "master", tcp)
res = dbMasterConnection.Open(strProv, "sqloledb", 30)
if 0 <> Err.number then
dbMasterConnection.HandleOpenConnectionErrorContinue "master", serverName, instanceName
Set propertyBag = opsMgrAPI.scriptAPI.CreateTypedPropertyBag(2)
opsMgrAPI.scriptAPI.AddItem(propertyBag)
opsMgrAPI.scriptAPI.ReturnItems()
Exit Function
end if
end if
Set listDatabases = dbMasterConnection.ExecuteQuery(query)
if dbMasterConnection.HandleExecutionQueryErrorContinue(query, serverName, instanceName) Then
Dim diskVolumesInfo : Set diskVolumesInfo = (new DiskVolumeInformation)(serverName)
Do While Not listDatabases.EOF
Dim databaseName : databaseName = listDatabases("name").Value
Dim databaseID : databaseID = listDatabases("database_id").Value
Dim dbConnection : Set dbConnection = new ADODB
If dbConnection.Open(BuildConnectionStringWithPort(connectionStr, databaseName, tcp), "sqloledb", 30) Then
query = "SET NOCOUNT ON " & vbCrLf & _
"SELECT size / 128.0 as fileSize, " & vbCrLf & _
" file_id as fileId," & vbCrLf & _
" FILEPROPERTY(name, 'SpaceUsed') / 128.0 as fileUsed," & vbCrLf & _
" CASE WHEN max_size = -1 OR max_size = 268435456 THEN -1 ELSE max_size / 128 END as fileMaxSize," & vbCrLf & _
" CASE WHEN growth = 0 THEN 0 ELSE 1 END as IsAutoGrow, " & vbCrLf & _
" is_percent_growth as isPercentGrowth, " & vbCrLf & _
" growth as fileGrowth, " & vbCrLf & _
" physical_name " & vbCrLf & _
" FROM sys.master_files WITH (NOLOCK)" & vbCrLf & _
" WHERE type IN (0,1) AND is_read_only = 0" & vbCrLf & _
" AND database_id = " & databaseID
Dim listFiles : Set listFiles = dbConnection.ExecuteQuery(query)
if dbConnection.HandleExecutionQueryErrorContinue(query, serverName, instanceName) Then
Do While Not listFiles.EOF
Dim fileId : fileId = listFiles("fileId").Value
Dim spaceCalculator : Set spaceCalculator = (new DBFileSpaceCalculator)(diskVolumesInfo)
spaceCalculator.CalculateFreeSpace(listFiles)
Dim propertyBag
Set propertyBag = opsMgrAPI.scriptAPI.CreateTypedPropertyBag(2)
opsMgrAPI.scriptAPI.AddItem(propertyBag)
listFiles.MoveNext
Loop
End If
dbConnection.Close()
Else
Dim replicaAllowConnections : replicaAllowConnections = AlwaysOnReplicaAllowConnections(dbMasterConnection, databaseID)
If replicaAllowConnections <> 0 Then
dbConnection.HandleOpenConnectionErrorContinue databaseName, serverName, instanceName
End If
End If
listDatabases.MoveNext
Loop
End If
dbMasterConnection.Close()
opsMgrAPI.scriptAPI.ReturnItems()
End Function