dkplayer

AD Exchange Skype user cration whith password generator

Dec 25th, 2018
1,015
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #############################################################
  2. # Compiled from internet by Dyachenko Konstantin
  3. # Website: https://www.k7d.ru
  4. #############################################################
  5.  
  6. Add-Type -assembly System.Windows.Forms
  7. $UserCredential = Get-Credential
  8.  
  9. function Get-RandomCharacters1($length, $characters) {
  10.     $random = 1..$length | ForEach-Object { Get-Random -Maximum $characters.length }
  11.     $private:ofs=""
  12.     return [String]$characters[$random]
  13. }
  14.  
  15. #Параметры отправки почты
  16. $EmailFrom = "[email protected]"
  17. $EmailTo = "[email protected]"
  18. $Subject = "Новые пользователи"
  19. $body = "Новые пользователи в домене contoso.com:      `n`n"
  20.  
  21. #Основные параметры
  22. #Файл со списком добавляемых пользователей
  23. $UserList=IMPORT-CSV  -Encoding default users.csv
  24. $Domain="@contoso.com"
  25. $domain_short="contoso\"
  26. # OU куда добавлять
  27. $OU="OU=new,OU=russia,DC=contoso,DC=com"
  28. #Параметры почтового сервера для отправки сообщения
  29. $SmtpServer = “mgw.contoso.com”
  30. $smtp_port=25
  31.  
  32. #Параметры Exchange
  33. $exch_database="Mailbox Database"
  34. $exch_server="http://ru-exch01.contoso.com/PowerShell/"
  35.  
  36. #Skyep
  37. $sip_domain="contoso.com"
  38. $skype_server="https://ru-skyep.contoso.com/ocspowershell/"
  39. $sip_pool="ru-skyep.contoso.com"
  40.  
  41.  
  42. #Создание формы
  43. $main_form = New-Object System.Windows.Forms.Form
  44. $main_form.Text ='Import CSV Users'
  45. $main_form.Width = 100
  46. $main_form.Height = 100
  47. $main_form.AutoSize = $true
  48.  
  49. $checkbox1 = new-object System.Windows.Forms.checkbox
  50. $checkbox1.Location = new-object System.Drawing.Size(10,10)
  51. $checkbox1.Size = new-object System.Drawing.Size(60,20)
  52. $checkbox1.Text = "email"
  53. $checkbox1.Checked = $false
  54. $main_form.Controls.Add($checkbox1)
  55.  
  56. $checkbox2 = new-object System.Windows.Forms.checkbox
  57. $checkbox2.Location = new-object System.Drawing.Size(80,10)
  58. $checkbox2.Size = new-object System.Drawing.Size(60,20)
  59. $checkbox2.Text = "Skype"
  60. $checkbox2.Checked = $false
  61. $main_form.Controls.Add($checkbox2)
  62.  
  63.  
  64. function TranslitRU2LAT ($inString) {
  65.     $Translit = @{
  66.     [char]'а' = "a";[char]'А' = "a";
  67.     [char]'б' = "b";[char]'Б' = "b";
  68.     [char]'в' = "v";[char]'В' = "v";
  69.     [char]'г' = "g";[char]'Г' = "g";
  70.     [char]'д' = "d";[char]'Д' = "d";
  71.     [char]'е' = "e";[char]'Е' = "e";
  72.     [char]'ё' = "e";[char]'Ё' = "e";
  73.     [char]'ж' = "zh";[char]'Ж' = "zh";
  74.     [char]'з' = "z";[char]'З' = "z";
  75.     [char]'и' = "i";[char]'И' = "i";
  76.     [char]'й' = "y";[char]'Й' = "y";
  77.     [char]'к' = "k";[char]'К' = "k";
  78.     [char]'л' = "l";[char]'Л' = "l";
  79.     [char]'м' = "m";[char]'М' = "m";
  80.     [char]'н' = "n";[char]'Н' = "n";
  81.     [char]'о' = "o";[char]'О' = "o";
  82.     [char]'п' = "p";[char]'П' = "p";
  83.     [char]'р' = "r";[char]'Р' = "r";
  84.     [char]'с' = "s";[char]'С' = "s";
  85.     [char]'т' = "t";[char]'Т' = "t";
  86.     [char]'у' = "u";[char]'У' = "u";
  87.     [char]'ф' = "f";[char]'Ф' = "f";
  88.     [char]'х' = "kh";[char]'Х' = "kh";
  89.     [char]'ц' = "ts";[char]'Ц' = "ts";
  90.     [char]'ч' = "ch";[char]'Ч' = "ch";
  91.     [char]'ш' = "sh";[char]'Ш' = "sh";
  92.     [char]'щ' = "shch";[char]'Щ' = "shch";
  93.     [char]'ъ' = "";[char]'Ъ' = "";
  94.     [char]'ы' = "y";[char]'Ы' = "y";
  95.     [char]'ь' = "";[char]'Ь' = "";
  96.     [char]'э' = "e";[char]'Э' = "e";
  97.     [char]'ю' = "yu";[char]'Ю' = "yu";
  98.     [char]'я' = "ya";[char]'Я' = "ya"
  99.     }
  100.     $outChars = ""
  101.     foreach ($c in $inChars = $inString.ToCharArray())
  102.         {
  103.         if ($Translit[$c] -cne $Null )
  104.             {$outChars += $Translit[$c]}
  105.         else
  106.             {$outChars += $c}
  107.         }
  108.  
  109.     Write-Output $outChars
  110.  
  111.  }
  112. #
  113.  
  114. $i=1
  115. #Задание массивов данных пользователей
  116. $Username=@()
  117. $transname=@()
  118. $FIO=@()
  119. $FN=@()
  120. $SN=@()
  121. $City=@()
  122. $Company=@()
  123. $Departament=@()
  124. $Title=@()
  125. $Phone=@()
  126. $old=@()
  127. $Password=@()
  128.  
  129. FOREACH ($Person in $UserList) {
  130. $Username+=$Person.Username
  131. $firstletter=$Person.Firstname[0]
  132. $transname1=$firstletter+$Person.Lastname
  133. $transname+=TranslitRU2LAT $transname1
  134. $FIO+=$Person.FIO
  135. $FN+=$Person.Firstname
  136. $SN+=$Person.Lastname
  137. $City+=$Person.City
  138. $Company+=$Person.Company
  139. $Departament+=$Person.Departament
  140. $Title+=$Person.Title
  141. $Phone+=$Person.Phone
  142. $old+=$Person.uSERNAME
  143. #генератор сложного пароля 12 символов 3-4 варианта символов
  144. $tmppw1=Get-RandomCharacters1 -length 4 -characters 'abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ'
  145. $tmppw2=Get-RandomCharacters1 -length 4 -characters '1234567890@&!'
  146. $tmppw3=Get-RandomCharacters1 -length 4 -characters 'abcdefghiklmnoprstuvwxyzABCDEFGHKLMNOPRSTUVWXYZ'
  147. $Password+=$tmppw1+$tmppw2+$tmppw3
  148. $Label = New-Object System.Windows.Forms.Label
  149. $Label.Text = $FIO[($i-1)]
  150. $Label.Location  = New-Object System.Drawing.Point(10,($i*20+20))
  151. $Label.AutoSize = $true
  152. $main_form.Controls.Add($Label)
  153.  
  154. $TextBox = New-Object System.Windows.Forms.TextBox
  155. $textBox.Name = "TextBox"+$i;
  156. $TextBox.Location  = New-Object System.Drawing.Point(256,($i*20+20))
  157. $TextBox.Text = $transname[($i-1)]
  158. $main_form.Controls.Add($TextBox)
  159.  
  160.  
  161. $i=$i+1
  162. }
  163. $button = New-Object System.Windows.Forms.Button
  164. $button.Text = 'Add'
  165. $button.Location = New-Object System.Drawing.Point(10,($i*20+20))
  166. $main_form.Controls.Add($button)
  167.  
  168.  
  169. $Label2 = New-Object System.Windows.Forms.Label
  170. $Label2.Location  = New-Object System.Drawing.Point(10,($i*20+40))
  171. $Label2.AutoSize = $true
  172. $main_form.Controls.Add($Label2)
  173.  
  174. $ProgressBar = New-Object System.Windows.Forms.ProgressBar
  175. $ProgressBar.Location  = New-Object System.Drawing.Point(10,($i*20+60))
  176. $main_form.Controls.add($ProgressBar)
  177.  
  178. $button.Add_Click({
  179. $progress=(100/$i)
  180. $ProgressBar.Value = $progress
  181.  
  182. $controls = $main_form.controls
  183. For ($j=1; $j -lt $i; $j++)  {
  184. $text1=$controls['TextBox'+$j].text
  185. $Label2.Text = "Adding "+$text1+"..."
  186. #Add AD Users
  187. $UPN=$text1+$Domain
  188.  
  189. # Отключение проверки отзыва сертификата при подключении к удаленному PowerShell
  190. $sessionOption = New-PSSessionOption -SkipRevocationCheck
  191.  
  192. #Добавление пользователя в AD
  193. New-ADUser -Name $FIO[($j-1)] –GivenName $FN[($j-1)] –Surname $SN[($j-1)] –DisplayName $FIO[($j-1)]  –SamAccountName $text1 –UserPrincipalName $UPN -City $City[($j-1)] -Company $Company[($j-1)] -Department $Departament[($j-1)] -Title $Title[($j-1)] -OfficePhone $Phone[($j-1)] -Path $OU
  194. Set-ADAccountPassword -Identity $text1 -NewPassword (ConvertTo-SecureString -AsPlainText $Password[($j-1)] -Force)
  195. Enable-ADAccount -Identity $text1
  196. Start-sleep -s 1
  197.  
  198. #Создание почтового ящика
  199. if ($checkbox1.Checked -eq $true)
  200. {
  201.     Write-host "Создаю почтовый ящик"
  202.     $body=$body+"Создан почтовый ящик "+$text1+$Domain+"`n`n"
  203.     $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $exch_server -Authentication Kerberos -Credential $UserCredential -SessionOption $sessionOption
  204.     Import-PSSession $Session
  205.     Enable-Mailbox -Identity $domain_short$text1 -Database $exch_database
  206.     Remove-PSSession $Session
  207.     Start-sleep -s 3
  208.   }
  209.  
  210. #Добавление на Lync User
  211. if ($checkbox2.Checked -eq $true)
  212. {
  213.     Write-host "Создаю Skype"
  214.     $body=$body+"Создан Skype "+$text1+$Domain+"`n`n"
  215.     $Session = New-PSSession -ConnectionUri $skype_server -Credential $UserCredential -SessionOption $sessionOption
  216.     Import-PSSession $Session
  217.     Enable-CsUser -Identity $domain_short$text1 -RegistrarPool $sip_pool -SipAddressType UserPrincipalName  -SipDomain $sip_domain
  218.     Remove-PSSession $Session
  219. }
  220.  
  221.  
  222.  
  223. $ProgressBar.Value = $ProgressBar.Value+$progress
  224. $body=$body+" "+$Departament[($j-1)]+"`n`n ФИО: "+$FIO[($j-1)]+"`n`n Логин: "+$text1+"`n`n Пароль: "+$Password[($j-1)]+"`n`n`n`n"
  225. }
  226.  
  227. #Отправка почты
  228. $smtp = New-Object net.mail.smtpclient($SmtpServer)
  229. $smtp.Port=$smtp_port
  230. $smtp.Send($EmailFrom, $EmailTo, $Subject, $body)
  231. $wshell = New-Object -ComObject Wscript.Shell
  232. $wshell.Popup("Operation Completed",0,"Done",0x1)
  233. $ProgressBar.Value = 0
  234. })
  235.  
  236. $main_form.ShowDialog()
Advertisement
Add Comment
Please, Sign In to add comment