Thanks again for the replies! Halr9000, your suggestion that the ldapquery was returning an array for the mail attribute was correct and very helpful. I've got the script setup in a very basic way that works if I pass an argument of username when I run the script but I'd like to run it against an OU where a number of mailbox-enabled users exist so that we can change the default Exchange smtp address for all the users in the OU. (Exchange Email Address Policy is not an option for us.) So I'm trying to come up with a for-each loop to do this but as I've stated before, my programming skills are close to null.
Any suggestions? The scripts are below:
This works for a single user when the username is passed as an argument when the script is run:
$user = $args[ 0 ]
$ldapquery = get-ldap -server "ldap.contoso.com" -dn "ou=people,dc=contoso,dc=com" -search "employeeNumber=$user" -attr
$ldapquery.mail[ 0 ]
Set-Mailbox -Identity $user -PrimarySmtpAddress $ldapquery.mail[ 0 ]
This is my attempt at a for-each loop (which does not work):
foreach ($x in Get-Mailbox -OrganizationalUnit "ou=employees,dc=ad,dc=contoso,dc=com") {
$ldapquery = get-ldap -server "ldap.contoso.com" -dn "ou=people,dc=contoso,dc=com" -search "employeeNumber=$x.Name" -attr
$ldapquery.mail[ 0 ]
Set-Mailbox -Identity $user -PrimarySmtpAddress $ldapquery.mail[ 0 ]
}
The error returned for each user object in the OU is Cannot index into a null array. + $ldapquery.mail[ 0 <<<< ]
(Meaning if there are four user objects in the OU, I get four of the errors.) I'm still reading and learning with Powershell so I'm trying loads of things trying to figure out for-each loops....
Thanks a lot!
Jason