Advertisement

04.24.2008 at 03:28PM PDT, ID: 23352180
[x]
Attachment Details

WMI is slow connecting to multiple sites. Is there a routine that will allow connections to be quicker?

Asked by wally_davis in Microsoft Visual Basic.Net

Tags: vb.net, ie 7.0

I'm testing a routine that will allow me to connect to a plethora of workstations, to retrieve a value from the Registry, and then based on that value, determine if we need to run another process.

The problem is that it appears that it could take some time for the reads to complete. I noticed that one connection took almost 1 minute 30 seconds and eventually, my code timed out and it thinks that a key does not exist when, in fact it does. Unfortunately, I had to revert back to using Microsoft.VisualBasic to use VBScript syntax.

The code needs some cleaning up but you may be able to tell what I might be doing wrong.

Could someone help me determine which method would be better or if I could set a timeout so that the code doesn't think the pc isn't alive or the registry key does not exist?

I've attached the code below for your viewing.
I appreciate your time and expertise,
WStart Free Trial
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:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
Imports System.IO
Imports System.Data
Imports System.Data.SqlClient
Imports System.Management
Imports System.Net
Imports ADODB
Imports System.Diagnostics
Imports Microsoft.Win32
Imports Microsoft.VisualBasic
 
Module Module1
 
    Structure nic_info
        Public MACAddress As String
    End Structure
 
    Const PC_DateTimeStamp = "C:\Temp\Uptime.txt"
    Const PC_Status = "C:\Temp\InActive_PC's.txt"
    Const PC_And_Pvt_Status = "C:\Temp\PC_And_Pvt_Status.txt"
 
    Sub Main()
 
        ' open & write datetime & Mac Address to file
        Dim sw As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(PC_DateTimeStamp, False)
        Dim info As New nic_info
 
        ' retrieve DateTime and MAC Address and save to file
        Dim objWMI As New ManagementObjectSearcher("Select MACAddress from Win32_NetworkAdapterConfiguration where MACAddress LIKE '__:__:__:__:__:__' AND IPEnabled = 'TRUE'")
        Dim obj As New ManagementObject
        For Each obj In objWMI.Get
            If info.MACAddress = Nothing Then
                If Not IsDBNull(info.MACAddress) Then
                    If Not "AdapterType" = "Miniport" Then
                        If Not InStr(info.MACAddress, "Miniport") Then
                            info.MACAddress = obj("MACAddress").ToString.Trim()
                            sw.WriteLine("MAC = " & info.MACAddress)
                        End If
                    End If
                End If
            End If
        Next
        Dim dt As DateTime = DateTime.Now
        Dim trimDate As String = dt
        trimDate = trimDate.Remove(10, 10)
        trimDate = trimDate.TrimEnd
        sw.WriteLine("DATE = " & trimDate)
        sw.WriteLine("STATUS = Active")
        sw.WriteLine("FLAG = 0")
        sw.Close()
 
        ' Read uptime.txt file
        Dim sr As StreamReader = My.Computer.FileSystem.OpenTextFileReader(PC_DateTimeStamp)
        Dim lineValues(3) As String
        Dim strMac As String = Nothing
        Dim strDate As String = Nothing
        Dim strStatus As String = Nothing
        Dim strFlag As String = 0
 
        Dim i As Integer = 0
        For i = 0 To 3
            lineValues(i) = sr.ReadLine
        Next
        For i = 0 To lineValues.Length - 1
            If InStr(lineValues(i), "MAC") Then
                strMac = lineValues(i)
            End If
            If InStr(lineValues(i), "DATE") Then
                strDate = lineValues(i)
            End If
            If InStr(lineValues(i), "STATUS") Then
                strStatus = lineValues(i)
            End If
            If InStr(lineValues(i), "FLAG") Then
                strFlag = lineValues(i)
            End If
            'Call MsgBox(lineValues(i))
        Next
        strMac = strMac.Remove(0, 6)
        strDate = strDate.Remove(0, 7)
        strStatus = strStatus.Remove(0, 9)
        strFlag = strFlag.Remove(0, 7)
        sr.Close()
 
        Dim sqlCon As New SqlConnection
        Dim sqlCom As New SqlCommand
 
        sqlCon.ConnectionString = My.Settings.ConnectionString
 
        ' Insert DateTime stamp into Uptime table to signify PC's still alive
        sqlCom.Connection = sqlCon
        sqlCom.Connection.Open()
        sqlCom.CommandType = CommandType.StoredProcedure
        sqlCom.CommandText = "InsertUptime"
        sqlCom.Parameters.AddWithValue("@Mac", strMac)
        sqlCom.Parameters.AddWithValue("@LastResponse", strDate)
        sqlCom.Parameters.AddWithValue("@Status", strStatus)
        sqlCom.Parameters.AddWithValue("@Flag", strFlag)
        sqlCom.Connection.Close()
 
 
        'Retrieve Status value = "InActive" from DB and write results to file
        Dim sw2 As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(PC_Status, False)
        Dim strInActive As String = Nothing
        Dim sdr As SqlDataReader = Nothing
        sqlCom = sqlCon.CreateCommand
        sqlCom.CommandType = CommandType.StoredProcedure
        sqlCom.CommandText = "GetInActive"
        'Must create parameter and set its value before calling SQL Server
        sqlCom.Parameters.Add("@Status", SqlDbType.VarChar, 11)
        sqlCom.Parameters("@Status").Value = "InActive"
        sqlCom.Connection.Open()
        Try
            sdr = sqlCom.ExecuteReader
            Do While sdr.Read
                strInActive = sdr.Item("Workstation_Name")
                sw2.WriteLine(strInActive)
                sw2.Flush()
            Loop
            sdr.Close()
            sqlCom.Connection.Close()
            sw2.Close()
        Catch ex As Exception
            Debug.WriteLine(ex.Message)
        End Try
 
        Call PingHost()
    End Sub
 
    ' 1. Verify PVT.exe is installed, if not, exit & report pvt is missing.
    ' 2. Attempt to Ping each computer in list to determine if the computer is online or offline
    ' send status to log file and if successful, attempt to initiate WOL
 
    Public Sub PingHost()
        Dim srInActive As StreamReader = My.Computer.FileSystem.OpenTextFileReader(PC_Status)
        Dim swStatus As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(PC_And_Pvt_Status, False)
        Dim host As String
        Dim hostValue() As String
        Dim finished As Boolean = False
 
        While Not finished
            host = srInActive.ReadLine
            If host Is Nothing Then
                finished = True
            Else
                hostValue = (Split(host, ""))
                For Each host In hostValue
                    Try
                        Dim ping As New NetworkInformation.Ping()
                        Dim pingReply As NetworkInformation.PingReply = ping.Send(host)
                        If Not pingReply.Status = NetworkInformation.IPStatus.Success Then
                            swStatus.WriteLine("Pinging Workstation " + host + "... host is unreachable.")
                            swStatus.Flush()
                            ' Process.Start("C:\program files\Platform Validation Tool\Wake On LAN\Wake On LAN.exe", host)
                        Else
                            'Connect to each workstation to retrieve Pvt Install Status
                            Const HKEY_LOCAL_MACHINE = &H80000002
                            Dim strComputer = host
                            Dim strKeyPath, strVersion, strInstall, strValue As String
                            Dim objReg As Object
                            strVersion = Nothing
                            strInstall = Nothing
                            strValue = Nothing
                            objReg = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\default:StdRegProv")
                            strKeyPath = "Software\ACME\Packages\Platform Validation Tool"
                            objReg.EnumKey(HKEY_LOCAL_MACHINE, strKeyPath)
                            Call ReadRegValue("", strKeyPath, "Version", "REG_SZ", "HKLM", strVersion)
                            Call ReadRegValue(strComputer, strKeyPath & "\" & strVersion, "Install Status", "REG_SZ", "HKLM", strValue)
                            swStatus.WriteLine("PVT Install Status = " & strValue)
                            swStatus.Flush()
                        End If
                    Catch ex As NetworkInformation.PingException
                        swStatus.WriteLine(ex.Message)
                        swStatus.Flush()
                    End Try
                Next
            End If
        End While
        swStatus.Close()
    End Sub
 
    'Reads a registry value
    Function ReadRegValue(ByVal strComputer, ByVal strKeyPath, ByVal strValueName, ByVal strRegType, ByVal strRegHive, ByRef dataValue)
        Dim objReg, SWBemlocator, objWMIService, hkHive, rVal
        rVal = -1
        hkHive = Nothing
        Const HKEY_EMPTY = &H0
        Const HKEY_LOCAL_MACHINE = &H80000002
        SWBemlocator = CreateObject("WBemScripting.SWbemlocator")
        objWMIService = SWBemlocator.ConnectServer(strComputer, "root\default")
        objReg = objWMIService.Get("StdRegProv")
        Select Case strRegHive
            Case "HKLM"
                hkHive = HKEY_LOCAL_MACHINE
        End Select
        If hkHive <> HKEY_EMPTY Then
            Select Case strRegType
                Case "REG_SZ"
                    rVal = objReg.GetStringValue(hkHive, strKeyPath, strValueName, dataValue)
            End Select
        End If
        If Not rVal = 0 Then
            If rVal = -1 And strRegType = "REG_EXPAND_SZ" Then
                dataValue = "Null Install Status data value"
            ElseIf rVal = -1 Then
                dataValue = "Null Install Status data value"
            ElseIf rVal = 1 Then
                dataValue = "Null Install Status data value"
            ElseIf rVal = 2 Then
                dataValue = "KEY Does not exist"
            ElseIf rVal = -2147217403 Then
                dataValue = "Null Install Status data value"
            Else
                dataValue = "Unknown Error"
            End If
            ReadRegValue = rVal
        End If
    End Function
End Module
[+][-]04.25.2008 at 05:06AM PDT, ID: 21438571

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 06:17AM PDT, ID: 21439176

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 09:08AM PDT, ID: 21440886

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 10:16AM PDT, ID: 21441453

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 10:19AM PDT, ID: 21441479

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 01:01PM PDT, ID: 21442793

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 01:03PM PDT, ID: 21442804

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 01:20PM PDT, ID: 21442925

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 01:45PM PDT, ID: 21443084

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 01:46PM PDT, ID: 21443092

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]04.25.2008 at 02:14PM PDT, ID: 21443246

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Microsoft Visual Basic.Net
Tags: vb.net, ie 7.0
Sign Up Now!
Solution Provided By: TheLearnedOne
Participating Experts: 1
Solution Grade: A
 
 
[+][-]04.25.2008 at 04:35PM PDT, ID: 21443860

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_2_20070628