I have a CSV file, below is an example:
FirstName, LastName, SamAccountName, Email
Peter, Ivanov, ivanov, ivanov@scripttest.ru
Yana, Budnya, budnya, budnya@scripttest.ru
I want to check SamAccountName, and if it already exists, then add the number 1, repeat the check, if this name is there, then add not 1 but 2.
For example, ivanov exists (validation is required via Get-mailbox, since we would like to use only 1 Exchange module due to Exchange integration with AD, for example, Get-mailbox -Filter “samaccountname -eq ‘ivanov’” ", add 1, get ivanov1, repeat the check, we get that ivanov1 is there, then we add 2 and we get ivanov2, we repeat the check, we get the answer that ivanov2 is unique.
Create a new CSV file in the same folder and add ivanov2 to CSV along with the rest of the data, we get:
FirstName, LastName, SamAccountName, Email
Peter, Ivanov, ivanov2, ivanov@scripttest.ru
Yana, Budnya, budnya, budnya@scripttest.ru
We create a user through the New-Mailbox, pause for synchronization, set the required Set-Mailbox limits.
At the output we have 2 files, the first one sent and the second new one with the changes.
This is part of the code I tried, but it doesn’t work:
$Logon = “ivanov”
$NTName = $Logon
$Found = $False
For ($i=1; $i ++) {
Check for existence of object with sAMAccountName $NTName.
$Check = Get-mailbox -Filter “samaccountname -eq ‘$NTName’”
If ($Check) {$NTName = “$Logon$i”}
Else {$Found = $True;Break}
}
If ($Found -eq $True)
{
Create user with sAMAccountName $NTName.
Write-Host $Logon “found” $NTName -ForegroundColor Red
}
Else
{
Write-Host $Logon “not found” -ForegroundColor Green
}
Below is an example of the original code, but there is no SamAccountName re-check that is an error:
clear screen
clear
import Exchange module
Add-pssnapin Microsoft.Exchange.Management.PowerShell.SnapIn
translate function
$csv = Read-Host Please input file path and name
check file extension, if not CSV - message and exit after 3 seconds
$exten = [IO.Path]::GetExtension(‘$csv’) -ne ‘csv’
if ($exten -eq $false){
write-host “This is not a CSV file” -ForegroundColor Red
Start-Sleep -s 3
exit
}
for replace emty string in CSV if found
creation temp file *_data.csv in the same folder
$csvfile = $csv.TrimEnd(“.csv”) + "data.csv"
$CSVData = Import-Csv $csv
$CSVData | ForEach-Object{
if($.FirstName -eq “”) {
Write-Host found empty string CSV for $.LastName -ForegroundColor Red
$CSVDatanew = Read-Host please input 2 letters for replace
$.FirstName=$CSVDatanew
$CSVData | Export-Csv $csvfile -NoTypeInformation -Encoding UTF8
}
elseif($.LastName -eq “”) {
Write-Host found empty string CSV for $.FirstName -ForegroundColor Red
$_.LastName=$CSVDatanew
$CSVData | Export-Csv $csvfile -NoTypeInformation -Encoding UTF8
}
}
request organization name example: test.ru
$org = Read-Host please input tenant name
request single password for accounts
$Password = Read-Host please input password for users account
set quota
$IWQ = “0.5gb”
$PSQ = “0.7gb”
$PSRQ = “1gb”
change the original file name (add “_changes”) to export it to the same location as the original
$csvfilepath = $csv.TrimEnd(“.csv”) + “_changes.csv”
checking for the presence of the file, which is created when there are empty stringes in the CSV
removing temp file - $csvfile
$WantFile = $csv.TrimEnd(“.csv”) + “_data.csv”
$FileExists = Test-Path $WantFile
If ($FileExists -eq $False) {
$csvfile = $csv
Write-Host In CSV no empty string -ForegroundColor Green
}
else
{
$csvfile = $csvfile
Remove-Item -Path $csvfile
Write-Host In CSV found empty string and replaced -ForegroundColor Yellow
}
check exchange alias=sam account name
if exist then replace sam account name
and export to new csv file
$checkSAM = Import-Csv $csvfile
$out = $checkSAM | ForEach{$exist = [bool](Get-mailbox $.SamAccountName -erroraction SilentlyContinue);
if ($Exist){
Write-Host $.SamAccountName “found, will be change” -ForegroundColor Red
translate first name
$transFirstName=Translit($.FirstName)
$transFirstNamel=$transFirstName.ToLower()
$FirstName = $.FirstName
$LastName = $_.LastName
replace sam Account Name
$SAM = $.samAccountName + $transFirstNamel
$Email = $.Email
creating an object with values for export
[PSCustomObject]@{
FirstName = $FirstName
LastName = $LastName
SamAccountName = $SAM
Email = $Email
export new values
} | Export-Csv $csvfilepath -NoTypeInformation -Append -Force -Encoding UTF8
}else{
Write-Host $.SamAccountName “not found, no changes” -ForegroundColor Green
$FirstName = $.FirstName
$LastName = $.LastName
$SAM = $.samAccountName
$Email = $_.Email
creating an object with values for export
[PSCustomObject]@{
FirstName = $FirstName
LastName = $LastName
SamAccountName = $SAM
Email = $Email
export new values
} | Export-Csv $csvfilepath -NoTypeInformation -Append -Force -Encoding UTF8
}
}
message about export changes in CSV
Write-Host “Changes saved in file” $csvfilepath -ForegroundColor Yellow
creating an account and setting quotas
Import-Csv $csvfilepath | ForEach-Object {
$samAccountName = $.SamAccountName
$upn = $.SamAccountName + “@" + $org
$uname = $.LastName + " " + $.FirstName
$OUpath = “OU=” + $org + “,OU=tenants,DC=ec,DC=mts,DC=ru”
$Email = $_.Email
translate first name + last name
$transLastName=Translit($.LastName)
$transFirstName=Translit($.FirstName)
$transInitials=Translit($_.Initials)
$transuname = $transLastName + " " + $transFirstName
creating mailbox-account
New-Mailbox -Name $uname `
-UserPrincipalName $upn `
-Password (ConvertTo-SecureString $Password -AsPlainText -force) `
-Alias $samAccountName `
-FirstName $.FirstName `
-LastName $.LastName `
-DisplayName $uname `
-OrganizationalUnit $OUpath `
-ResetPasswordOnNextLogon $true `
-erroraction silentlycontinue | Out-Null
pause for sync Exchange-AD changes
Start-Sleep -s 5
set Warning Quota, Prohibit Send Quota, Prohibit Send Receive Quota, Use Database Quota Defaults
set correct email address (reason: default policy)
need only for policy, but there is a question about the direction of the parameters:
-CustomAttribute1 @{‘extensionAttribute1’=$org} `
policy disabled -EmailAddressPolicyEnabled
Set-Mailbox -Identity $upn `
-IssueWarningQuota $IWQ `
-ProhibitSendQuota $PSQ `
-ProhibitSendReceiveQuota $PSRQ `
-EmailAddressPolicyEnabled $false `
-EmailAddresses $Email `
-UseDatabaseQuotaDefaults $false
}