Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


UpTime

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,40611
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> UpTime
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 UpTime - 12/7/2006 11:25:03 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
I am trying to put two scripts together, I now have syntax error line 73 (Function ConvertTime(seconds))
any ideas



      
 
 
Post #: 1
 
 RE: UpTime - 12/7/2006 11:31:59 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
so the Function was in the loop
How do I get it to query each computer and add it line by line

      

(in reply to gdewrance)
 
 
Post #: 2
 
 RE: UpTime - 12/8/2006 12:08:25 AM   
  ehvbs

 

Posts: 2169
Score: 50
Joined: 6/22/2005
From: Germany
Status: offline
When ebgreen wrote in

  http://www.visualbasicscript.com/m_40352/tm.htm

    It is perfectly valid to stick a sub in the middle. Granted it makes
    the code harder to read and certainly is not best practice, but it
    would compile and run just fine.

  and

    I agree that the sub appears useless in the posted code. I just
    wanted to make sure that it was clear that there is no right or
    wrong place to put procedures as far as the compiler is concerned.
    There are simply guidelines to make things easier to read.

I was tempted to argue against it. But I thought:

   If you read "not best practice" as "a very bad practice" is sounds ok
   
   He says "useless" explicitly
  
   He restricts his statement to the compiler - and who wants to be as dumb
   as a VBScript compiler
  
   Don't be pedantic again
  
That's why I refrained from posting. But this the third occurence of grief
because of putting your subs all over the place in a few days.

  SO DON'T DO IT!


P.S.
 (Why has it to be me? Aren't there other PPs (pedantic paranoiacs) around here?)
 [My apologies to gdewrance for using his topic without helping]
 

(in reply to gdewrance)
 
 
Post #: 3
 
 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

(in reply to ehvbs)
 
 
Post #: 4
 
 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

(in reply to ebgreen)
 
 
Post #: 5
 
 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 >

(in reply to ebgreen)
 
 
Post #: 6
 
 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

(in reply to gdewrance)
 
 
Post #: 7
 
 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"

(in reply to ebgreen)
 
 
Post #: 8
 
 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

(in reply to gdewrance)
 
 
Post #: 9
 
 RE: UpTime - 12/11/2006 5:29:35 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
This is the code from the first script (Thanks ginolard)
It works and echo's all computer names.


      

Then this script for all Machine names IPadresses mac address uptime to excel

      

I have also tried just creating a text file listing all computers and reading that in, but guess what that fails too.
Error on this line
Function ConvertTime(seconds) "Syntax error


      

< Message edited by gdewrance -- 12/11/2006 6:44:18 PM >

(in reply to ebgreen)
 
 
Post #: 10
 
 RE: UpTime - 12/12/2006 12:53:22 AM   
  ebgreen


Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
Jimminy Christmas....how many times do we have to say it DON"T PUT FUNCTIONS IN THE MIDDLE OF YOUR MAIN CODE!

_____________________________

"... 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

(in reply to gdewrance)
 
 
Post #: 11
 
 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

(in reply to ebgreen)
 
 
Post #: 12
 
 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

(in reply to gdewrance)
 
 
Post #: 13
 
 RE: UpTime - 12/12/2006 8:27:44 PM   
  gdewrance


Posts: 587
Score: 3
Joined: 3/16/2006
Status: offline
OK so now I have this working.
The problem is still that a new excel application is opened for PC, how do I get it to write line by line.


      

(in reply to ebgreen)
 
 
Post #: 14
 
 RE: UpTime - 12/13/2006 12:03:16 AM   
  ebgreen


Posts: 4969
Score: 31
Joined: 7/12/2005
Status: offline
You solved this problem yourself in an earlier post. Don't create the Excel object inside the loop.

_____________________________

"... 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

(in reply to gdewrance)
 
 
Post #: 15
 
 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

(in reply to ebgreen)
 
 
Post #: 16
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> UpTime Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts