Solved

VBscript 'Variable is undefined'

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

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 51

Expert Comment

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

~bp
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Well hello again!  Glad to see you've made it this far without giving up.  In this, the fourth installment of my popular series, I'm going to cover functions and subroutines, what they are, and why they are useful.  Just in case you stumbled onto th…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now