Page 1 of 1 1
Topic Options
#195801 - 2009-09-08 06:39 PM AD Change users
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Hi all, working on a script to add users details in the Active Directory.

what this script does it pulls the details from our oracle server like user name, first name, lastname and the script should modify the active directory.

The problem is that the varible is not being picked up on $test, however if you look at my script you will see this bit of code ? $test which display the number fine, but not when you try to insert it into the script.

 Code:
;region Script Settings
;<ScriptSettings xmlns="http://tempuri.org/ScriptSettings.xsd">
;  <ScriptPackager>
;    <process>kix32.exe</process>
;    <arguments />
;    <extractdir>%TEMP%</extractdir>
;    <files />
;    <usedefaulticon>true</usedefaulticon>
;    <showinsystray>false</showinsystray>
;    <altcreds>false</altcreds>
;    <efs>true</efs>
;    <ntfs>true</ntfs>
;    <local>false</local>
;    <abortonfail>true</abortonfail>
;    <product />
;    <version>1.0.0.1</version>
;    <versionstring />
;    <comments />
;    <includeinterpreter>false</includeinterpreter>
;    <forcecomregistration>false</forcecomregistration>
;    <consolemode>false</consolemode>
;    <EnableChangelog>false</EnableChangelog>
;    <AutoBackup>false</AutoBackup>
;    <snapinforce>false</snapinforce>
;    <snapinshowprogress>false</snapinshowprogress>
;    <snapinautoadd>0</snapinautoadd>
;    <snapinpermanentpath />
;  </ScriptPackager>
;</ScriptSettings>
;endregion

Break on

Function fn_LDAP()
	Dim $selectedProperties, $propertynames, $oCn, $oCmd, $oRS, $i
	$selectedProperties = "ADsPath"
	$propertynames = Split($selectedProperties, ",")
	
	$oCn = CreateObject("ADODB.Connection")
	$oCmd = CreateObject("ADODB.Command")
	$oCn.Provider = "ADsDSOObject"
	$ = $oCn.Open("Active Directory Provider", "", "", -1)
	$oCmd.ActiveConnection = $oCn
	$oCmd.Properties("Page Size").Value = 1000
	$oCmd.Properties("Searchscope").Value = 2 ;ADS_SCOPE_SUBTREE
	? $test
	$oCmd.CommandText = "SELECT " + $selectedProperties + " FROM 'LDAP://server.co.uk' WHERE objectCategory='user' AND cn = '$test' ORDER BY Name"
	$oRS = $oCmd.Execute
	While Not $oRS.EOF
		$obj = GetObject($oRS.Fields($propertynames[0]))
		;$obj.Put("cn", "VALUE")
		; $obj.SetInfo
	
		$obj.Put("displayName", "TESTVALUE")
		$obj.SetInfo
		$oRS.MoveNext
	Loop
EndFunction

Function fn_ODBC()
	Dim $cn, $cmd, $rs, $cnstring, $cmdtext
	$cnstring = 'Provider=MSDAORA;Password=password;User ID=id;Data Source=server;Persist Security Info=True;'
		
