Solved

VBscript 'Variable is undefined'

Posted on 2011-03-22
5
1,592 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 55

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 55

Expert Comment

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

~bp
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

687 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