.NET Framework - Updating AD proxyAddresses

Asked By ShawnDuma
13-Sep-07 08:12 PM
param ([array]$users=$(throw "Need Users..."))
foreach ($user in $users)
{
$u = (ldap
if ($u.proxyAddresses)
{
$pa = $u.proxyAddresses | ?{$_ -match 'bad-address'}
$pa = '@("' + ([string]::join('","', $pa)) + '")'
$u.PutEx(2, 'proxyAddresses', (invoke-expression "$pa"))
$u.SetInfo()
}
}

This works but why on earth do I have to get so jiggy with the array before
PutEx will take it?
GetDirectoryEntry
(1)
ADrectoryEntry
(1)
GetDi
(1)
PutEx
(1)
ProxyAddresses
(1)
ObjectCategory
(1)
ObjectClass
(1)
Dumas
(1)
  Oisin Grehan replied...
13-Sep-07 08:20 PM
On Sep 13, 8:12 pm, Shawn Dumas <ShawnDu...@>
re

I remember going through similar hoops trying to get arrays as
parameters passed correctly; a bit painful. I still muck it up now and
again. Powershell has a habit of unrolling arrays before passing them
anywhere, so try using the array constructor:

$pa =3D $u.proxyAddresses | ?{$_ -match 'bad-address'}
$u.PutEx(2, 'proxyAddresses', (,$pa))

I'm not able to test this, so it might not work outright, but
Hopefully it'll give you some ideas. So, why do this? By putting the
array constructor in front of it, I'm making an array with 1 element,
which is your $pa array. Powershell will unroll it, leaving the inner
array intact (hopefully!)

Hope this helps

- Oisin
  ShawnDuma replied...
13-Sep-07 08:28 PM
I will give that a shot, thanks!
  Brandon Shell replied...
13-Sep-07 08:26 PM
I would assume it because that is a multi value attribute and with
ADS_PROPERTY_UPDATE (aka 2) it expects an arrayish value.

Are you trying to replace all the values or just one particular one?
  Brandon Shell replied...
13-Sep-07 08:32 PM
Try adding [array] in front of $pa
[array]$pa = $u.proxyAddresses | ?{$_ -match 'bad-address'}
$pa += New-Value
$u.PutEx(2, 'proxyAddresses', $pa)
$u.setinfo()
  ShawnDuma replied...
13-Sep-07 08:40 PM
$u.PutEx(2, 'proxyAddresses', (,"$pa"))

All I had to do was add quotes to what you posted and then, pow; it worked!
Thanks again.
  ShawnDuma replied...
13-Sep-07 08:44 PM
I am replacing.
Create New Account
help
Object For Each src As SearchResult In sr rw = dsData.Tables("managers").NewRow If src.GetDirectoryEntry().Properties("cn").Value IsNot Nothing Then cn = src.GetDirectoryEntry.Properties("cn").Value rw("fullname") = cn(1).ToString End If If src.GetDirectoryEntry().Properties("mail").Value IsNot Nothing Then rw("email") = src GetDirectoryEntry().Properties("mail").Value.ToString End If dsData.Tables("managers").Rows.Add(rw) Next Catch ex 21 names. How can I speed this up? rg, Eric VB.NET Discussions PropertyValueCollection (1) GetDirectoryEntry (1) DirectorySearcher (1) SearchResult (1) Tables (1) NewRow (1) Properties mail (1) Properties cn (1) If you are only reading values you do not need all those calls to GetDirectoryEntry, it is quite a heavy operation and exactly why it takes so long. You only need to call GetDirectoryEntry if you are making changes to the object in AD. For Each src As SearchResult
dn $Searcher = New-Object DirectoryServices.DirectorySearcher cat C: \ users.txt | % { $Searcher.Filter = $usr = $Searcher.FindOne().GetDirectoryEntry() $usr.distinguishedname # do aome other stuff based on user dn } The first iteration returns the on a null-valued expression. At C: \ as400.ps1:62 char:46 + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< ) - - $hay http: / / scriptolog.blogspot.com Powershell Discussions GetDirectoryEntry (1) FindOne (1) SamAccountName (1) ObjectCategory (1) DirectoryServices (1) DirectorySearcher (1) ObjectClass (1) PowerShell (1 psuguk.org.uk trying manually : $Searcher = New-Object DirectoryServices.DirectorySearcher $Searcher.Filter = $usr = $Searcher.FindOne().GetDirectoryEntry() $usr.distinguishedname $Searcher.Filter = $usr = $Searcher.FindOne().GetDirectoryEntry() $usr.distinguishedname output: CN = user1 name, OU = Users, DC = somedomainname You cannot call a method on a null-valued expression. At C: \ as400.ps1:62 char:45 + $usr = $Searcher.FindOne().GetDirectoryEntry( <<<< ) CN = user1 name, OU = Users, DC = somedomainname this always returns (in addition to the error
Powershell Discussions System.DirectoryServices.DirectoryEntry (1) DirectoryServices.DirectorySearcher (1) ADSISearcher (1) SAMAccountName (1) MyADUser (1) GetDirectoryEntry (1) DirectorySearcher (1) DirectoryEntry (1) Hello Swamy, $username = "swamy" $searcher = new-object DirectoryServices.DirectorySearcher([ADSI user)" $ds = new-object system.directoryservices.directorysearcher([adsi]"", $filter) $ds.pagesize = 1000 $ds.findall() | %{$_.GetDirectoryEntry()} } Karl. . . just remember that findall() doesnt return DirectoryEntry and it acts different. To get a DirectoryEntry you need to call the GetDirectoryEntry() method on the returned object. p.s. dont use objectclass ;) You are right There is no findall() and getdirectoryentry() method may be with V2 it is available. It seems using quest's get-qaduser Object System.DirectoryServices.DirectoryEntry("LDAP: / / USERDN") To create a Searcher that WILL have findall() and getdirectoryentry(). $de = [ADSI]"LDAP: / / <Root of Search> " # DirectoryEntry for the root of the search MUST HAVE