Home > Software design >  How can add mail to this table
How can add mail to this table

Time:11-26

Table

server name email
srv1 SecondName
srv2 SecondName
srv3 ThirdName

How can I add mail to same table from Domain? I made a request with Get-ADUser, but it does not work correctly. It does not work correctly.

$CSVpatch = "C:\temp\address.csv"
$result = Import-Csv $CSVpatch -Delimiter ";" -Encoding Default | % {
$name = $_.name;
$server = $_.server;
Get-ADUser -Properties *  -filter " (DisplayName  -eq '$name')" | Select-Object  DisplayName, mail}
$result | Add-Member -MemberType NoteProperty -Name server -Value $server
$result

CodePudding user response:

You should not use -Properties * when all you need are two extra properties.

You can do this using calculated properties like:

$CSVpatch = "C:\temp\address.csv"
$result = Import-Csv -Path $CSVpatch -Delimiter ";" -Encoding Default | ForEach-Object {
    $server = $_.server
    Get-ADUser -Filter "DisplayName -eq '$($_.name)'" -Properties DisplayName, EmailAddress | 
    Select-Object @{Name = 'server'; Expression = {$server}},
                  @{Name = 'name'; Expression = {$_.DisplayName}},
                  @{Name = 'email'; Expression = {$_.EmailAddress}}
}

$result | Format-Table -AutoSize

# or save as CSV perhaps?
$result | Export-Csv -Path 'C:\Temp\address_NEW.csv' -NoTypeInformation -UseCulture
  • Related