$cmdtext = "SELECT DISTINCT PEOPLE.PERSON_CODE, PEOPLE.FORENAME, PEOPLE.SURNAME, PEOPLE_UNITS.UNIT_INSTANCE_CODE, PEOPLE_UNITS.CALOCC_CODE FROM PEOPLE INNER Join PEOPLE_UNITS ON PEOPLE.PERSON_CODE = PEOPLE_UNITS.PERSON_CODE and PEOPLE_UNITS.UNIT_TYPE = 'R' and PEOPLE_UNITS.CALOCC_CODE = '09' and length(PEOPLE_UNITS.UNIT_INSTANCE_CODE) = '6' "
	$cn = CreateObject("adodb.connection") 
	$cmd = CreateObject("adodb.command") 
	$cn.connectionstring = $cnstring 
	$cn.Open 
	$cmd.activeconnection = $cn 
	$cmd.commandtext = $cmdtext 
		
	$rs = CreateObject("adodb.recordset") 
	$rs.cursortype = 3 
	$rs.locktype = 3 
	$rs.Open($cmd)
	While Not $rs.EOF And Not $rs.BOF
					
						
		? "PERSON_CODE: " + $rs.Fields.Item("PERSON_CODE").Value
		? "FORENAME: " + $rs.Fields.Item("FORENAME").Value
		? "SURNAME: " + $rs.Fields.Item("SURNAME").Value
		? "YEAR: " + $rs.Fields.Item("CALOCC_CODE").Value
		? "GROUP: " + $rs.Fields.Item("UNIT_INSTANCE_CODE").Value
		? "PERSON_CODE: " + $rs.Fields.Item("CODE").Value
		? " "
		$test = $rs.Fields.Item("PERSON_CODE").Value
							
		 $rs.Close
		 $cn.Close
	EndFunction
	fn_ODBC()	
	fn_LDAP()
	
	fn_QUIT()
			
	Function fn_quit()
		Quit
	EndFunction
		


However if I do the following the program works.

$test = 123456

the statement above will find the varible text and modify the AD account.

It would be great if someone can help me out.

Glyn

Top
#195802 - 2009-09-08 08:55 PM Re: AD Change users [Re: 5861king]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
I also added thios command ? $oCmd.CommandText to see what the output was like, and below is the output, and as you can see the cn'' has no number inside the brackets

' AND cn='' ORDER BY Name://student.server.co.uk' WHERE objectCategory='user


Edited by 5861king (2009-09-08 08:55 PM)

Top
#195803 - 2009-09-09 02:02 AM Re: AD Change users [Re: 5861king]
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
This looks like a script generated with ASE (Admin Script Editor).

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#195809 - 2009-09-09 12:53 PM Re: AD Change users [Re: Kdyer]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
o.k.

need some help here I have posted below a basic script to update AD. The problem here is that the name I'm trying to update has a apostrophe in the name. and I think this is what is causeing the script not work. I think it fails during the LDAP lookup However no errors are displayed in the script.

 Code:
Dim $selectedProperties, $propertynames, $oCn, $oCmd, $oRS, $i
$selectedProperties = "ADsPath"
$propertynames = Split($selectedProperties,",")

$FN = '1'
$SN = '2'

$oCn = CreateObject("ADODB.Connection")
$oCmd = CreateObject("ADODB.Command")
$oCn.Provider = "ADsDSOObject"
$ = $oCn.Open("Active Directory Provider", "", "", -1)
$oCmd.ActiveConnection = $oCn
$oCmd.Properties("Page Size").Value = 1000
$oCmd.Properties("Searchscope").Value = 2 ;ADS_SCOPE_SUBTREE
$oCmd.CommandText = "SELECT " + $selectedProperties + " FROM 'LDAP://Student.carshalton.ac.uk' WHERE objectCategory='user' AND description = 'A'me Down' ORDER BY Name"

$oRS = $oCmd.Execute
While Not $oRS.EOF
		$obj = GetObject($oRS.Fields($propertynames[0]))
		$obj.Put("givenName", "$FN")
		$obj.Put("sn", "$SN")
		$obj.SetInfo
	$oRS.MoveNext
Loop


Any help much appreciated.

Thanks

Glyn

Top
#195810 - 2009-09-09 01:56 PM Re: AD Change users [Re: 5861king]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
' = chr(39)
Maybe that helps?
Or try to wrap your single quotes in double quotes

Top
#195811 - 2009-09-09 02:20 PM Re: AD Change users [Re: Witto]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
I think you were talking about this?
I don't know if this will work:
 Code:
