dkplayer

AD Exchange Skyep add User v2

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