$data = $null
$data = @{}
Select-String c:\hash2.txt -Pattern "([a-z]+).+?=.+?([a-z.]+)" |
ForEach { $_.matches} |
ForEach { $tokens = @($_.groups[1].value,$_.groups[2].value); $data.Add($tokens[0],$tokens[1])}
#$data
Import-Csv c:\import1.csv | ForEach `
{
$_.name.ToLower() -match "^(?<last>[a-z -]+),( (?<middle>[a-z]+))? (?<first>[a-z-]+)( (?<code>[a-z0-9()]+))?$" | Out-Null
$first = $matches["first"] -replace "[^a-z]","."
$middle = $matches["middle"] -replace "[^a-z]","."
$middle2 = $matches["middle"] -replace "[^a-z]","."
$last = $matches["last"] -replace "[^a-z]","."
$code = $matches["code"] -replace "[()]"
$company = $data[$_.company]
If ($middle)
{ $middle = "." + $middle }
If ($code)
{ $code = "." + $code }
$newSMTP = "{0}{1}{2}.{3}{4}@{5}" -f $first,$middle,$middle2,$last,$code,$company
#$ADUser = Get-ADUser -identity $_.UserID -Properties Company
$mbox = Get-Mailbox $_.UserID
$mbox
If ($mbox.PrimarySMTPAddress -ne $newSMTP)
{
Write-Host -ForegroundColor Green "Setting primary SMTP $newsmtp address for $mbox "
$newsmtp | out-file -filepath c:\smtp4.csv -append
set-mailbox -identity $_.UserID -PrimarySmtpAddress $newsmtp -EmailAddressPolicyEnabled $false
} else {
Write-Host -ForegroundColor red -backgroundcolor yellow "$mbox has an SMTP-Address already exists in system"
}
}
hash2.txt looks like that:
"Company1" = 'company1.com'
"company2" = 'company2.com'
"company3" = 'company3.com'
Now the script working as follows: looking in CSV for company and attach depending on the company the correct mail prefix like @company1.com.
looking in csv for name buidling the name: firstname.lastname@company.com
What i like to do is to write every build smtp to a special worksheet in excel from row1 up.