~siggi-bjarnason/siggivbscript/vbscript

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
Option Explicit
Dim FileObj, strLine, fso, f, fc, f1, strOut, strParts, FolderSpec, strOutFileName
Dim objFileOut, strFileNameParts, x, iLimit, strReport, cn, cmd

Const MailServerName = "smtphost.redmond.corp.microsoft.com" 
'Const MailServerName = "Tk2smtp2.phx.gbl" 
Const SMTPTimeout = 10
Const FromAddress = """Siggi Bjarnason"" <siggib@microsoft.com>"
Const ToAddress = """Internal Netpro"" <inetpro@microsoft.com>"
Const CCAddress = ""
Const Subject = "IPC Queue report" 
Const cdoSendUsingPort = 2
Const cdoNTLM = 2

Const strFileNameCriteria = "_sh_ipc_queue"
Const DBServerName = "satnetengfs01"
Const DBName = "Reports"

Set cn      = CreateObject("ADODB.Connection")
Set cmd     = CreateObject("ADODB.Command")

cn.Provider = "sqloledb"
cn.Properties("Data Source").Value = DBServerName
cn.Properties("Initial Catalog").Value = DBName
'cn.Properties("User ID").Value = UserName
'cn.Properties("Password").Value = Password
cn.Properties("Integrated Security").Value = "SSPI"
cn.Open
Cmd.ActiveConnection = cn

If WScript.Arguments.Count <> 2 Then 
  WScript.Echo "Usage: parser inpath limit"
  WScript.Quit
End If

FolderSpec = WScript.Arguments(0)
iLimit = CInt(WScript.Arguments(1))
strreport = Now & " Starting analyzing " & folderspec & " limiting to " & ilimit & vbcrlf
strreport = strreport & String(65,"-") & vbcrlf

'wscript.echo strreport
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.GetFolder(folderspec)
Set fc = f.Files
For Each f1 in fc
	If f1.name <> strOutFileName AND InStr(f1.name,strFileNameCriteria) > 0 Then
		strFileNameParts = split(f1.name,"_")
		Set FileObj = fso.opentextfile(folderspec & "\" & f1.name)
		While not fileobj.atendofstream
			strLine = Trim(FileObj.readline)
			If strline <> "" Then
				strparts = split(strline," ")
				If IsNumeric(strparts(2)) Then
					If CInt(strparts(2)) > ilimit Then 
						strOut = strFileNameParts(0) & "," & strparts(2) & ","
						For x=3 to UBound(strparts) 
							strout = strout & strparts(x) & " " 
						Next
					End If 
				End If  
			End If 
		Wend 
		If strOut <> "" Then 
			strparts = split(strout,",")
			Cmd.CommandText = "insert into Reports.dbo.IPCReport (DeviceName, IPCQueueCount, Description, dttimestamp) values ('" & strparts(0) & "'," & strparts(1) & ",'" & strparts(2) & "','" & Now & "')"
			Cmd.Execute			
			strreport = strreport & strout & vbcrlf
		End If 
		FileObj.close
		strOut = ""
	End If
Next
cn.close

Set cmd = nothing
Set cn = nothing
Set FileObj = nothing
Set fc = nothing
Set f = nothing
Set fso = nothing

wscript.echo strreport
wscript.echo Now & " Analysis complete"

'mysendmail subject,strreport
'wscript.echo "Mail sent"


Sub MySendMail(StrSubject,msg)
	Dim iMsg,iConf,Flds
	
	Set iMsg = CreateObject("CDO.Message") 
	Set iConf = CreateObject("CDO.Configuration") 
	Set Flds = iConf.Fields 
	
	With Flds 
	  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort 
	  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = MailServerName 
	  .Item("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = SMTPTimeout
	  .item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate")= cdoNTLM
	  .Update 
	End With 
	
	With iMsg 
	  Set .Configuration = iConf 
	      .To       = ToAddress
	      .CC	= CCAddress
	      .From     = FromAddress 
	      .Subject  = StrSubject 
	      .textbody = Msg
	      .Send 
	End With
End Sub