简体   繁体   English

从代理地址 powershell 获取主 SMTP

[英]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).不幸的是,我需要从 proxyaddresses PrimarySMTP 中提取地址(从脚本中的 email 字段中提取地址)。 I have tried this process like crazy and cant get it to pull and output the info.我已经疯狂地尝试了这个过程,但无法将其拉出 output 信息。 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

    }

}

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" EmailAddress 不是您正在寻找“邮件”的 AD 属性
  2. mail sometimes differes from the primary SMTP address on the ProxyAddesses property. mail 有时与 ProxyAddesses 属性上的主要 SMTP 地址不同。 That property returns as a string array.该属性作为字符串数组返回。 To isolate the primary you need to look for the one starting with capitol "SMTP:".要隔离主节点,您需要查找以首字母“SMTP:”开头的主节点。 Below I used the string .StartsWith() method to isolate it.下面我使用字符串.StartsWith()方法来隔离它。 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.我还删除了很多到Where-Object的管道,因为您可以使用 -and 运算符。

If you have no reason to believe the mail property will stray from the primary address on the ProxyAddresses property just use it like:如果您没有理由相信 mail 属性会偏离 ProxyAddresses 属性上的主地址,只需像这样使用它:

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:否则,您可以解析 ProxyAddresses,例如:

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.显然,考虑到我不在您的情况下,这非常粗略,但应该给您足够的时间来度过难关。 Let me know how it goes.让我知道事情的后续。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM