locked
build Ad object's proxy address with powershell RRS feed

  • Question

  • Hello

    I am trying to create users from powershell and one of the steps that I have to do is add the proxy address that exists in ADSI.

    I need in the proxy address 3 fields(usually, sometimes more): smtp, SMTP and SIP

    Right now I am attempting like this:

    $SIP="SIP:$Alldatafrom.mail" ,  to get SIP:name.lastname@domain.com
    $SMTP="SMTP:$Alldatafrom.mail", to get SMTP:name.lastname@domain.com

    try
    {
        IF([string]::IsNullOrEmpty($SMTP)) {            
            Write-output "SIP  is NULL or EMPTY"            
        } else {            
                Write-output "$SmTP    : " $SMTP       
            Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SMTP}   
        }
    }
     catch {
                    $ErrorMessage = $_.Exception.Message
                    $FailedItem = $_.Exception.ItemName
                    Write-output  $ErrorMessage
                    Write-output  $FailedItem
              }

    than I do this:

    try
    {
        IF([string]::IsNullOrEmpty($SIP)) {            
            Write-output "SIP  is NULL or EMPTY"            
        } else {            
                Write-output "$SIP    : " $SIP       
            Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SIP}   
        }
    }
     catch {
                    $ErrorMessage = $_.Exception.Message
                    $FailedItem = $_.Exception.ItemName
                    Write-output  $ErrorMessage
                    Write-output  $FailedItem
              }

    But the thing is that it overwrites my values, so I only get one if the at the end.

    Can someone advise?

    Thursday, September 21, 2017 11:02 AM

Answers

  • Inspect these two lines.  They will not produce a correct output:

    $SIP="SIP:$Alldatafrom.mail" 
    $SMTP="SMTP:$Alldatafrom.mail"

    Using a colon will cause issues.

    $SIP='SIP:{0}' -f $Alldatafrom.mail" 
    $SMTP='SMTP:{0}'  -f  $Alldatafrom.mail

    or

    $SIP="SIP:$($Alldatafrom.mail)" 
    $SMTP="SMTP:$($Alldatafrom.mail)"


    \_(ツ)_/

    Thursday, September 21, 2017 1:41 PM

