Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Define Some Variables
- $stamp=Get-Date -Format "M/dd/yyyy, hh:mm"
- $filepath = "\\%server_name_or_IP%\c$\Scripts"
- $Logfile = "$filepath\Log\new_user.log"
- $path = "$filepath\new_user.csv"
- #Admin Account Information
- If(-not(Test-Path -path $path)){exit}
- Else {
- $MyDomain='%domain_name%'
- $MyClearTextUsername='%DOMAIN_ADMIN_SERVICE_ACCT%'
- $MyClearTextPassword='%password%'
- $MyUsernameDomain=$MyDomain+'\'+$MyClearTextUsername
- $MyUsernameDomain2=$MyClearTextUsername+"@"+"%domain_name.com%"
- $SecurePassword=Convertto-SecureString –String $MyClearTextPassword –AsPlainText –force
- $usercredential=New-object System.Management.Automation.PSCredential $MyUsernameDomain,$SecurePassword
- #Credentials for Office365
- $usercredential2=New-object System.Management.Automation.PSCredential $MyUsernameDomain2,$SecurePassword
- #Define Environment Varibles
- $now=Get-Date -Format "0:M-dd-yyyy, hh-mm"
- $exchangeserver = "%hybrid_exchange_server_name_or_IP%"
- $home_dc = "&domain_controller%"
- $userou = $OU
- $exchangeSession2 = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$exchangeserver/PowerShell/ -Authentication Kerberos -Credential $UserCredential
- Import-PsSession $exchangesession2 -AllowClobber
- # Process the script for each new employee in the csv document
- $UserList=IMPORT-CSV -header a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p $path
- ForEach($Person in $UserList) {
- $firstname=$Person.a
- $middlename=$Person.b
- $lastname=$Person.c
- if ($middlename -eq ""){
- $name_string="$firstname $lastname"
- $username = $Person.d }
- elseif ($middlename -ne "") {
- $middleinitial=($middlename.Substring(0,1))
- $name_string="$firstname $middleinitial $lastname"
- $username = $firstname+"."+$middleinitial+"."+$lastname}
- $name=$name_string
- $companyname = $Person.e
- $office = $Person.g
- $department = $Person.h
- $title = $Person.i
- $manager = $Person.k
- $adpassword = $Person.l+"!"
- $accountpassword = Convertto-SecureString –String $adpassword –AsPlainText –force
- $email_add = $username+"@"+$Person.f
- $creator = $Person.o
- $upn = $username+"@"+$Person.f
- $inital = $Person.p
- #Set OU Based on Office Location
- if ($person.g -eq %officelocation1%) {$OU = "OU_For_Location1%"}
- if ($person.g -eq %officelocation2%) {$OU = "OU_For_Location2%"}
- #Build the Exchange Online Mailbox.
- New-RemoteMailbox -Name $name -DisplayName $name -UserPrincipalName $upn -Password $accountpassword -OrganizationalUnit $userou
- # Sleep 15 minutes for AD replication
- Start-Sleep -Seconds 900
- #Get New AD Account and Fill In Info
- $ADPrincipal = Get-ADUser -Identity $username -Properties * -Server $home_dc
- $membership = Get-Content "$filepath\$username.txt"
- Add-ADPrincipalGroupMembership -Identity $ADPrincipal -Server $home_dc -MemberOf "%add_groups_here%" -Confirm:$false
- 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
- move-item "$filepath\$username.txt" ("$filepath\new_userscript\$username({$now}).txt" -f (get-date))
- #Sleep another 15 minutes for O365 Sync
- Start-Sleep -Seconds 900
- #Assign Office365 License to New Mailbox
- Connect-MsolService -Credential $usercredential2
- $activeunits=(Get-MsolAccountSku | where {$_.AccountSkuId -eq '%company%:ENTERPRISEPACK'}).ActiveUnits
- $consumedunits=(Get-MsolAccountSku | where {$_.AccountSkuId -eq '%company%:ENTERPRISEPACK'}).ConsumedUnits
- if ($activeunits -ne $consumedunits) {Set-MsolUserLicense -UserPrincipalName $username+"@"+"%company_domain.com%" -AddLicenses "%company%:ENTERPRISEPACK"}
- #logging all information
- $Log = "$username was created by $creator on $stamp"
- Add-Content $Logfile (Get-Date)
- Add-Content $Logfile $Log
- #Define email details
- $fromemail = "New Employee <%company@domain.com%>"
- $fromemail2 = "Urgent Automation Alert! <%company@domain.com>"
- $smtpserver = "%smtp_address_here"
- $email=$Person.n
- $sender=$Person.m
- $subject = "$name has been created"
- $subject2 = "Error: No Office 365 Licenses Remain!"
- $body = "*** This is automatically generated email - please do not reply ***
- Hello $sender,
- Name: $name
- Email: $email_add
- All passwords are set to default.
- "
- $body2 = "*** Automated message - Please do not reply. ***
- This message is to inform you that the new user automation is unable to apply an Office 365 license to user: $name
- However, the account has still been created.
- Please verify there are Office 365 licenses available and assign manually to $name. This script will not run again.
- Details
- MsolService result: %company%:ENTERPRISEPACK .ActiveUnits $activeunits is equal to %company%:ENTERPRISEPACK .ConsumedUnits $consumedunits
- "
- #Send an email upon successful completion of user creation
- Send-MailMessage -To "%email@company.com%" -From $fromemail -Subject $subject -Body $body -SmtpServer $smtpserver
- #Send an email upon failure due to unavailable O365 licenses
- else {Send-MailMessage -To "%email@company.com" -From $fromemail2 -Subject $subject2 -Body $body2 -SmtpServer $smtpserver}
- }
- }
- <#---move CSV file to archive-----#>
- move-item "$filepath\new_user.csv" ("$filepath\new_userscript\Processedfile({$now}).csv" -f (get-date))
- exit
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement