Advertisement
Guest User

Untitled

a guest
May 17th, 2016
1,266
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.51 KB | None | 0 0
  1. #Define Some Variables
  2. $stamp=Get-Date -Format "M/dd/yyyy, hh:mm"
  3. $filepath = "\\%server_name_or_IP%\c$\Scripts"
  4. $Logfile = "$filepath\Log\new_user.log"
  5. $path = "$filepath\new_user.csv"
  6.  
  7. #Admin Account Information
  8. If(-not(Test-Path -path $path)){exit}
  9. Else {
  10. $MyDomain='%domain_name%'
  11. $MyClearTextUsername='%DOMAIN_ADMIN_SERVICE_ACCT%'
  12. $MyClearTextPassword='%password%'
  13. $MyUsernameDomain=$MyDomain+'\'+$MyClearTextUsername
  14. $MyUsernameDomain2=$MyClearTextUsername+"@"+"%domain_name.com%"
  15. $SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force
  16. $usercredential=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword
  17. #Credentials for Office365
  18. $usercredential2=New-object System.Management.Automation.PSCredential $MyUsernameDomain2,$SecurePassword
  19.  
  20. #Define Environment Varibles
  21. $now=Get-Date -Format "0:M-dd-yyyy, hh-mm"
  22. $exchangeserver = "%hybrid_exchange_server_name_or_IP%"
  23. $home_dc = "&domain_controller%"
  24. $userou = $OU
  25. $exchangeSession2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeserver/PowerShell/ -Authentication Kerberos -Credential $UserCredential
  26. Import-PsSession $exchangesession2 -AllowClobber
  27.  
  28. # Process the script for each new employee in the csv document
  29. $UserList=IMPORT-CSV -header a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p $path
  30. ForEach($Person in $UserList) {
  31.  
  32. $firstname=$Person.a
  33. $middlename=$Person.b
  34. $lastname=$Person.c
  35. if ($middlename -eq ""){
  36. $name_string="$firstname $lastname"
  37. $username = $Person.d }
  38. elseif ($middlename -ne "") {
  39. $middleinitial=($middlename.Substring(0,1))
  40. $name_string="$firstname $middleinitial $lastname"
  41. $username = $firstname+"."+$middleinitial+"."+$lastname}
  42. $name=$name_string
  43. $companyname = $Person.e
  44. $office = $Person.g
  45. $department = $Person.h
  46. $title = $Person.i
  47. $manager = $Person.k
  48. $adpassword = $Person.l+"!"
  49. $accountpassword = Convertto-SecureString –String $adpassword –AsPlainText –force
  50. $email_add = $username+"@"+$Person.f
  51. $creator = $Person.o
  52. $upn = $username+"@"+$Person.f
  53. $inital = $Person.p
  54.  
  55. #Set OU Based on Office Location
  56. if ($person.g -eq %officelocation1%) {$OU = "OU_For_Location1%"}
  57. if ($person.g -eq %officelocation2%) {$OU = "OU_For_Location2%"}
  58.  
  59.  
  60. #Build the Exchange Online Mailbox.
  61. New-RemoteMailbox -Name $name -DisplayName $name -UserPrincipalName $upn -Password $accountpassword -OrganizationalUnit $userou
  62.  
  63.  
  64. # Sleep 15 minutes for AD replication
  65. Start-Sleep -Seconds 900
  66.  
  67. #Get New AD Account and Fill In Info
  68. $ADPrincipal = Get-ADUser -Identity $username -Properties * -Server $home_dc
  69. $membership = Get-Content "$filepath\$username.txt"
  70.  
  71. Add-ADPrincipalGroupMembership -Identity $ADPrincipal -Server $home_dc -MemberOf "%add_groups_here%" -Confirm:$false
  72.  
  73. Set-ADuser -Server $home_dc -Identity $username -Enabled $true -Manager $manager -Credential $usercredential -SamAccountName $username -GivenName $firstname -Initials $middleinitial -Surname $lastname -DisplayName $name -Company $companyname -Department $department -Title $title -Office $office -City $office
  74.  
  75. move-item "$filepath\$username.txt" ("$filepath\new_userscript\$username({$now}).txt" -f (get-date))
  76.  
  77. #Sleep another 15 minutes for O365 Sync
  78. Start-Sleep -Seconds 900
  79.  
  80. #Assign Office365 License to New Mailbox
  81. Connect-MsolService -Credential $usercredential2
  82. $activeunits=(Get-MsolAccountSku | where {$_.AccountSkuId -eq '%company%:ENTERPRISEPACK'}).ActiveUnits
  83. $consumedunits=(Get-MsolAccountSku | where {$_.AccountSkuId -eq '%company%:ENTERPRISEPACK'}).ConsumedUnits
  84. if ($activeunits -ne $consumedunits) {Set-MsolUserLicense -UserPrincipalName $username+"@"+"%company_domain.com%" -AddLicenses "%company%:ENTERPRISEPACK"}
  85.  
  86.  
  87.  
  88. #logging all information
  89. $Log = "$username was created by $creator on $stamp"
  90. Add-Content $Logfile (Get-Date)
  91. Add-Content $Logfile $Log
  92.  
  93. #Define email details
  94. $fromemail = "New Employee <%company@domain.com%>"
  95. $fromemail2 = "Urgent Automation Alert! <%company@domain.com>"
  96. $smtpserver = "%smtp_address_here"
  97. $email=$Person.n
  98. $sender=$Person.m
  99. $subject = "$name has been created"
  100. $subject2 = "Error: No Office 365 Licenses Remain!"
  101. $body = "*** This is automatically generated email - please do not reply ***
  102.  
  103. Hello $sender,
  104.  
  105.  
  106. Name: $name
  107. Email: $email_add
  108. All passwords are set to default.
  109.  
  110.  
  111. "
  112. $body2 = "*** Automated message - Please do not reply. ***
  113.  
  114. This message is to inform you that the new user automation is unable to apply an Office 365 license to user: $name
  115.  
  116. However, the account has still been created.
  117.  
  118. Please verify there are Office 365 licenses available and assign manually to $name. This script will not run again.
  119.  
  120.  
  121.  
  122. Details
  123.  
  124. MsolService result: %company%:ENTERPRISEPACK .ActiveUnits $activeunits is equal to %company%:ENTERPRISEPACK .ConsumedUnits $consumedunits
  125.  
  126. "
  127.  
  128. #Send an email upon successful completion of user creation
  129. Send-MailMessage -To "%email@company.com%" -From $fromemail -Subject $subject -Body $body -SmtpServer $smtpserver
  130.  
  131. #Send an email upon failure due to unavailable O365 licenses
  132. else {Send-MailMessage -To "%email@company.com" -From $fromemail2 -Subject $subject2 -Body $body2 -SmtpServer $smtpserver}
  133.  
  134. }
  135. }
  136.  
  137. <#---move CSV file to archive-----#>
  138. move-item "$filepath\new_user.csv" ("$filepath\new_userscript\Processedfile({$now}).csv" -f (get-date))
  139. exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement