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
Solved

VBscript 'Variable is undefined'

Posted on 2011-03-22
5
1,504 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
  • 3
  • 2
5 Comments
 
LVL 53

Accepted Solution

by:
Bill Prew earned 500 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 53

Expert Comment

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

~bp
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

840 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