Powershell ile AD ve Exchange Hesabı Oluşturma

Aşağıdaki Powershell Script’i sayesinde Active Directory ve MS Exchange hesabını bir arada oluşturabilirsiniz.
Script, AD ve Exchange hesabı oluşturması dışında arka planda çok daha kapsamlı ve detaylı işler de gerçekleştiriyor.
Script’in yaptığı işleri sıralarsak;
- İlk olarak yetkili hesap girişi istiyor. Giriş yaptıktan sonra, sizden yeni oluşturulacak kullanıcı hakkında bazı input’lar girilmesi isteniyor.
- Yeni oluşturulacak kullanıcının yetkisi, grubu vs. referans olarak istenen kişi ile aynı oluyor. Ve bu referans alınan kişi hangi OU içinde ise, yeni kullanıcı o OU içine açılıyor.
- Daha sonra Exchange mail adresi için DB adı girmemiz isteniyor.
- Gerekli bilgileri girdikten sonra, ADCreatingUser kısmında AD üzerinde yeni kullanıcıyı oluşturuyor. ADCopyMember kısmında, referans alınan kullanıcının yetki ve grupları yeni kullanıcıya tanımlanıyor.
- CreatingMailbox kısmında Exchange makinesine session açıp, kullanıcının mail hesabını ilgili DB’de oluşturuyor.
- Tüm bu işlemlerden sonra işe yeni başlayan kişiye tüm bilgileri içeren bir rapor vermek kim istemez ki! İşte en son kısımda script, baştan – en sona kadar yaptığı işlerin özetini (yani; yeni işe başlayan kişinin kullanıcı adı, şifresi, mail adresi, şirket hakkında gerekli bilgiler vs.) içeren .pdf uzantılı bir rapor oluşturuyor.
- Bu işlemi yaparken PS 2 şey kullanıyor; birincisi MSSQL Reporting Services, ikincisi Web Servis.
- En basit anlatımıyla; Web Servis’e script içerisindeki bilgileri post ediyoruz, Web Servis ise bu bilgileri MSSQL Reporting Services içinde oluşturduğumuz bir raporun içine basıyor.
- Script’in en sonunda ise belirttiğimiz path’de rapor oluşuyor ve karşımıza pop-up olarak açılıyor.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
cls #Text Write-Host –foregroundcolor yellow "##########################" Write-Host –foregroundcolor yellow "# Script: StartofWork #" Write-Host –foregroundcolor yellow "# Author: Nuri Gundogan #" Write-Host –foregroundcolor yellow "# Version: 5.0 #" Write-Host –foregroundcolor yellow "##########################" #Variables Write-Host -ForegroundColor Magenta "Yetkili Hesap Girişi" $UserName = Read-Host "Kullanıcı Adınızı Giriniz (Domain\Username)" Write-Host –foregroundcolor green "Açılan Ekranda Şifrenizi Giriniz." $Password = Read-Host -AsSecureString "Şifrenizi Giriniz" New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $UserName, $Password Write-Host –foregroundcolor yellow "Açılacak Hesabın..." $Name = Read-Host "Adı" $SurName = Read-Host "Soyadı" Write-Host –foregroundcolor green "Kullanıcının Tam Adını ve Soyadını Giriniz." $NameSurname = Read-Host "Adı ve Soyadı" $SAM = Read-Host "SAM Account Name (domain.user)" $UPN = $SAM + "@UPNUzantısı" $Mail = $SAM + "@MailUzantısı" Write-Host –foregroundcolor yellow "UPN Account Oluşturuldu: $SAM@UPNUzantısı" Write-Host –foregroundcolor yellow "Şifresi Default Olarak Ayarlandı." $Title = Read-Host "Ünvanı" $Department = Read-Host "Departmanı" $TelephoneNumber = Read-Host "Telefon Numarası (Mobil)" $ShortCode = Read-Host "Dahili/Kısa Kodu" $Manager = Read-Host "Yöneticisi (domain.user)" if ($Manager -eq "") { Write-Host -foregroundcolor red "Yönetici Seçilmedi!" } $Access = Read-Host "Yetkiler için Referans Alınacak Kişi (domain.user)" $UserDistPath = Get-ADUser $Access | select distinguishedName $DN = $UserDistPath.distinguishedName | foreach{($_ -split ',')[1..8] -join ','} $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://ExchangeMakineAdı/PowerShell/ -Authentication Kerberos Write-Host –foregroundcolor green "Mail Adresini Oluşturmak için Mailbox Database Seçimi Yapınız." $MailDB = Read-Host "Database Seçimi" Write-Host –foregroundcolor yellow "Kullanıcı Hesabı Oluşturuluyor..." Write-Host –foregroundcolor yellow "Mail Adresi Oluşturuluyor..." Write-Host –foregroundcolor yellow "WelcomeReport Oluşturuluyor..." $MailAdress = $UPN $Access2 = $Access + "@MailUzantısı" #ADCreatingUser New-ADUser -GivenName $Name -Surname $SurName -Name $NameSurname -DisplayName $NameSurname -Title $Title -Department $Department -SamAccountName $SAM -UserPrincipalName $UPN -AccountPassword (ConvertTo-SecureString -AsPlainText "DefaultPassword" -Force) -ChangePasswordAtLogon $true -Enabled $true -Company "ŞirketAdı" -MobilePhone $TelephoneNumber -OfficePhone $ShortCode -Manager $Manager -Path $DN #ADCopyMember $CopyFromUser = Get-ADUser $Access -prop MemberOf $CopyToUser = Get-ADUser $SAM -prop MemberOf $CopyFromUser.MemberOf | Where{$CopyToUser.MemberOf -notcontains $_} | Add-ADGroupMember -Members $CopyToUser #CreatingMailbox Import-PSSession $Session Enable-Mailbox -Alias $SAM -Identity $MailAdress -Database $MailDB Remove-PSSession $Session #Creating WelcomeReport $apix = "WebServisAPI" $headers = @{} $headers.Add("APIKey","Key") $endpointx = "$apix/welcomescript" $bodyCredx = @" { "username": "$SAM", "password": "DefaultPassword", "email": "$Mail" } "@ $defaultEncoding = [System.Text.Encoding]::GetEncoding('ISO-8859-1') #welcomereport $utf8Bytes = [System.Text.Encoding]::UTf8.GetBytes($bodyCredx) $bodyCredx = $defaultEncoding.GetString($utf8Bytes) $Path = "\\DataSource" $PDF = "\\DataSource\$SAM.pdf" New-Item -ItemType Directory -Path $Path -ErrorAction SilentlyContinue | Out-Null $response2 = Invoke-WebRequest $endpointx -UseDefaultCredentials -SessionVariable my_session -UseBasicParsing -Method Post -OutFile $PDF -Headers $headers -Body $bodyCredx -ContentType 'application/json' | ConvertFrom-Json #Info Write-Host –foregroundcolor green "Başarılı!" Write-Host –foregroundcolor yellow "Kullanıcı Hesabı Oluşturuldu: $SAM" Write-Host –foregroundcolor yellow "Mail Adresi Oluşturuldu: $SAM@MailUzantısı" Write-Host –foregroundcolor yellow "WelcomeReport Oluşturuldu." Start-Sleep -s 3 Start-Process ((Resolve-Path "\\DataSource\$SAM.pdf").Path) |