$oCmd.CommandText = "SELECT "
$oCmd.CommandText = $oCmd.CommandText + $selectedProperties
$oCmd.CommandText = $oCmd.CommandText + " FROM 'LDAP://Student.carshalton.ac.uk'"
$oCmd.CommandText = $oCmd.CommandText + " WHERE objectCategory='user' "
$oCmd.CommandText = $oCmd.CommandText + " AND description = "
$oCmd.CommandText = $oCmd.CommandText + '"A'me Down"'
$oCmd.CommandText = $oCmd.CommandText + " ORDER BY Name"

Top
#195815 - 2009-09-09 04:45 PM Re: AD Change users [Re: Witto]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
 Originally Posted By: Witto
I think you were talking about this?
I don't know if this will work:
 Code:
$oCmd.CommandText = "SELECT "
$oCmd.CommandText = $oCmd.CommandText + $selectedProperties
$oCmd.CommandText = $oCmd.CommandText + " FROM 'LDAP://Student.carshalton.ac.uk'"
$oCmd.CommandText = $oCmd.CommandText + " WHERE objectCategory='user' "
$oCmd.CommandText = $oCmd.CommandText + " AND description = "
$oCmd.CommandText = $oCmd.CommandText + '"A'me Down"'
$oCmd.CommandText = $oCmd.CommandText + " ORDER BY Name"


Hi Witto tried that but it did not work. I am sure its to do with the apostrophe in the SQL query. \:\(

I did find this vbscript which when using apostrophe in SQL it does not like it. so someone created a vbscript to fix this.

lsLastName = Replace(lsLastName, "'", "''")

can this be converted into kix?

Thanks

Glyn


Edited by 5861king (2009-09-09 05:14 PM)

Top
#195816 - 2009-09-09 05:01 PM Re: AD Change users [Re: 5861king]
Allen Administrator Offline
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4545
Loc: USA
I think your have to escape the single quote with another... try this.

 Code:
$oCmd.CommandText = $oCmd.CommandText + "A''me Down"
or
$oCmd.CommandText = $oCmd.CommandText + "A'" + "'me Down"


Edited by Allen (2009-09-09 05:02 PM)

Top
#195817 - 2009-09-09 05:36 PM Re: AD Change users [Re: Allen]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Nope still no go \:\) any other ideas
Top
#195818 - 2009-09-09 05:49 PM Re: AD Change users [Re: 5861king]
Witto Offline
MM club member
*****

Registered: 2004-09-29
Posts: 1828
Loc: Belgium
 Code:
$oCmd.CommandText = $oCmd.CommandText + '"A ' + "'" + 'me Down "'

Top
#195834 - 2009-09-09 09:28 PM Re: AD Change users [Re: Witto]
5861king Offline
Fresh Scripter

Registered: 2006-09-28
Posts: 48
Loc: UK
Thanks witto for helping out & Allen, but that did not work either, its bugging the hell out of mw \:\( all other names without like john doe work fine!!!
Top
#195838 - 2009-09-10 08:24 AM Re: AD Change users [Re: 5861king]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
 Code:
$oCmd.CommandText = $oCmd.CommandText + "A"+Chr(39)+"me Down"

Top
#195840 - 2009-09-10 09:15 AM Re: AD Change users [Re: Arend_]
Richard H. Administrator Offline
Administrator
*****

Registered: 2000-01-24
Posts: 4946
Loc: Leatherhead, Surrey, UK
Carshalton, eh? I did a course or two at the college.

LDAP generally uses the Unix "\" convention for escaping characters (you'll see this often with "," in names), so try this:

 Code:
$oCmd.CommandText = $oCmd.CommandText + '"A ' + "\'" + 'me Down "'



Edited by Richard H. (2009-09-10 09:16 AM)

Top
Page 1 of 1 1


Moderator:  Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart 
Hop to:
Shout Box

Who's Online
0 registered and 248 anonymous users online.
Newest Members
gespanntleuchten, DaveatAdvanced, Paulo_Alves, UsTaaa, xxJJxx
17864 Registered Users

Generated in 0.105 seconds in which 0.067 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org