update ProxyAddresses from CSV

Hi,

where have the error?

$ou = "OU=Users,DC=domain,DC=local"

Import-Csv C:\smtp.csv |Foreach {
       Get-ADUser -SearchBase $ou -SearchScope SubTree -Filter "userPrincipalName -like '$($_.userPrincipalName)'" | 
                Set-ADUser -Add @{proxyAddresses = ($_.ProxyAddresses -split ";")}


Error:

Set-ADUser : add
At C:\ProxyAddresses.ps1:4 char:17
+                 Set-ADUser -Add @{proxyAddresses = ($_.ProxyAddresses -split ";" ...
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (CN=User,DC=domain,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

C:\smtp.csv
userPrincipalName,ProxyAddresses
user@domain.com,smtp:user01@domain.com;smtp:user01@domain.onmicrosoft.com;

Thanks

July 21st, 2015 2:33pm

$ou='OU=Users,DC=domain,DC=local'

Import-Csv C:\smtp.csv |
    Foreach {
        [array]$proxyAddresses=$_.ProxyAddresses -split ";"
        if($proxyAddress[0] -notmatch '^smtp:'){"BAD ADDRESS"}
        Get-ADUser -SearchBase $ou -Filter "userPrincipalName -eq '$($_.userPrincipalName)'" | 
                 Set-ADUser -Add @{proxyAddresses=$proxyAddreses[0]}
 }		
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:22pm

Hi JRV. Thanks

the script gives me an error

BAD ADDRESS
Cannot index into a null array.
At C:\tmp\AD\ProxyAddresses.ps1:6 char:70
+         Get-ADUser -SearchBase $ou -Filter "userPrincipalName -eq '$($_.userPrin ...
+                                                                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


Currently, all proxyaddress are empty 
July 21st, 2015 3:29pm

the if statement says $proxyAddress[0] rather than $proxyAddresses[0]. Also, at the end it will only add the first proxy address, remove the [0], in order to add the array of addresses

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:37pm

Just do this to test your file.

Import-Csv C:\smtp.csv |
    Foreach {
        Write-Host "UPN->>$($_.UserPrincipalName)" -fore green
        Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName)'" |
    }


July 21st, 2015 3:39pm

the if statement says $proxyAddress[0] rather than $proxyAddresses[0]. Also, at the end it will only add the first proxy address, remove the [0], in order to add the array of add

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:41pm

Hi

the result :(

Foreach {

        [array]$proxyAddresses=$_.ProxyAddresses -split ";"
        if($proxyAddress -notmatch '^smtp:'){"BAD ADDRESS"}
        Get-ADUser -SearchBase $ou -Filter "userPrincipalName -eq '$($_.userPrincipalName)'" | 
                 Set-ADUser -Add @{proxyAddresses=$proxyAddreses}

Error:
Set-ADUser : Cannot validate argument on parameter 'Add'. The argument is null or an element of the argument collection contains a null value.

July 21st, 2015 3:44pm

Check your spelling of variables, the error messages indicate the issue
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:45pm

Check your spelling of variables, the error messages indicate
July 21st, 2015 3:52pm

For this?  $proxyAddreses

I change for :  Set-ADUser -Add @{proxyAddresses=$proxyAddresses}

BAD ADDRESS
Set-ADUser : add
At C:\tmp\AD\ProxyAddresses.ps1:8 char:18
+                  Set-ADUser -Add @{proxyAddresses=$proxyAddresses}
+                  ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (CN=User, DC=domain,DC=local:ADUser) [Set-ADUser], ADInvalidOperationException
    + FullyQualifiedErrorId : ActiveDirectoryServer:0,Microsoft.ActiveDirectory.Management.Commands.SetADUser

What I don't understand is the split command. What with "," works with ";" not?

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 3:53pm

Clayman is confusing the issue here.

Please resolve the following:

The names are not there because the field is empty and it will throw an error.  You cannot run this with an empty field. Either use data or filter out the null entries.

July 21st, 2015 3:54pm

But all fields are null. I don't need to check them, just write the field
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:05pm

But all fields are null. I don't need to check them, just write the field

Why are all of the fields in your CSV null?  What is it that you are trying to do.  YOu cannot use a CSV with all empty fields.

July 21st, 2015 4:09pm

No, sorry, explain me evil. There is no ninfun datoi null the CSV. I mean that the ProxyAddress are empty of allusers
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:12pm

No, sorry, explain me evil. There is no ninfun datoi null the CSV. I mean that the ProxyAddress are empty of allusers

What the hell is that supposed to mean?  It is not English.  It is not Spanish.  It is just nothing.

What is in the CSV.  We have not ever been referring to anything but the contents of the CSV.

Post the first two or three lines of the CSV.

July 21st, 2015 4:14pm

CSV

userPrincipalName,ProxyAddresses
user01@domain.com,smtp:user01@domain.com;smtp:user01@domain.onmicrosoft.com;
user02@domain.com,X500:user02@domain.com;smtp:user02@domain.onmicrosoft.com;

The original script isbut what I don't understand is why the ';' does not work.

$ou = "OU=Users,DC=domain,DC=local"

Import-Csv C:\smtp.csv |Foreach {
       Get-ADUser -SearchBase $ou -SearchScope SubTree -Filter "userPrincipalName -like '$($_.userPrincipalName)'" | 
                Set-ADUser -Add @{proxyAddresses = ($_.ProxyAddresses -split ";")}

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:19pm

The original script isbut what I don't understand is why the ';' does not work.

Because the field is bad.  It ends in a semicolon which creates an array with a blank entry.

I was trying to get you to see that and then show you how to fix it. You have to use a different split method.

$_.UserPrincilpeNames.Split(';',[System.StringSplitOptions]::RemoveEmptyEntries)

July 21st, 2015 4:25pm

Gee!

you have reason. Not that I have posted in that. Many thanks for your patience

Set-ADUser -Add @{ProxyAddresses = ($_.proxyaddresses.Split(';',[System.StringSplitOptions]::RemoveEmptyEntries))} 

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:35pm

You do not need to use the search base.  UPN is indexed and must be unique.

Get-ADUser -Filter "userPrincipalName  -eq '$($_.userPrincipalName)'"

All that other decoration will likely slow dowsn the process and make it more prone to error.

Using "-like" with no wildcards serves no purpose over "-eq".  We have a lot of superstitious people here who believe it adds some special thing to the search.  It doesn't.

July 21st, 2015 4:39pm

One last thing. If the CSV tnego end;, and exclude it will not start the PorxyAddresswith a space?

userPrincipalName,ProxyAddresses
user01@domain.com,smtp:user01@domain.com;smtp:user01@domain.onmicrosoft.com; ;
user02@domain.com,X500:user02@domain.com;smtp:user02@domain.onmicrosoft.com; ;

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:42pm

One last thing. If the CSV tnego end;, and exclude it will not start the PorxyAddresswith a space?

What?  Doesn't make sense.
July 21st, 2015 4:45pm

If the CSV which have provided me a last null value, as I exclude it?

user01@domain.com,smtp:user01@domain.com;smtp:user01@domain.onmicrosoft.com; ;
user02@domain.com,X500:user02@domain.com;smtp:user02@domain.onmicrosoft.com; ;

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 4:47pm

The Split() method of string removes ALL null entries.

July 21st, 2015 5:04pm

Thanks!!!

I'm going to try it

Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 5:15pm

Hi JRV. Thanks

the script gives me an error

BAD ADDRESS
Cannot index into a null array.
At C:\tmp\AD\ProxyAddresses.ps1:6 char:70
+         Get-ADUser -SearchBase $ou -Filter "userPrincipalName -eq '$($_.userPrin ...
+                                                                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


Currently, all proxyaddress are empty 
July 21st, 2015 7:26pm

Hi JRV. Thanks

the script gives me an error

BAD ADDRESS
Cannot index into a null array.
At C:\tmp\AD\ProxyAddresses.ps1:6 char:70
+         Get-ADUser -SearchBase $ou -Filter "userPrincipalName -eq '$($_.userPrin ...
+                                                                      ~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (:) [], RuntimeException
    + FullyQualifiedErrorId : NullArray


Currently, all proxyaddress are empty 
Free Windows Admin Tool Kit Click here and download it now
July 21st, 2015 7:26pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics