Function IsValidObject(ByVal oObject)
IsValidObject = False
If IsObject(oObject) Then
If Not oObject Is Nothing Then
IsValidObject = True
End If
End If
End Function
Function MomCreateObject(ByVal sProgramId)
Dim oError
Set oError = New Error
On Error Resume Next
Set MomCreateObject = CreateObject(sProgramId)
oError.Save
On Error GoTo 0
If oError.Number <> 0 Then ThrowScriptError "Unable to create automation object '" & sProgramId & "'", oError
End Function
'#Include File:Error.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
Function ThrowScriptErrorNoAbort(ByVal sMessage, ByVal oErr)
On Error Resume Next
Dim oAPITemp
Set oAPITemp = MOMCreateObject("MOM.ScriptAPI")
oAPITemp.LogScriptEvent WScript.ScriptName, 4001, 1, sMessage & ". " & oErr.Description
End Function
Function ThrowScriptError(Byval sMessage, ByVal oErr)
On Error Resume Next
ThrowScriptErrorNoAbort sMessage, oErr
Quit()
End Function
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
'#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:GetSQL2005LongRunningJobs.vbs
'Copyright (c) Microsoft Corporation. All rights reserved.
Dim SCRIPT_SQL
SCRIPT_SQL = "SET NOCOUNT ON" & VbCrLf &_
"DECLARE @job_activity TABLE (" & VbCrLf &_
"[session_id] [int] NOT NULL," & VbCrLf &_
"[job_id] [uniqueidentifier] NOT NULL," & VbCrLf &_
"[job_name] [sysname] COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL," & VbCrLf &_
"[run_requested_date] [datetime] NULL," & VbCrLf &_
"[run_requested_source] [sysname] COLLATE SQL_Latin1_General_CP1_CS_AS NULL," & VbCrLf &_
"[queued_date] [datetime] NULL," & VbCrLf &_
"[start_execution_date] [datetime] NULL," & VbCrLf &_
"[last_executed_step_id] [int] NULL," & VbCrLf &_
"[last_exectued_step_date] [datetime] NULL," & VbCrLf &_
"[stop_execution_date] [datetime] NULL," & VbCrLf &_
"[next_scheduled_run_date] [datetime] NULL," & VbCrLf &_
"[job_history_id] [int] NULL," & VbCrLf &_
"[message] [nvarchar](1024) COLLATE SQL_Latin1_General_CP1_CS_AS NULL," & VbCrLf &_
"[run_status] [int] NULL," & VbCrLf &_
"[operator_id_emailed] [int] NULL," & VbCrLf &_
"[operator_id_netsent] [int] NULL," & VbCrLf &_
"[operator_id_paged] [int] NULL," & VbCrLf &_
"[execution_time_minutes] [int] NULL )" & VbCrLf &_
"INSERT INTO @job_activity" & VbCrLf &_
"([session_id]" & VbCrLf &_
",[job_id]" & VbCrLf &_
",[job_name]" & VbCrLf &_
",[run_requested_date]" & VbCrLf &_
",[run_requested_source]" & VbCrLf &_
",[queued_date]" & VbCrLf &_
",[start_execution_date]" & VbCrLf &_
",[last_executed_step_id]" & VbCrLf &_
",[last_exectued_step_date]" & VbCrLf &_
",[stop_execution_date]" & VbCrLf &_
",[next_scheduled_run_date]" & VbCrLf &_
",[job_history_id]" & VbCrLf &_
",[message]" & VbCrLf &_
",[run_status]" & VbCrLf &_
",[operator_id_emailed]" & VbCrLf &_
",[operator_id_netsent]" & VbCrLf &_
",[operator_id_paged])" & VbCrLf &_
"EXECUTE [msdb].[dbo].[sp_help_jobactivity]" & VbCrLf &_
"SELECT" & VbCrLf &_
"[ja].[job_id]" & VbCrLf &_
",[ja].[job_name]" & VbCrLf &_
",[originating_server]" & VbCrLf &_
",DATEDIFF(minute, [start_execution_date], GETDATE()) AS [execution_time_minutes]" & VbCrLf &_
"FROM @job_activity [ja]" & VbCrLf &_
"JOIN [msdb].[dbo].[sysjobs_view] [sjv] ON [sjv].[job_id] = [ja].[job_id]" & VbCrLf &_
"WHERE [start_execution_date] IS NOT NULL AND [run_status] IS NULL"
'Start of Main
'-----------------------------------------------------------------------------------------------
call Main()
Sub Main()
Dim objParameters, sConnectionString, sTcpPort
Dim oAPI, oBag
Set objParameters = WScript.Arguments
If objParameters.Count <> 2 Then
Quit()
End If
Set oAPI = MOMCreateObject("MOM.ScriptAPI")
Set oBag = oAPI.CreatePropertyBag()
If CheckLongRunningJobs(oBag, sConnectionString, sTcpPort) = 0 Then
Call oAPI.Return(oBag)
Else
Quit()
End If
End Sub
'End of Main
'-----------------------------------------------------------------------------------------------
Function WMIExecQuery(ByVal sNamespace, ByVal sQuery)
'
' WMIExecQuery :: Executes the WMI query and returns the result set.
'
'
Dim oWMI, oQuery, nInstanceCount
Dim e
Set e = New Error
On Error Resume Next
Set oWMI = GetObject(sNamespace)
e.Save
On Error Goto 0
If IsEmpty(oWMI) Then
ThrowScriptError "Unable to open WMI Namespace '" & sNamespace & "'. Check to see if the WMI service is enabled and running, and ensure this WMI namespace exists.", e
End If
On Error Resume Next
Set oQuery = oWMI.ExecQuery(sQuery)
e.Save
On Error Goto 0
If IsEmpty(oQuery) Or e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' returned an invalid result set. Please check to see if this is a valid WMI Query.", e
End If
'Determine if we queried a valid WMI class - Count will return 0 or empty
On Error Resume Next
nInstanceCount = oQuery.Count
e.Save
On Error Goto 0
If e.Number <> 0 Then
ThrowScriptError "The Query '" & sQuery & "' did not return any valid instances. Please check to see if this is a valid WMI Query.", e
End If
Set WMIExecQuery = oQuery
End Function
Function CheckLongRunningJobs (ByRef oBag, ByVal sConnectionString, ByVal sTcpPort)
Dim cnADOConnection
Dim strProv
Dim strQuery
Dim rsSQLLongRunningJobs
Dim bFail
Dim nDuration
Dim maxDuration
Dim e
Set e = New Error
e.Clear
On Error Resume Next
cnADOConnection.Open strProv
e.Save
On Error Goto 0
If e.Number <> 0 Then
e.Clear
strProv = BuildConnectionString(sConnectionString, "msdb")
On Error Resume Next
cnADOConnection.Open strProv
e.Save
On Error Goto 0
If e.Number <> 0 Then
'Error event in here
CheckLongRunningJobs = SQL_MONITORING_CONNECT_FAILURE
Exit Function
End If
End If
strQuery = SCRIPT_SQL
Err.Clear
Set rsSQLLongRunningJobs = cnADOConnection.Execute(strQuery)
If 0 <> Err.number Then
CheckLongRunningJobs = SQL_MONITORING_QUERY_FAILURE
Exit Function
End If
If 0 = rsSQLLongRunningJobs.State Then
' No records returned, recordset is likely closed.
Exit Function
End If
maxDuration = 0
Do
bFail = True
Err.Clear
if rsSQLLongRunningJobs.EOF then
if 0 <> Err.number then Exit Do
bFail = False
Exit Do
End If
If 0 <> Err.number Then Exit Do