Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to I create a varible in VBScript?

Status
Not open for further replies.

matrix101

MIS
Jun 5, 2007
60
US
Hello,

I'm new to vbscript and am trying to figure out how to write a vbscript that will: move all computers in the computer container beginning with CSC to a specific OU named Corp-SD.

I'm using a basic script Microsoft put out:

Set objNewOU = GetObject("LDAP://OU=CORP-SD, OU=Workstations, OU=Managed Objects, dc=corp, DC=jitb, DC=net")
Set objMoveComputer = objNewOU.MoveHere _
("LDAP://CN=CSCT1*, CN=Computers, dc=corp, DC=jitb, DC=net", _
"CN=CSCT1*")

This script works fine as long as your specify the computer name. All computers that belong in the CORP-SD OU start with CSC so I wanted to do something like CSC* but it didn't work. Any Ideas???

Thx!


 
You can't do what you are trying in that manner because the distinguished name "LDAP://XXXXX" needs to be exact. What you will need to do is Query AD ( for any computer with a name starting with CSCT1* and get their Distinguished names to move them.



--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Easy one. First enumerate through the computers. Grab the name and distinguishedName. Then check if the name matches your criteria and if so do the move.

Code:
[green]
'==========================================================================
'
' NAME: MoveCSCComputers.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' DATE  : 06/05/07
'
' COMMENT: Moves computers from computer container to an OU if prefix
'          in the name matches criteria
'          
'
'    THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
'    ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED To
'    THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
'    PARTICULAR PURPOSE.
'
'    IN NO EVENT SHALL THE SPIDER'S PARLOR AND/OR ITS RESPECTIVE SUPPLIERS 
'    BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY
'    DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
'    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
'    ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
'    OF THIS CODE OR INFORMATION.
'
'==========================================================================[/green]

Option Explicit
On Error Resume Next

dim qQuery
dim objConnection
dim objCommand
dim objRecordSet
dim objNewOU 
dim objMoveComputer 

qQuery = "<LDAP://CN=computers,DC=corp,DC=jitb,DC=net>;" & _
		"(objectCategory=computer)" & _
       ";distinguishedName,name;subtree"

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Open "Provider=ADsDSOObject;"
objCommand.ActiveConnection = objConnection
objCommand.CommandText = qQuery
Set objRecordSet = objCommand.Execute

While Not objRecordSet.EOF
  If Left(UCase(objRecordSet.Fields("name")),3) = "CSC" Then
    Set objNewOU = GetObject("LDAP://OU=CORP-SD, OU=Workstations, OU=Managed Objects, dc=corp, DC=jitb, DC=net")
    Set objMoveComputer = objNewOU.MoveHere _
        ("LDAP://" & objRecordSet.Fields("distinguishedName"))
  End If
    objRecordSet.MoveNext
Wend
objConnection.Close

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Rather than comparing the computer name in the While Loop I'd change the query to only return those that start with CSCT1.

Something like...
qQuery = "<LDAP://CN=computers,DC=corp,DC=jitb,DC=net>;" & _
"(&(objectCategory=computer)(name=CSCT1*))" & _
";name,distinguishedname;subtree"

--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
Good suggestion DM4EVER. I was going to do that but had a brain fart ans was trying to use the "like" comparrison and it was failing on me. Forgot about the wildcard. [blush]

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Thanks for the input everyone!

I should have mentioned this before but I will be searching for computers starting with CSCT1... and DAL...

Would I still follow the same concepts as listed here?

Thanks again!
 
Yes.

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top