Announcement

Collapse
No announcement yet.

object required errors

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • object required errors

    I'm not sure what's wrong with this code....but according to computerperformance.co, I have something spelled wrong somewhere or the object isn't declared.. I looked everywhere and couldn't find the misspelling.

    This script goes through the domain, uses the Function IsConnectible from rlmueller to see if the machines are alive, determines if the machine is XP, checks if a specific reg key exists, and if it doesn't, adds the reg key. Everything is outputted to a text file.

    The reg key removes the "Previous Versions" tab that is associated with shadow copy. I believe it was rolled out by default with xp service pack 2.

    The error is on line 104 saying "Object Required: 'objRegService' which is with the RegKeyExists function at the bottom of the script.

    Now I tried setting objRegService inside the function too, but I still got errors. Do I need to add "with" statements in the function?

    Code:
    Const HKLM = &H80000002
    
    'OU to query
    
    Remove_Shadow "Wayne 1"
    Remove_Shadow "Wayne 2"
    
    Sub Remove_Shadow(sOU)
    
     'On Error Resume Next
    
     strLogFile = "C:\log.txt"
     strRegKey = "Software\Microsoft\Windows\CurrentVersion\Explorer"
     Set objFSO = CreateObject("Scripting.FileSystemObject")
     Set objFile = objFSO.OpenTextFile(strLogFile, 8) 
    
     Set objRootDSE = GetObject("LDAP://rootDSE")
     strDNSDomain = objRootDSE.Get("defaultNamingContext")
    
     'Start the ADO connection
     Set objCommand = CreateObject("ADODB.Command")
     Set objConnection = CreateObject("ADODB.Connection")
     objConnection.Provider = "ADsDSOObject"
     objConnection.Open "Active Directory Provider"
     objCommand.ActiveConnection = objConnection
    
     'Set the ADO connection query strings
     StartNode = strDNSDomain
     SearchScope = "subtree"
    
     FilterString = "(&(sAMAccountType=805306369)(name=*))"
    
     Attributes = "adspath"
    
     'Create the LDAP-Query
     LDAPQuery = "<LDAP://OU=" & sOU & "," & StartNode & ">;" & FilterString & ";" _
                	& Attributes & ";" & SearchScope
    
     objCommand.CommandText = LDAPQuery
     objCommand.Properties("Page Size") = 100
     objCommand.Properties("Timeout") = 30
     objCommand.Properties("Cache Results") = False
    
     Set objRecordSet = objCommand.Execute
    
     If NOT objRecordSet.eof Then
      objRecordSet.MoveFirst
       While Not objRecordset.EOF
        	Set objRecord = GetObject(objRecordSet.Fields("AdsPath").Value)
    	strComputer = objRecord.CN
    			If (IsPingable(strComputer) = True) then
    				Set objWMIService = GetObject("winmgmts:" _
        					& "{impersonationLevel=impersonate}!\\"_
        					& strComputer & "\root\cimv2")
    				Set colItems = objWMIService.ExecQuery("SELECT * FROM Win32_OperatingSystem",,48)
    					For Each objItem In colItems
    						If (InStr(1,objItem.Caption, "XP", 1)) Then
    						Set objRegService = GetObject("WinMgmts:{impersonationLevel=impersonate}!//" _
    	 						& strComputer & "/root/default:stdRegProv")
    							If RegKeyExists(HKLM, strRegKey, "NoPreviousVersionsPage") Then
    								objFile.WriteLine strComputer & " : Reg Key already exists!"	
    							Else
    								objRegService.SetDwordValue HKLM ,strRegKey ,"NoPreviousVersionsPage" , 1
    								objFile.WriteLine strComputer & " : Reg Key installed!"
    							End If
    						End If
    					Next
    			Else
    				objFile.WriteLine strComputer & " : was not pingable!"
    			End If
        	objRecordSet.MoveNext
      Wend
     End If
    objFile.Close
    End Sub
    
    Function IsPingable(ByVal strHost)
      If Trim(strHost) <> "" Then
         strCommand = "Ping.exe -n 1 -w 750 " & strHost
         Set objShell = CreateObject("WScript.Shell")
         Set objExecObject = objShell.Exec _
            ("%comspec% /c title " & strHost _
            & chr(38) & strCommand)
         Do While Not objExecObject.StdOut.AtEndOfStream
            strText = objExecObject.StdOut.ReadLine()
            If Instr(strText, "TTL=") > 0 _
              Then IsPingable = True : Exit Do
         Loop
         If IsPingable = True then
            With GetObject("winmgmts:root\cimv2")
               For Each objProcess in .ExecQuery _
                  ("SELECT commandline FROM Win32_Process" _
                  & " WHERE Name = 'ping.exe'",,48)
                  If objProcess.commandline = strCommand _
                    Then objProcess.Terminate() : Exit For
               Next
            End With
         End If
      End If
      If (not IsPingable = True) Then IsPingable = False
    End Function
    
    Function RegKeyExists(nHive, strPath, strValueName)
    	objRegService.GetDwordValue nHive, strPath, strValueName, strValue
      	RegKeyExists = Not IsNull(strValue)
    End Function
    
    msgbox "Done!"
    WScript.Quit

  • #2
    Re: object required errors

    In your script the object objRegService does not have a global scope.
    It is created in the subroutine "Remove_Shadow" and therefore unknown to the function "RegKeyExists".
    To give it a global scope - declare objRegService in the main script before the subroutine "Remove_Shadow" is called.
    Code:
    Const HKLM = &H80000002
    Dim objRegService
    
    'OU to query
    
    Remove_Shadow "Wayne 1"
    Remove_Shadow "Wayne 2"
    
    <.................>


    One other thing, about the Function IsPingable(ByVal strHost), two things actualy about that function
    1. The object objShell is created over and over for every computer in the record set. It would be less expensive if you create the object once in the main script before the function is called, it will have a global scope then.

    2. The other thing is that since you have changed the number of echo requests for the Ping to 1, the "Win32_Process" part has become useless. This function was written for multiple echo requests, the moment that "TTL" is echoed the "Win32_Process" part is actioned to terminate the Ping task. So if the computer is pingable it takes just one or two pings, if not it continuous trying '-n' times.

    If you like the function to send only 1 request you can use something like:
    Code:
    Function IsPingable(ByVal strHost)
      ' objShell has (is expected to have) a global scope!
      If Trim(strHost) <> "" Then
         strCommand = "Ping.exe -n 1 -w 750 " & strHost
         Set objExecObject = objShell.Exec _
            ("%comspec% /c title " & strHost _
            & chr(38) & strCommand)
         Do While Not objExecObject.StdOut.AtEndOfStream
            strText = objExecObject.StdOut.ReadLine()
            If Instr(strText, "TTL=") > 0 _
              Then IsPingable = True
         Loop
      End If
      If (not IsPingable = True) Then IsPingable = False
    End Function

    \Rems

    This posting is provided "AS IS" with no warranties, and confers no rights.

    __________________

    ** Remember to give credit where credit's due **
    and leave Reputation Points for meaningful posts

    Comment


    • #3
      Re: object required errors

      OoOOo, that's what those Dim statements are for lol. I couldn't understand why it was working when it wasn't in a subroutine, as apposed to in the subroutine, but now that make sense. I always thought people used the Dim statements to look more professional

      From MSDN:
      Variables declared with Dim at the script level are available to all procedures within the script. At the procedure level, variables are available only within the procedure.
      Well that's good to know. And also thanks for the updated ping function (although the other function makes more sense, so I'll probably change the pings back to 2 or 3)
      Last edited by ekrengel; 25th March 2009, 22:01.

      Comment


      • #4
        Re: object required errors

        Originally posted by ekrengel View Post
        I always thought people used the Dim statements to look more professional
        You should always start your scripts with "option explicit" statement. It will require you to declare all the variables with dim statement. This helps you to catch typos and other errors. In this case, the "object required" error should be "Variable is undefined", which is more useful a message.

        -vP

        Comment

        Working...
        X