Login | |
|
 |
RE: UpTime - 12/8/2006 1:27:32 AM
|
|
 |
|
| |
ebgreen
Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
|
I can be just as pedantic as the next guy and I will always point out what I consider to be not best (very bad) practices. I promise my next post in this thread will actually be helpful.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: UpTime - 12/8/2006 1:42:19 AM
|
|
 |
|
| |
ebgreen
Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
|
Ok, I'm going to repost your code without code tags so that I can use formatting to color my comments: On error resume Next 'Global On Error Resume Next is always bad IMO Dim objConnection,objCommand,objRootLDAP,strDNSDomain Const ADS_SCOPE_SUBTREE = 2 Set objConnection = CreateObject("ADODB.Connection") Set objCommand = CreateObject("ADODB.Command") Set objRootLDAP = GetObject("LDAP://RootDSE") strDNSDomain = objRootLDAP.Get("DefaultNamingContext") Wscript.Echo "Searching " & strDNSDomain objConnection.Provider = "ADsDSOObject" objConnection.Open "Active Directory Provider" objConnection.Cursorlocation=3 Set objCommand.ActiveConnection = objConnection '******** First CommandText = Search for a single user (strUser variable) *********** '******** Second Commandtext = Return all enabled users *********** '******** Third Commandtext = Return all users (enabled AND disabled) *********** '******** Fourth CommandText = Return all computers ************* 'strUser="xxxx" 'objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)(samaccountname=" & strUser & ")); sAMAccountName ;subtree" 'objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(&(objectCategory=person)(objectClass=user)(!userAccountControl:1.2.840.113556.1.4.803:=2)); name,sAMAccountName ;subtree" 'objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(&(objectCategory=person)(objectClass=user)); name,sAMAccountName ;subtree" objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer); name ;subtree" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set objRecordSet = objCommand.Execute objRecordSet.Sort="name" objRecordSet.MoveFirst Do Until objRecordSet.Eof 'your indentation is very inconsistent 'as a matter of fact it is so inconsistent that I will have to fix it to avoid giving 'myself a headache...I hate friday headaches. Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 'Here you are trying to connect to WMI on a computer whose name is stored in the 'variable strComputer but you never set strComputer to have any value. If you had not used the global On Error Resume Next, you would have seen this error Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Add intRow = 2 objExcel.Cells(1, 1).Value = "Machine Name" objExcel.Cells(1, 2).Value = "IP Address" objExcel.Cells(1, 3).Value = "MAC Address" objExcel.Cells(1, 4).Value = "Days" objExcel.Cells(1, 5).Value = "Hours" objExcel.Cells(1, 6).Value = "Minutes" objExcel.Cells(1, 7).Value = "Report Time Stamp" Set colAdapters = objWMIService.ExecQuery ("SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True") For Each objAdapter in colAdapters objExcel.Cells(intRow, 1).Value = objAdapter.DNSHostName If Not IsNull(objAdapter.IPAddress) Then For i = 0 To UBound(objAdapter.IPAddress) objExcel.Cells(intRow, 2).Value = objAdapter.IPAddress(i) Next End If objExcel.Cells(intRow, 3).Value = objAdapter.MACAddress Next Set colObjects = objWMIService.ExecQuery ("SELECT * FROM Win32_PerfRawData_PerfOS_System") For Each objWmiObject In colObjects intPerfTimeStamp = objWmiObject.Timestamp_Object intPerfTimeFreq = objWmiObject.Frequency_Object intCounter = objWmiObject.SystemUpTime Next iUptimeInSec = (intPerfTimeStamp - intCounter)/intPerfTimeFreq sUptime = ConvertTime(iUptimeInSec) objExcel.Cells(intRow, 7).Value = Now() intRow = intRow + 1 objExcel.Range("A1:G1").Select objExcel.Selection.Interior.ColorIndex = 19 objExcel.Selection.Font.ColorIndex = 11 objExcel.Selection.Font.Bold = True objExcel.Cells.EntireColumn.AutoFit 'Wscript.Echo "Done" 'WScript.Echo objRecordSet.Fields("name") objRecordSet.Movenext Loop objRecordSet.close objConnection.close 'I realize that the only code after this function is just cleaning up, but it is still code. DON'T PUT FUNCTIONS IN THE MIDDLE OF THE MAIN CODE. It confuses things. Function ConvertTime(seconds) ConvDays = seconds \ (3600 * 24) ConvHour = (seconds Mod (3600 * 24)) \ 3600 ConvMin = (seconds Mod 3600) \ 60 objExcel.Cells(intRow, 4).Value = ConvDays objExcel.Cells(intRow, 5).Value = ConvHour objExcel.Cells(intRow, 6).Value = ConvMin End Function Set objWMIService = Nothing Set objExcel = Nothing Set colAdapters = Nothing Set colObjects = Nothing It looks like your main functional issue is just that you never set strComputer to anything. Figure that out and I think you will be headed in the right direction. As a hint, try to understand this part of the code and what it returns: objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer); name ;subtree" objCommand.Properties("Page Size") = 1000 objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE Set objRecordSet = objCommand.Execute objRecordSet.Sort="name" objRecordSet.MoveFirst
< Message edited by ebgreen -- 12/8/2006 1:46:27 AM >
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: UpTime - 12/10/2006 8:30:04 PM
|
|
 |
