?
Solved

VBscript 'Variable is undefined'

Posted on 2011-03-22
5
Medium Priority
?
1,640 Views
Last Modified: 2012-05-11
I have computers in the default cn=computers,dc=domain,dc=ext OU.  I want to determine what OS they're running and move the computers objects to selected OU's.  I 'borrowed' the following script from experts.  When I run it there's an error at Line 45,1 'Variable is undefined: 'objMoveComputer'.  If there are no Windows 7 computers then it changes to Line 47 for the XP machines.

The odd thing is the script moves one computer to the correct OU, gives the error and exists.  If I run it again it moves the next computer to the correct followed by the error.

We  have Windows 7 and XP machines we want to move to OU's just for those OS's and ignore anything else.  The domain is a Windows2003.  The code is below, I've also attached it as a file to make for easier reading.


MoveWks.vbs
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Option Explicit

Dim objRootDSE, strDNSDomain, objCommand, objConnection
Dim strBase, strFilter, strAttributes, strQuery, objRecordSet
Dim strDN, strOS, objWin7OU, objXPOU

' Bind to target Win7 and XP OU containers.
Set objWin7OU = GetObject("LDAP://OU=Windows7,OU=Virtual Workstations,OU=HomeResources,DC=bigmac,DC=home")
Set objXPOU = GetObject("LDAP://OU=WindowsXP,OU=Virtual Workstations,OU=HomeResources,DC=bigmac,DC=home")


' Determine DNS domain name.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")


' Use ADO to search Active Directory.
Set objCommand = CreateObject("ADODB.Command")
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
objCommand.ActiveConnection = objConnection
strBase = "<LDAP://" & strDNSDomain & ">"

' Filter on all computer objects.
strFilter = "(objectCategory=computer)"
' Retrieve distinguishedName and operatingSystem attributes.
strAttributes = "distinguishedName,operatingSystem"
strQuery = strBase & ";" & strFilter & ";" & strAttributes & ";subtree"
objCommand.CommandText = strQuery
objCommand.Properties("Page Size") = 100
objCommand.Properties("Timeout") = 30
objCommand.Properties("Cache Results") = False
Set objRecordSet = objCommand.Execute

' Loop through all computer objects.
Do Until objRecordSet.EOF
' Make strings lower case.
strDN = LCase(objRecordSet.Fields("DistinguishedName").Value)
strOS = LCase(objRecordset.Fields("OperatingSystem").Value)
' Check if computer object in cn=computers container.
If (InStr(strDN, "cn=computers,dc=bigmac,dc=home")) Then
' Check OS, skip servers.
If strOS = "windows 7 professional" Then
set objMoveComputer = objWin7OU.MoveHere("LDAP://" & strDN, vbNullString)
ElseIf strOS = "windows xp professional" Then
set objMoveComputer = objXPOU.MoveHere("LDAP://" & strDN, vbNullString)
End If
End If
' Move to the next computer object.
objRecordSet.MoveNext
Loop

' Clean up.
Set objRootDSE = Nothing
Set objCommand = Nothing
Set objConnection = Nothing
Set objRecordSet = Nothing
Set objWin7OU = Nothing
Set objXPOU = Nothing MoveWks.vbs
0
Comment
Question by:BigmacMc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 56

Accepted Solution

by:
Bill Prew earned 2000 total points
ID: 35195349
Since you have OPTION EXPLICIT, every variable you reference needs to be defined with a DIM statement before being used.  SO add a DIM for the variable involved at the top of the script with the other DIMs, as in:

Dim objMoveComputer

~bp
0
 

Author Comment

by:BigmacMc
ID: 35198339
billprew

Is 'Set' replaced with 'Dim' or just added at the beginning of the line?

Ex:  Set objRootDSE = GetObject("LDAP://ROOOTDSE")

bigmac
0
 

Author Comment

by:BigmacMc
ID: 35198474
billprew,

Sorry for being dense.  

I added the 'Dim  objMoveComputer' at the top.  Now I get 'Wrong number of arguments or invalid property assignment' error message.  It still moves one computer and exists.  Thinking perhaps the 'objMoveComputer' had to be in a certain order with other Dim definitions, I add in various places, not still had the same error.

Bigmac
0
 

Author Comment

by:BigmacMc
ID: 35201323
billprew,

Discovered my mistake.  Your solution worked.  The workstations moved to the correct OU.  Thanks very much.
0
 
LVL 56

Expert Comment

by:Bill Prew
ID: 35201507
Sorry I was away for a bit, but glad you got it sorted.

~bp
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
Hello again, all.  For those of you that have been following along, you'll know that this is my third article on this topic (though it is not Part III).  This article is sort of remedial, and probably the topic with which I should have started the s…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

801 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