• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

Script Question - Locate New Servers

When I run the script below, I get " Incorrect scope argument. Use either 'subtree' or 'onelevel' "

This is how I'm running it:

C:\>cscript get_servers.vbs /date:01/01/11 /email:saclayton@jlg.com /base:"OU=Corp_Servers,DC=corp,DC=com"/scope=subtree
Microsoft (R) Windows Script Host Version 5.7
Copyright (C) Microsoft Corporation. All rights reserved.

Incorrect scope argument. Use either 'subtree' or 'onelevel'


What am I doing wrong?

---------------------This is the script----------------------------------------------------------

Set oRootDSE = GetObject("LDAP://RootDSE")
strFilter = "(&(objectclass=computer));"
strAttrs  = "distinguishedName;"
strScope = WScript.Arguments.Named("scope")
strBaseArg = WScript.Arguments.Named("base")
datCutOff = WScript.Arguments.Named("date")
strEmailTo = WScript.Arguments.Named("email")
strSMTPServer = "mail.yourcompany.com"
intSMTPPort = 25
strEmailFrom = "you@yourcompany.com"
If Not(UCase(strScope) = "SUBTREE" Or UCase(strScope) = "ONELEVEL") Then
      WScript.Echo "Incorrect scope argument. Use either 'subtree' or 'onelevel'"
End If
If strBaseArg <> "" Then
      If UCase(strBaseArg) = "ROOT" Then strBase ="<LDAP://" & oRootDSE.get("defaultNamingContext") _
            & ">;" Else strBase = "<LDAP://" & strBaseArg & ">;"
      WScript.Echo strBase
      WScript.Echo "No search base given. Please specify a search base"
End If
If datCutOff = "" Then
      WScript.Echo "No date given. Please specify a search base"
End if
Set objConn = CreateObject("ADODB.Connection")
objConn.Provider = "ADsDSOObject"
objConn.Open "Active Directory Provider"
On Error Resume Next
Set objRS = objConn.Execute(strBase & strFilter & strAttrs & strScope)
If Err.Number <> 0 Then
      WScript.Echo "Error performing query: " & Err.Number & " - " & Err.Description
      On Error Goto 0      
      If objRs.RecordCount > 0 Then
            strEmailBody = "<font face='Verdana' size='1'><b>Report of servers created on or after " & datCutOff & "...<br>"
            strEmailBody = strEmailBody & "Searching: " & Replace(Replace(strBase,"<",""),">","") & " (" & strScope & ")</b><br><br>"
            While Not objRS.EOF
                  Set objPC = GetObject("LDAP://" & Replace(objRS.Fields(0).Value,"/","\/"))
                  strWhenCreated = objPC.whenCreated
                  If InStr(UCase(objPC.operatingSystem),"SERVER") > 0 Then
                        If DateValue(strWhenCreated) > DateValue(datCutOff) Then
                              WScript.Echo objPC.distinguishedName
                              WScript.Echo "This server was created on : " & strWhenCreated
                              WScript.Echo ""
                              strEmailBody = strEmailBody & objPC.distinguishedName & "<br>" & "This server was created on : " & strWhenCreated & "<br><br>"
                        End If
                  End If
            strEmailBody = strEmailBody & "</font>"
            Set objPC = Nothing
            'SEND THE EMAIL....
            If strEmailTo <> "" Then
                  WScript.Echo "Sending email to " & strEmailTo & "....."
                  On Error Resume Next
                  Set objMail = CreateObject("CDO.Message")
                  objMail.Subject = "Server Creation Report"
                  objMail.From = strEmailFrom
                  objMail.To = strEmailTo
                  objMail.HTMLBody = strEmailBody
                  objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                  objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strSMTPServer
                  objMail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = intSMTPPort
                  If Err.Number = 0 Then
                        WScript.Echo "Email Sent. Script Finished"
                        WScript.Echo "Error sending email: " & Err.Number & " - " & Err.Description
                  End if
                  Set objMail = Nothing
            End If
            WScript.Echo "No servers found for this search criteria"
      End If
End If
Set oRootDSE = Nothing
Set objConn = Nothing
1 Solution
Does it help if you put a space between

and also change the equals to a colon, so it becomes
/base:"OU=Corp_Servers,DC=corp,DC=com" /scope:subtree


Glen KnightCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Tackle projects and never again get stuck behind a technical roadblock.
Join Now