'get primary SMTP from Proxyaddresses powershell

I am working on the script below, The posted script works; unfortunately I need to pull the address from proxyaddresses PrimarySMTP (pulling it from email field in script). I have tried this process like crazy and cant get it to pull and output the info. Hoping someone could point me in the right direction.

$CountA = 1

$CountB = 0

 

Function PasswordExpiration

{

        While ($CountB -ne 5)

    {

        $CountA = $CountA + 1

        $CountB = $CountB + 1

        $FinalPath = "c:\support\PasswordExpiration\PasswordExpiration"+$CountB+"Days.csv"

        $FilterEndDate = (Get-Date).AddDays($CountA).Date

        $FilterStartDate = (Get-Date).AddDays($CountB).Date

        Get-ADUser -Filter * -Properties "DisplayName", "msDS-UserPasswordExpiryTimeComputed" , 
        EmailAddress, DisplayName, PasswordNeverExpires | 
        where EmailAddress -ne $null |
        where { $_.passwordNeverExpires -eq $false } | 
        where {$_.enabled -eq $true}|

            Select-Object -Property "Displayname", GivenName , EmailAddress,@{Name="Expiration Date";Expression={[datetime]::FromFileTime($_."msDS-UserPasswordExpiryTimeComputed")}}, PasswordNeverExpires, enabled |

            Where-Object { $_.'Expiration Date' -lt $FilterEndDate} |

            Where-Object { $_.'Expiration Date' -gt $FilterStartDate} |

            Export-Csv -Path $FinalPath -Encoding ascii -NoTypeInformation

    }

}



Solution 1:[1]

Obviously I cannot test this but there are a few things I noticed, so I cleaned it up a bit below:

  1. EmailAddress is not an AD property you are looking for "mail"
  2. mail sometimes differs from the primary SMTP address on the ProxyAddesses property. That property returns as a string array. To isolate the primary you need to look for the one starting with capitol "SMTP:". Below I used the string .StartsWith() method to isolate it. The method is case sensitive so it should work.

I also removed a lot of piping to Where-Object because you can use the -and operator.

If you have no reason to believe the mail property will stray from the primary address on the ProxyAddresses property just use it like:

Function PasswordExpiration
{
    $ExpirationDate = @{Name = "Expiration Date"; Expression = { [DateTime]::FromFileTime( $_."msDS-UserPasswordExpiryTimeComputed" ) } }
    While ($CountB -ne 5)
    {
        $CountA = $CountA + 1
        $CountB = $CountB + 1

        $FinalPath  = "c:\support\PasswordExpiration\PasswordExpiration"+$CountB+"Days.csv"
        $Properties = "DisplayName", "msDS-UserPasswordExpiryTimeComputed", "mail","ProxyAddresses", "PasswordNeverExpires"
        
        $FilterEndDate   = (Get-Date).AddDays($CountA).Date
        $FilterStartDate = (Get-Date).AddDays($CountB).Date

        Get-ADUser -Filter * -Properties $Properties | 
        Where-Object{$_.mail -and !$_.passwordNeverExpires -and $_.enabled } |
        Select-Object -Property Displayname, GivenName, mail, $ExpirationDate, PasswordNeverExpires, Enabled | 
        Where-Object { $_.'Expiration Date' -lt $FilterEndDate -and $_.'Expiration Date' -gt $FilterStartDate } | 
        Export-Csv -Path $FinalPath -Encoding ascii -NoTypeInformation 
    }
} # End Function PasswordExpiration

Otherwise you can parse ProxyAddresses like:

Function PasswordExpiration
{
    While ($CountB -ne 5)
    {    
        $CountA = $CountA + 1
        $CountB = $CountB + 1

        $FinalPath  = "c:\support\PasswordExpiration\PasswordExpiration"+$CountB+"Days.csv"
        $Properties = "DisplayName", "msDS-UserPasswordExpiryTimeComputed", "mail", "ProxyAddresses", "PasswordNeverExpires"

        #Calculated Property Expressions:
        $EmailAddress   = @{Name = "EmailAddress"; Expression = { ( ( $_.ProxyAddresses.Where( { $_.StartsWith( 'SMTP:' ) } ) ) -replace 'SMTP:' )  } }
        $ExpirationDate = @{Name = "Expiration Date"; Expression = { [DateTime]::FromFileTime( $_."msDS-UserPasswordExpiryTimeComputed" ) } }
                
        $FilterEndDate   = (Get-Date).AddDays($CountA).Date
        $FilterStartDate = (Get-Date).AddDays($CountB).Date

        Get-ADUser -Filter * -Properties $Properties | 
        Where-Object{$_.mail -and !$_.passwordNeverExpires -and $_.enabled } |
        Select-Object Displayname, GivenName, $EmailAddress, $ExpirationDate, PasswordNeverExpires, Enabled  |
        Where-Object { $_.'Expiration Date' -lt $FilterEndDate -and $_.'Expiration Date' -gt $FilterStartDate } |
        Export-Csv -Path $FinalPath -Encoding ascii -NoTypeInformation
    }
} # End Function PasswordExpiration

Obviously this is very crude considering I'm not in your situation, but should give you enough to get through it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 halfer