[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Script Question - Locate New Servers

Posted on 2011-05-02
3
Medium Priority
?
201 Views
Last Modified: 2013-12-05
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
Comment
Question by:KurtHoffeditz
2 Comments
 
LVL 65

Accepted Solution

by:
RobSampson earned 500 total points
ID: 35509082
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
 
LVL 74

Expert Comment

by:Glen Knight
ID: 37485317
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Office 365 is a subscriptions based service which includes services like Exchange Online and Skype for business Online. These services integrate with Microsoft's online version of Active Directory called Azure Active Directory.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question