Avatar of 056044787
056044787
Flag 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

Visual Basic ClassicVB ScriptMicrosoft Legacy OS

Avatar of undefined
Last Comment
RobSampson

8/22/2022 - Mon
Kyle Abrahams

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

Set colComputers = GetObject("LDAP://CN=Computers, OU=UK, DC=Domain, DC=com")
RobSampson

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.
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.


All of life is about relationships, and EE has made a viirtual community a real community. It lifts everyone's boat
William Peck
Kyle Abrahams

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?
RobSampson

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.
056044787

ASKER
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?...



⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
ASKER CERTIFIED SOLUTION
RobSampson

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
056044787

ASKER
Great! now i wonder how can i run this on my entire Domain (OUs)
RobSampson

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