#195801 - 2009-09-08 06:39 PM
AD Change users
|
5861king
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.
;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
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
KiX Supporter
Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
|
This looks like a script generated with ASE (Admin Script Editor).
Thanks,
Kent
|
Top
|
|
|
|
#195809 - 2009-09-09 12:53 PM
Re: AD Change users
[Re: Kdyer]
|
5861king
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.
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
|
|
|
|
#195811 - 2009-09-09 02:20 PM
Re: AD Change users
[Re: Witto]
|
Witto
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:
$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
Fresh Scripter
Registered: 2006-09-28
Posts: 48
Loc: UK
|
I think you were talking about this? I don't know if this will work:
$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
KiX Supporter
Registered: 2003-04-19
Posts: 4545
Loc: USA
|
I think your have to escape the single quote with another... try this.
$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
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
MM club member
Registered: 2004-09-29
Posts: 1828
Loc: Belgium
|
$oCmd.CommandText = $oCmd.CommandText + '"A ' + "'" + 'me Down "'
|
Top
|
|
|
|
#195834 - 2009-09-09 09:28 PM
Re: AD Change users
[Re: Witto]
|
5861king
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_
MM club member
Registered: 2005-01-17
Posts: 1894
Loc: Hilversum, The Netherlands
|
$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
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:
$oCmd.CommandText = $oCmd.CommandText + '"A ' + "\'" + 'me Down "'
Edited by Richard H. (2009-09-10 09:16 AM)
|
Top
|
|
|
|
Moderator: Jochen, Allen, Radimus, Glenn Barnas, ShaneEP, Ruud van Velsen, Arend_, Mart
|
0 registered
and 248 anonymous users online.
|
|
|