Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I feed a script with a .csv file

Status
Not open for further replies.

dtweten

MIS
Apr 28, 2010
18
US
Hi I was curious if anyone could help me figure out how to have my script below read a single column .csv file and use each rows content as the distinguished name in the script (there are two places it needs to be). So that the script would read the first line in the .csv file and use its contents as the distinguished name then run the script and then loop back up and read the next row and run its through the script so on and so forth until there was no more names.



On Error Resume Next

Const ADS_PROPERTY_DELETE = 4
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D

Set objUser = GetObject _
("LDAP://CN=Doe\, John,OU=Users OU,DC=domain,DC=com")
arrMemberOf = objUser.GetEx("memberOf")

If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
WScript.Echo "This account is not a member of any security groups."
WScript.Quit
End If

For Each Group in arrMemberOf
Set objGroup = GetObject("LDAP://" & Group)
objGroup.PutEx ADS_PROPERTY_DELETE, _
"member", Array("CN=Doe\, John,OU=Users OU,DC=domain,DC=com")
objGroup.SetInfo
Next
 
[0] I take it that the alluded csv means a text file having each line a distinguished name to work upon. The comma separator is understood to be the construction of the dn's.

[1] You can do this. As once the data are in a file, anything can happen, so quite a bit of error checking should be used. (Edited and written on-line, watch typos etc.)
[tt]
Const ADS_PROPERTY_DELETE = 4
Const E_ADS_PROPERTY_NOT_FOUND = &h8000500D

'[green]your givens
dim sfile, fso, f, sdn
sfile="c:\pqr\xyz.txt"[/green]

set fso=createobject("scripting.filesystemobject")
if not fso.fileexists(sfile) then
wscript.echo "data file not found."
set fso=nothing
wscript.quit 99
end if

set f=fso.opentextfile(sfile,1,false,0) 'make sure it is ascii, else have to modify the last param to -1

do while not f.atendofstream
sdn=trim(f.readline)
if sdn<>"" then
on error resume next
Set objUser = GetObject("LDAP://" & sdn)
if err.number<>0 then
wscript.echo "cannot find user " & sdn
err.clear
else
arrMemberOf = objUser.GetEx("memberOf")
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
WScript.Echo "This account is not a member of any security groups."
err.clear
elseif err.number<>0 then
wscript.echo "Unidentified error when applying getex(""memberOf"") to " & sdn & ":" & vbcrlf & vbtab & err.description
err.clear
else
For Each Group in arrMemberOf
'assume integrity of the directory
Set objGroup = GetObject("LDAP://" & Group)
objGroup.PutEx ADS_PROPERTY_DELETE, _
"member", Array(sdn)
objGroup.SetInfo
set objGroup=nothing
Next
End If
on error goto 0
end if
set objUser=nothing
end if
loop
f.close
set f=nothing
set fso=nothing
[/tt]
 
That works for the most part but it keeps saying that it can not find the users.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top