Advertisement
Advertisement
| 04.24.2008 at 03:28PM PDT, ID: 23352180 |
|
[x]
Attachment Details
|
||
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
|