Search code examples
powershellactive-directoryimport-csv

Get a list of users name, email and proxyaddresses from csv in Powershell, but proxyaddress must be 1 per line


I am working on a script that provides an output with a specific data from some users from csv. The csv is in the following format:

samacountname
User1
User2
USer3

This is what I've got so far, but works when I prompt a single user as expected

$user = Read-Host "prompt samaccountname"
$get = get-aduser -identity $user -properties * -server domain.name
$get | foreach-object { 
foreach($smtp in $_.proxyaddresses {
    $_ | select-object Name,mail,@{n="proxyaddress";e={$smtp}}
    }
} | ft -autosize

read-host "press any key to continue"

Output

Name           mail                proxyAddress
----           ----                ------------
User JohnDoe0  [email protected]       SMTP:[email protected]
User JohnDoe0  [email protected]    smtp:[email protected]
User JohnDoe1  [email protected]       smtp:[email protected]
User JohnDoe1  User1sec2asd.com    SMTP:User1sec2asd.com

How can I do the same with a list of users in a .csv file?

This is what I've tried so far:

$csv = Import-CSV -path "c:\file.csv" -encoding utf8 -delimiter ";"
$get = get-aduser -identity $user -properties * -server domain.name
$csv | foreach-object {
$get | foreach $smtp in $get.proxyaddress { 
foreach($smtp in $_.proxyaddresses {
    $_ | select-object Name,mail,@{n="proxyaddress";e={$smtp}}
    }
} | ft -autosize

read-host "press any key to continue"

Error:

Get-ADUser : Variable: '_' found in expression: $_.samaccountname is not defined.
At C:\TEMP\tivit\act\scripts\consulta dados de email\consulta proxyaddress em massa.ps1:14 char:8
+ $get = Get-ADUser -filter {samaccountname -eq $_.samaccountname} -Pro ...
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Get-ADUser], ArgumentException
    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser
 
ForEach-Object : Cannot bind parameter 'RemainingScripts'. Cannot convert the "in" value of type "System.String" to type "System.Management.Automation.ScriptBlock".
At C:\TEMP\tivit\act\scripts\consulta dados de email\consulta proxyaddress em massa.ps1:18 char:8
+ $get | foreach $smtp in $get.proxyaddresses {
+        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [ForEach-Object], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ForEachObjectCommand

Solution

  • You need to pass each value of the samAccountName column of your CSV to Get-ADUser, all of this in a loop:

    Import-CSV -path "c:\file.csv" -encoding utf8 | ForEach-Object {
        try {
            $user = Get-ADUser $_.samAccountName -Properties proxyAddresses, mail -Server domain.name
            $user.proxyAddresses | ForEach-Object {
                [pscustomobject]@{
                    Name           = $user.Name
                    Mail           = $user.Mail
                    ProxyAddresses = $_
                }
            }
        }
        catch {
            # handle errors here (i.e.: if user couldn't be found)
            Write-Error -Exception $_
        }
    } | Format-Table -AutoSize