Solved

VBscript 'Variable is undefined'

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

770 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