将数据导出为CSV但首先对其进行过滤

时间:2016-09-26 19:18:51

标签: email powershell

我需要过滤而不是从AD中查询包含X500的值。这会很好地导出所有内容,但除了smtp条目之外我不需要任何东西。我的条目看起来像这样,我需要

  

SMTP:Administrator@domain.com

我有下面的条目,我不想要。

  

X500:/ o = domain / ou = Exchange管理   组(FYDPDLT)/ cn =收件人/ CN =邮件

$Users = Get-ADUser -Filter "mail -like '*@*'" -Properties proxyAddresses |
select name, @{ L = 'ProxyAddress_1'; E = { $_.proxyaddresses[0] } },
   @{ L = 'ProxyAddress_2'; E = { $_.ProxyAddresses[1] } },
   @{ L = 'ProxyAddress_3'; E = { $_.ProxyAddresses[2] } } | Export-Csv c:\temp\proxyadlist.csv -Notypeinformation -force

2 个答案:

答案 0 :(得分:0)

你的意思是你想要这个吗?

$ldapFilter = "(&(proxyAddresses=smtp:*)(proxyAddresses=x500:*))"
Get-ADUser -LDAPFilter $ldapFilter -Properties proxyAddresses | ForEach-Object {
  $_.proxyAddresses | Where-Object { $_ -match '^smtp:' }
}

这将获取smtp属性中同时包含x500proxyAddresses的所有用户对象,然后仅输出smtp个地址。

答案 1 :(得分:0)

可能会进行进一步的改进,但我掀起了这一点,它应该可以解决问题。我想确保CSV不再需要列。想要展示我认为你需要的基础知识。

# Get all users
$users = Get-ADUser -Filter "mail -like '*@*'" -Properties proxyAddresses

# Calculate the number of columns needed for wanted proxyaddresses
$maximumProxyAddresses = ($users | ForEach-Object{($_.proxyaddresses -match "^smtp").count} |  Measure-Object -Maximum).Maximum

# Build a new object for each user that contains each address we need. 
$users | ForEach-Object{
    $singleUser = $_
    $props = @{Name=$singleUser.Name}

    # Add each proxy address as its own column
    0..($maximumProxyAddresses - 1) | ForEach-Object{
        $props."ProxyAddress$_" = ($singleUser.proxyAddresses -match "^smtp")[$_]
    }

    # Send the hashtable as an object down the pipe.
    [pscustomobject]$props
# Order of variables needs to be corrected.
} | Select (@("name") + (0..($maximumProxyAddresses - 1) | Foreach-object{"proxyaddress$_"}))