All replies

  • Use a lower case 'smtp' to add a secondary address. Upper Case is for primary addresses

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, September 21, 2017 11:17 AM
  • Hello seanmcavinue

    Ok, I understand. But the issue is that values are overwritten, they do not remain all, only the last one

    Thursday, September 21, 2017 11:32 AM
  • Where do you pull the values from? Do you have a loop for go through each of them?

    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, September 21, 2017 12:11 PM
  • ProxyAddresses is an array and must be added or changed as an array. Using individual addresses always replaces the whole array with a single address.

    Look in Gallery for scripts that manage this array correctly.


    \_(ツ)_/

    This Syntax is correct and works fine for me:

    "Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SMTP}"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.

    Thursday, September 21, 2017 1:24 PM
  • ProxyAddresses is an array and must be added or changed as an array. Using individual addresses always replaces the whole array with a single address.

    Look in Gallery for scripts that manage this array correctly.


    \_(ツ)_/

    This Syntax is correct and works fine for me:

    "Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SMTP}"


    If you find that my post has answered your question, please mark it as the answer. If you find my post to be helpful in anyway, please click vote as helpful.


    Yes.  You are correct.  I was thinking of the ADSI methods. 

    \_(ツ)_/

    Thursday, September 21, 2017 1:33 PM
  • Yes, but if you add 3 values it only keeps the last ..     

      Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SMTP}   
            Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$smtp2}   

            Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SIP}   

    Let me review the code again, maybe I omitted something

    Thursday, September 21, 2017 1:41 PM
  • Inspect these two lines.  They will not produce a correct output:

    $SIP="SIP:$Alldatafrom.mail" 
    $SMTP="SMTP:$Alldatafrom.mail"

    Using a colon will cause issues.

    $SIP='SIP:{0}' -f $Alldatafrom.mail" 
    $SMTP='SMTP:{0}'  -f  $Alldatafrom.mail

    or

    $SIP="SIP:$($Alldatafrom.mail)" 
    $SMTP="SMTP:$($Alldatafrom.mail)"


    \_(ツ)_/

    Thursday, September 21, 2017 1:41 PM
  • I'll try to find a script that uses matrix and see

    Those don't work or I am doing something wrong

    try
    {
         [string[]]$newproxy = @()
         $newproxy +=$sip 
         $newproxy +=$SMTP
        #$SIP="sip:$($Alldatafrom.mail)" 
        #$SMTP="SMTP:$($Alldatafrom.mail)"
        IF([string]::IsNullOrEmpty($SIP)) {            
            Write-output "SIP  is NULL or EMPTY"            
        } else {            
                Write-output "SIP    : " $SIP       
                Write-output "SMTP    : " $SMTP       
            #Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SIP}   
            #Set-ADUser -Identity  $sam  -Add @{Proxyaddresses=$SMTP}   
                 Set-ADUser -identity $sam -Replace @{ProxyAddresses=$NewProxyAddresses}

        }
    }
     catch {
                    $ErrorMessage = $_.Exception.Message
                   $FailedItem = $_.Exception.ItemName
                   Write-output  $ErrorMessage
                   Write-output  $FailedItem
            }


    I get 
    Cannot validate argument on parameter 'Replace'. The argument is null or an element of the argument collection contains a null value.
    
    


    Friday, September 22, 2017 7:50 AM
  • Hi,

    Based on my research, I’d like to explain that -replace parameter need to specify both the original value and new value.
    You could try to modify your script like:
    Set-ADUser -identity $sam -Replace @{ProxyAddresses=$OldProxyAddresses, $NewProxyAddresses}

    For more information about -replace parameter, please refer to the following article:
    Set-ADUser
    https://technet.microsoft.com/en-us/library/ee617215.aspx

    If you need further help, please feel free to let us know.
     
    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Wednesday, September 27, 2017 9:43 AM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 2, 2017 7:45 AM
  • Hello Albert

    I made some progress, but I still have some issues to fix.  I corercted the sintax according to your example, but now I am getting a type mismatc error, as the initial proxyaddres contains only one parameter, and I need to replace it with one that has two: 

    Cannot validate argument on parameter 'Replace'. All values in the argument collection should be of the same type.

    This is my code now, I will try to see if I can fix it it this week, though I am very busy.

    I dod not forget to retrun here and reply

    try
    {
         $emailAddr = Get-ADUser -Identity $sam -Properties * | Select Emailaddress
         $OldProxyAddresses = Get-ADUser -Identity $sam -Properties * | Select ProxyAddresses
         $sip = "sip:"+$emailAddr.Emailaddress
         $SMTP = "SMTP:"+$emailAddr.Emailaddress
         [string[]]$newproxy = @()
         $newproxy +=$sip 
         $newproxy +=$SMTP

        IF([string]::IsNullOrEmpty($SIP)) {            
            Write-output "SIP  is NULL or EMPTY"            
        } else {            
                Write-output "SIP    : " $SIP       
                Write-output "SMTP    : " $SMTP       
                 Set-ADUser -identity $sam -Replace @{ProxyAddresses=$OldProxyAddresses, $newproxy}

        }
    }
     catch {
                   $ErrorMessage = $_.Exception.Message
                   $FailedItem = $_.Exception.ItemName
                   Write-output  $ErrorMessage
                   Write-output  $FailedItem
            }



    Monday, October 2, 2017 10:27 AM
  • Hi,

    Thanks for sharing your current progress.

    Based on your situation, I will give you the following recommendations. Hope it is helpful to you:
    1. You could have a try to first remove the values of ProxyAddresses using -Clear parameter since you want to replace the initial value, then you could use -Add parameter to add the new ProxyAddresses, the following scripts for your reference:
    Set-ADUser -Identity $sam -Clear ProxyAddresses
    Set-ADUser -Identity $sam -Add @{ProxyAddresses=$NewProxyAddresses}

    2. You could also try to combine with the -Add parameter and -Remove parameter to meet your needs:
    Set-ADUser -Identity $sam -Add @{ProxyAddresses=$NewProxyAddresses} -Remove @{ProxyAddresses=$OldProxyAddresses}

    If you need further help, please feel free to let us know.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, October 3, 2017 9:59 AM
  • Hello Albert

    Point 1 did not work. It gives "

    The specified directory service attribute or value does not exist
    Parameter name: @{ProxyAddresses=Microsoft.ActiveDirectory.Management.ADPropertyValueCollection}"

    I have to go away until Monday, but I will test nr 2 and let you know.

    Also, I will try to see if I can overcome that error.

    Thank you

    Wednesday, October 4, 2017 11:53 AM
  • Hi,

    Just want to confirm the current situations.

    Based on your situation, I recommend you could have a try to set one ADUser for testing to see if the issue remains. The following script for your reference:
    $sam = 
    $emailAddr = Get-ADUser -Identity $sam -Properties * | Select Emailaddress
    $sip = "sip:"+$emailAddr.Emailaddress
    $SMTP = "SMTP:"+$emailAddr.Emailaddress
    [string[]]$newproxy = @()
    $newproxy +=$sip 
    $newproxy +=$SMTP
    Set-ADUser -Identity $sam -Add @{ProxyAddresses=$newproxy}

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Monday, October 9, 2017 9:55 AM
  • Hi,

    Just checking in to see if the information provided was helpful.

    Please let us know if you would like further assistance.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Thursday, October 12, 2017 6:21 AM
  • Hello Albert

    I did not have time to retry, but when I will I am going to post an update.

    It may take some time, as I am really busy on several projects, but I promise to return and reply no matter what the result is

    Monday, October 16, 2017 1:09 PM
  • Hi,

    I am checking how the issue is going, if you still have any questions, please feel free to contact us.

    If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.
    If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.
    If no, please reply and tell us the current situation in order to provide further help.

    Appreciate for your feedback.

    Best Regards,
    Albert Ling

    Please remember to mark the replies as an answers if they help and unmark them if they provide no help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.

    Tuesday, October 17, 2017 7:36 AM