Link to home
Start Free TrialLog in
Avatar of 056044787
056044787Flag for Israel

asked on

VBS to run on specific OU

Hi,

I have this script, it can run automaticly on each computer on the computers OU.
I need this script to run on other OU (named UK). what do i have to change in this code in order to implement this?

Thanks!
Set colComputers = GetObject("LDAP://CN=Computers, DC=Domain, DC=com")
set fso=CreateObject("scripting.filesystemobject")
strTargetPath="c:\scripts\"
i=1
 
 
For Each objComputer in colComputers
	strComputer = objComputer.CN
	on error resume next
	set fOut=fso.CreateTextFile(strTargetPath & strComputer & ".txt", true)
	if Err.Number<>0 then
		Err.Clear
		set fOut=fso.CreateTextFile(strTargetPath & "INVALID_NAME" & i & ".txt", true)
		i=i+1
		fOut.WriteLine "Computer name: " & strComputer
		fOut.WriteLine ""
	end if
' ----------
	
	'Sub KBcheck()
Const HKEY_LOCAL_MACHINE = &H80000002
 
'strComputer = "l-yairn"
Set objRegistry = GetObject("winmgmts:\\" & _ 
    strComputer & "\root\default:StdRegProv")
 
strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\KB955839"
strValueName = "Installed"
 
objRegistry.GetDwordValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 
 
If IsNull(strValue) Then
 
      	fOut.WriteLine   "No value - You should install KB955839"
		Wscript.Echo "No value - You should install KB955839"
Else
 
    If strValue = 1 then 
	'intValue = 3
	'objRegistry.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,intValue
	' Wscript.Echo "value exist"
	 fOut.WriteLine  "KB exist"
	 Wscript.Echo  "KB exist"
    End If
 
End If
 
if Err.Number=0 then
		on error goto 0
'end sub
' -----------
	else
		Err.Clear
		Wscript.Echo  "Computer not available"
		'fOut.WriteLine "Computer not available"
	end if
	on error goto 0
	fOut.Close
	set fOut=nothing
Next
set fso=Nothing

Open in new window

Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

http://www.rlmueller.net/LDAP_Binding.htm

Set colComputers = GetObject("LDAP://CN=Computers, OU=UK, DC=Domain, DC=com")
ged325 is right. You just need to specify your OU path (in reverse order) on this line:
Set colComputers = GetObject("LDAP://CN=Computers, DC=Domain, DC=com")

So the above looks at domain.com\Computers

If you want to look at domain.com\Sites\UK\Computers, you would use
Set colComputers = GetObject("LDAP://OU=Computers,OU=UK,OU=Sites, DC=Domain, DC=com")

Regards,

Rob.
Avatar of 056044787

ASKER

That's what i thought, but it doesn't work...
i get the following error:

(1, 1) (null): There is no such object on the server.


your LDAP path is wrong then.

You have to change DC=Domain with your actual domain . . . can you post your full path here so we ensure it's correct or screen shot?
No such objct means you haven't specified the correct path....make sure it's in reverse order, except for the last DC parts....

Rob.
first -  of course i wrote domain name o.k. actually it's wokring well on computers OU. but i need this to run on all the OUs in the domain or alternatively on the UK OU, which located the same level as Computers OU.
So no need to write in reverse. so i guess there is someting else that missing?...



ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Great! now i wonder how can i run this on my entire Domain (OUs)
OK, this will run over your entire domain....all computer objects....

Regards,

Rob.
set fso=CreateObject("scripting.filesystemobject")
strTargetPath="c:\scripts\"
i=1
 
Const ADS_SCOPE_SUBTREE = 2
Const HKEY_LOCAL_MACHINE = &H80000002
 
Set objConnection = CreateObject("ADODB.Connection")
Set objCommand = CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
 
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText = _
    "SELECT Name FROM 'LDAP://DC=domain,DC=com' WHERE objectClass='computer'"  
objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE
Set objRecordSet = objCommand.Execute
objRecordSet.MoveFirst
 
Do Until objRecordSet.EOF
	strComputer = objComputer.Name
	On Error Resume Next
	Set fOut=fso.CreateTextFile(strTargetPath & strComputer & ".txt", true)
	If Err.Number<>0 Then
		Err.Clear
		Set fOut=fso.CreateTextFile(strTargetPath & "INVALID_NAME" & i & ".txt", true)
		i=i+1
		fOut.WriteLine "Computer name: " & strComputer
		fOut.WriteLine ""
	End If
	' ----------
	
	'Sub KBcheck()
 
	'strComputer = "l-yairn"
	Set objRegistry = GetObject("winmgmts:\\" & strComputer & "\root\default:StdRegProv")
 
	strKeyPath = "SOFTWARE\Microsoft\Windows NT\CurrentVersion\Hotfix\KB955839"
	strValueName = "Installed"
 
	objRegistry.GetDwordValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
 
	If IsNull(strValue) Then
		fOut.WriteLine   "No value - You should install KB955839"
		Wscript.Echo "No value - You should install KB955839"
	Else
		If strValue = 1 then 
			'intValue = 3
			'objRegistry.SetDWORDValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,intValue
			' Wscript.Echo "value exist"
			fOut.WriteLine  "KB exist"
			Wscript.Echo  "KB exist"
		End If
	End If
 
	If Err.Number=0 Then
		On Error GoTo 0
		'end sub
		' -----------
	Else
		Err.Clear
		Wscript.Echo  "Computer not available"
		'fOut.WriteLine "Computer not available"
	End If
	On Error GoTo 0
	fOut.Close
 
	objRecordSet.MoveNext
Loop
Set fOut=Nothing

Open in new window