• 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'

C:\>

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")
 
'YOU'LL NEED TO EDIT THESE YOURSELF!!!
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'"
      WScript.Quit
End If
 
If strBaseArg <> "" Then
      If UCase(strBaseArg) = "ROOT" Then strBase ="<LDAP://" & oRootDSE.get("defaultNamingContext") _
            & ">;" Else strBase = "<LDAP://" & strBaseArg & ">;"
      WScript.Echo strBase
Else
      WScript.Echo "No search base given. Please specify a search base"
      WScript.Quit
End If
 
If datCutOff = "" Then
      WScript.Echo "No date given. Please specify a search base"
      WScript.Quit
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
Else
      On Error Goto 0      
      
      If objRs.RecordCount > 0 Then
            objRS.MoveFirst
            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
                  objRS.MoveNext
            Wend
            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
                  objMail.Configuration.Fields.Update
                  objMail.Send
                  If Err.Number = 0 Then
                        WScript.Echo "Email Sent. Script Finished"
                  Else
                        WScript.Echo "Error sending email: " & Err.Number & " - " & Err.Description
                  End if
                  Set objMail = Nothing
            End If
      Else
            WScript.Echo "No servers found for this search criteria"
      End If
End If
 
Set oRootDSE = Nothing
Set objConn = Nothing
0
KurtHoffeditz
Asked:
KurtHoffeditz
1 Solution
 
RobSampsonCommented:
Does it help if you put a space between
/base:"OU=Corp_Servers,DC=corp,DC=com"/scope=subtree

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

Regards,

Rob.
0
 
Glen KnightCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
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