|
| |
gdewrance
Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
|
Thanks ebgreen this line objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer); name ;subtree" I dont understand it. I take it that it sets objCommand.CommandText to forest root as [strDNSDomain = objRootLDAP] & gets the computer (OU),Name(could this be the strComputer?);subtree ( I take it any folders under the computer OU) I thought that all you needed was [strComputer = WSHNetwork.ComputerName] but that didn't work by the way what makes a new line in excel, instead of opening a new excel document each time? (can find info on this either)
< Message edited by gdewrance -- 12/10/2006 9:28:48 PM >
|
|
| |
|
|
|
 |
RE: UpTime - 12/11/2006 1:39:11 AM
|
|
 |
|
| |
ebgreen
Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
|
" objCommand.CommandText = "<LDAP://" & strDNSDomain & ">;(objectCategory=computer); name ;subtree" I dont understand it. I take it that it sets objCommand.CommandText to forest root as [strDNSDomain = objRootLDAP] & gets the computer (OU),Name(could this be the strComputer?);subtree ( I take it any folders under the computer OU)" I am definitely not the person to be answering AD/LDAP questions. Not my baliwick, but I believe you are pretty much correct. "I thought that all you needed was [strComputer = WSHNetwork.ComputerName] but that didn't work" Where are you putting this and what "...didn't work" about it? Errors? Wrong goutput? No output?
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: UpTime - 12/11/2006 1:48:45 AM
|
|
 |
|
| |
gdewrance
Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
|
no output. using strComputer = WSHNetwork.ComputerName I'm not getting the computer account to inserted into the excel spreadsheet, therefore queries only the default computer One other problem is I think, this should be taken out the loop, else a new excel document will be created each time! Set objExcel = CreateObject("Excel.Application") objExcel.Visible = True objExcel.Workbooks.Add intRow = 2 objExcel.Cells(1, 1).Value = "Machine Name" objExcel.Cells(1, 2).Value = "IP Address" objExcel.Cells(1, 3).Value = "MAC Address" objExcel.Cells(1, 4).Value = "Days" objExcel.Cells(1, 5).Value = "Hours" objExcel.Cells(1, 6).Value = "Minutes" objExcel.Cells(1, 7).Value = "Report Time Stamp"
|
|
| |
|
|
|
 |
RE: UpTime - 12/11/2006 2:08:29 AM
|
|
 |
|
| |
ebgreen
Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
|
You are correct that the object creation should not be in the loop. I'll go you one better. Comment out all of the excel stuff altogether for the time being. Focus on just getting the information back first. Then you can worry about nice pretty output. So, I would suggest commenting out the excel stuff. Use WScript.Echo to echo out information about everything under the sun (pretty much anytime that you set a variable to something echo it out so that you can be sure that it is set to what you though it would be). Use the command line to run the script anf run it using CScript.exe.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: UpTime - 12/12/2006 12:58:19 AM
|
|
 |
|
| |
gdewrance
Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
|
I didn't the authors did, I'm just picking up bad habits
|
|
| |
|
|
|
 |
RE: UpTime - 12/12/2006 1:08:09 AM
|
|
 |
|
| |
ebgreen
Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
|
Well your bad habit is the cause of your error. I know that I said the compiler doesn't care if you put a function in the middle of your main code. Well, I was only partially right. It does care if the function is inside of another code block. In your case you are declaring a function inside of a loop. this just gives the compiler fits. You need to do a reset. List all of the steps that you need the script to do roughly in the order that they need to happen please.
_____________________________
"... when you are good and crazy, oooh, oooh, oooh, the sky is the limit!" - The Tick Goog places to start:http://www.visualbasicscript.com/m_24727/tm.htm http://www.visualbasicscript.com/m_47117/tm.htm
|
|
| |
|
|
|
 |
RE: UpTime - 12/13/2006 12:09:37 AM
|
|
 |
|
| |
gdewrance
Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
|
Yip just fixed it. Thanks
|
|
| |
|
|
|
|
|