locked
So I wrote a script... RRS feed

  • Question

  • I am still practicing getting the logic down, but I wrote the below script to search X users for an primary SMTP in the proxyaddresses field. If said "SMTP:" is missing pull the email address from the Mail property and make that the primary. It seems to work, there are no errors, and I spot checked some that didn't have an "SMTP:" and they do know, but some didn't work, even if I add an email address in the mail property. Also some have outputs that are just numbers. Some of them I know have null proxyaddress values, but the first write-host isn't firing. Any ideas? Thanks for your help in advance. 

    $Users= get-aduser -filter * -properties * -searchbase "somewhere in my domain"

      Foreach($User in $Users)
                {    
                    If($User.proxyAddresses -eq $null)
                        {
                           Write-Host "$($User.Name) does not have an email address on file"  
                        }
                    If($User.proxyaddresses -notmatch 'SMTP:')
                            {
                                if($User.mail -ne $null)
                                    {   
                                        $User.proxyaddresses.add("SMTP:$($User.mail)")
                                        Write-host "$($User)'s Email added to SMTP"
                                    }
                                Else 
                                    {
                                        Write-host "$($User.Name) doesn't have an email address in the Email section"
                                    }
                            }
                    Else
                        {
                            Write-Host "$($User.Name) has a primary SMTP!"  
                        }
                }

    RESULTS:

    8
    Joe Blow Email added to SMTP
    Zulfi Bhati has a primary SMTP!
    Sud Soni has a primary SMTP!
    5
    Mike White's Email added to SMTP
    Satya Mahan has a primary SMTP!
    Jas Gill has a primary SMTP!
    Kaya Malhota has a primary SMTP!
    Kin Tank has a primary SMTP!


    Tuesday, October 11, 2016 11:46 PM

Answers

  • You want to search for addresses that begin with "SMTP:", but not "smtp:". The case sensitive operator is -cNotMatch.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by RedBeardOne Wednesday, October 12, 2016 4:19 PM
    Wednesday, October 12, 2016 12:00 AM
  • The following returns the array index.

     $User.proxyaddresses.add("SMTP:$($User.mail)")

    To prevent it do this:

    [void]$User.proxyaddresses.add("SMTP:$($User.mail)")


    \_(ツ)_/

    • Marked as answer by RedBeardOne Wednesday, October 12, 2016 4:20 PM
    Wednesday, October 12, 2016 12:31 AM

All replies

  • You want to search for addresses that begin with "SMTP:", but not "smtp:". The case sensitive operator is -cNotMatch.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    • Marked as answer by RedBeardOne Wednesday, October 12, 2016 4:19 PM
    Wednesday, October 12, 2016 12:00 AM
  • You must have one and only one SMTP.  You can have as many smtp as you want.  Be careful as errors can make all mail addresses fail.

    \_(ツ)_/

    Wednesday, October 12, 2016 12:02 AM
  • Thanks, I didn't know that. Would you happen to know why it is spitting out numbers? 
    Wednesday, October 12, 2016 12:05 AM
  • The following returns the array index.

     $User.proxyaddresses.add("SMTP:$($User.mail)")

    To prevent it do this:

    [void]$User.proxyaddresses.add("SMTP:$($User.mail)")


    \_(ツ)_/

    • Marked as answer by RedBeardOne Wednesday, October 12, 2016 4:20 PM
    Wednesday, October 12, 2016 12:31 AM
  • Ok, all very good suggestions. Thanks for everyone's help. One more question. Could you explain why adding the [void] stopped that from happening? I added [void] in front of the other $User.proxyaddresses filters and it seemed to behave as I expected. Actually started seeing that they didn't have an email address in the mail section.   
    Wednesday, October 12, 2016 4:19 PM
  • [void] specifies that the method (in the case the Add method) does not return a value. See this link about [void] in C#, which PowerShell is based on:

    https://msdn.microsoft.com/en-us/library/yah0tteb.aspx


    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Wednesday, October 12, 2016 5:14 PM
  • In PowerShell, putting a .NET type in square brackets before a return value "casts" the return value as the specified type. For example:


    PS C:\> $n = 3
    PS C:\> $n.GetType().Name
    Int32
    PS C:\> $s = [String] $n
    PS C:\> $s.GetType().Name
    String
    

    In this case, [Void] means "cast the return value as void" (i.e., throw away the return value and do not output it). Another way to accomplish this is to pipe to Out-Null; e.g.:


    $User.proxyAddresses.Add("SMTP:$($User.mail)") | Out-Null
    


    -- Bill Stewart [Bill_Stewart]

    Wednesday, October 12, 2016 7:24 PM