Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # Make sure script is running in correct folder.
- #
- CD "path to script"
- ##################
- # Static Entries #
- ##################
- $ExchangeServer = "ExchangeServer.domain.com"
- $LyncServer = "LyncServer.domain.com"
- $Registrar = "LyncServer.domain.com"
- #$dialplan = "Registrar:LyncServer.domain.com"
- $intdomain = "domain.com"
- $sipdomain = "domain.com"
- $company = "Company/School Name"
- $mbdb = "ExchangeMailDatabaseName"
- $website = "www.domain.com"
- #$umpolicy = "Unified Messaging Policy Name"
- $state = "US State"
- $SpecChars = '!', '"', '£', '$', '%', '&', '^', '*', '(', ')', '@', '=', '+', '¬', '`', '\', '<', '>', '.', '?', '/', ':', ';', '#', '~', "'", '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-', ' ', 'jr', 'III', 'II'
- $remspecchars = [string]::join('|', ($SpecChars | % {[regex]::escape($_)}))
- $today = Get-Date
- $expiredate = (Get-Date).AddDays(21)
- Clear-Content .\CreateStudent_newstudents.csv
- Clear-Content .\CreateStudent_dupcheck.csv
- Clear-Content .\CreateStudent_LOG_Duplicates.txt
- Clear-Content .\CreateStudent_LOG_Created.txt
- Clear-Content .\CreateStudent_LOG_NoClass.txt
- $expiredate
- #$ErrorActionPreference= 'silentlycontinue'
- $script:countexists = 0
- $script:countinvalid = 0
- $script:countnew = 0
- $script:countnoclass = 0
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" " New import started: $today"
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" " New import started: $today"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
- Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
- Add-Content "CreateStudent_LOG_NoClass.txt" " New import started: $today"
- Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
- Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
- Add-Content "CreateStudent_LOG_Created.txt" " New import started: $today"
- Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
- ##############################
- # Import session information #
- ##############################
- $User = "domain\user"
- $Pass = "supersecretpassword" | ConvertTo-SecureString -asPlainText -Force
- $MyCredential=New-Object -TypeName System.Management.Automation.PSCredential($User,$Pass)
- $ExchSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionURI http://$ExchangeServer/powershell -Credential $MyCredential
- Import-PSSession $ExchSession
- $LyncSessionOptions = New-PSSessionOption -SkipCACheck:$true -SkipCNCheck:$true -SkipRevocationCheck:$true
- $LyncSession = New-PSSession -ConnectionURI https://$LyncServer/ocspowershell -Credential $MyCredential -SessionOption $LyncSessionOptions
- Import-PSSession $LyncSession
- Function dataimport {
- #######################
- # Data Import section #
- #######################
- $fname = $_.fname2
- #$fname = $fname.Substring(0,1).ToUpper()+$fname.Substring(1).ToLower()
- $finit = $fname.Substring(0,1)
- #$mname = $_.mname
- #$minit = $mname.Substring(0,1)
- $lname = $_.lname2
- #$lname = $lname.Substring(0,1).ToUpper()+$lname.Substring(1).ToLower()
- $linit = $lname.Substring(0,1)
- $name = $_.name
- $password = (convertto-securestring "GenericPassword" -asplaintext -force)
- # Remove unwanted characters
- $lname = $lname -replace $remspecchars, ""
- $fname = $fname -replace $remspecchars, ""
- $program = $_.program2
- $sam = $_.sam
- Switch ($program)
- {
- AFS
- {
- $wing = "D-Wing"
- $room = "D50"
- $script = "D50"
- }
- AMT
- {
- $wing = "Aviation"
- $room = "AVStudents"
- $script = "AVStudents"
- }
- AMT1
- {
- $wing = "Aviation"
- $room = "AV-B10"
- $script = "AV-B10"
- }
- AMT2
- {
- $wing = "Aviation"
- $room = "AV-B30"
- $script = "AV-B30"
- }
- AMT3
- {
- $wing = "Aviation"
- $room = "AV-A20"
- $script = "AV-A20"
- }
- AMT4
- {
- $wing = "Aviation"
- $room = "AV-C30"
- $script = "AV-C30"
- }
- "AST Seminole"
- {
- $wing = "Seminole"
- $room = "SM-S60"
- $script = "SM-S60"
- }
- "AST Shawnee"
- {
- $wing = "G-Wing"
- $room = "G60"
- $script = "G60"
- }
- AWT
- {
- $wing = "B-Wing"
- $room = "B30"
- $script = "B30"
- }
- CMT
- {
- $wing = "H-Wing"
- $room = "H30"
- $script = "H30"
- }
- COS
- {
- $wing = "Seminole"
- $room = "SM-S40"
- $script = "SM-S40"
- }
- CRT
- {
- $wing = "C-Wing"
- $room = "C40"
- $script = "C40"
- }
- DMP
- {
- $wing = "Seminole"
- $room = "SM-S20"
- $script = "SM-S20"
- }
- ECE
- {
- $wing = "L-Wing"
- $room = "L30"
- $script = "L30"
- }
- ECT
- {
- $wing = "F-Wing"
- $room = "F10"
- $script = "F10"
- }
- EMT
- {
- $wing = "E-Wing"
- $room = "E70"
- $script = "E70"
- }
- GD
- {
- $wing = "G-Wing"
- $room = "G20"
- $script = "G20"
- }
- HAR
- {
- $wing = "H-Wing"
- $room = "H40"
- $script = "H40"
- }
- "HCC Seminole"
- {
- $wing = "Seminole"
- $room = "SM-S50"
- $script = "SM-S50"
- }
- "HCC Shawnee"
- {
- $wing = "E-Wing"
- $room = "E90"
- $script = "E90"
- }
- "LPN"
- {
- $wing = "E-Wing"
- $room = "E15"
- $script = "E15"
- }
- "LPN eve"
- {
- $wing = "E-Wing"
- $room = "E60"
- $script = "E60"
- }
- CNT
- {
- $wing = "E-Wing"
- $room = "E20"
- $script = "E20"
- }
- OMT
- {
- $wing = "D-Wing"
- $room = "D80"
- $script = "D80"
- }
- PDT
- {
- $wing = "M-Wing"
- $room = "M20"
- $script = "M20"
- }
- PDT2
- {
- $wing = "M-Wing"
- $room = "M30"
- $script = "M30"
- }
- PEA
- {
- $wing = "D-Wing"
- $room = "Engineering"
- $script = "Engineering"
- }
- PM
- {
- $wing = "B-Wing"
- $room = "PrecisionMachining"
- $script = "PrecisionMachining"
- }
- SCA
- {
- $wing = "C-Wing"
- $room = "C30"
- $script = "C30"
- }
- default
- {
- $wing = "bork"
- $room = "bork"
- }
- }
- $ou = "OU=Users,OU=$room,OU=$Wing,OU=Students,DC=domain,DC=com"
- $title = $program
- $desc = $title + " Student"
- $dept = $room
- #$city = $_.city
- #$zip = $_.zip
- $pwd = $password
- $upn = $sam + "@" + $intdomain
- $alias = $sam
- $HomeDirectory = "\\ServerName\Share\$wing\$room\$sam"
- $scriptpath = $script + ".bat"
- $date = Get-Date -Format g
- $UserCheck = Get-ADUser -Identity $sam -ErrorVariable uce -EA SilentlyContinue
- IF ($uce){Write-Host "New User!"}
- IF ($UserCheck.SamAccountName -contains $sam)
- {
- "User " + $upn + " already exists and added to log."
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "$name already exists. Updating expiredate to $expiredate"
- $script:countexists ++
- updateuser
- return
- }
- $pat = "^[a-zA-Z]+$"
- IF ($lname -notmatch $pat )
- {
- "Error: Invalid Characters found in name: " + $upn + ". Added to log."
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "$name has spacces or invalid characters"
- $script:countinvalid ++
- return
- }
- IF ($wing -eq "bork")
- {
- "User " + $upn + " has no class. Added to log. Class found is: " + $program
- Add-Content "CreateStudent_LOG_NoClass.txt" "$name assigned to class: $program"
- $script:countnoclass ++
- return
- }
- $script:countnew ++
- createuser
- }
- Function updateuser {
- IF ($UserCheck.DistinguishedName -notcontains $ou)
- {Get-ADUser -Identity $sam | Move-ADObject -TargetPath $ou}
- ELSE {Write "User in correct OU"}
- Enable-ADAccount -Identity $sam
- Set-ADUser -Verbose `
- -Identity $sam `
- -Server ADServer.domain.com `
- -AccountExpirationDate $expiredate `
- -Enabled $true `
- -Department $dept `
- -Description $desc `
- -HomeDirectory $HomeDirectory `
- -Office $room `
- -ScriptPath $scriptpath `
- -Title $title
- Add-ADGroupMember -Identity $room -Members $sam
- Add-ADGroupMember -Identity Students -Members $sam
- }
- Function createuser {
- "Creating " + $name + "."
- New-ADUser -Verbose `
- -Server ADServer.domain.com `
- -Name $name `
- -Path $ou `
- -SamAccountName $sam `
- -UserPrincipalName $upn `
- -AccountPassword (ConvertTo-SecureString "GenericPassword" -AsPlainText -Force) `
- -ChangePasswordAtLogon $true `
- -Enabled $true `
- -CannotChangePassword $false `
- -Company $company `
- -Department $dept `
- -Description $desc `
- -DisplayName $name `
- -GivenName $fname `
- -HomeDirectory $HomeDirectory `
- -HomeDrive "Z:" `
- -HomePage $website `
- -HomePhone $phone `
- -Office $room `
- -OfficePhone $phone `
- -PasswordNeverExpires $false `
- -ScriptPath $scriptpath `
- -City $city `
- -State $state `
- -StreetAddress $street `
- -Surname $lname `
- -Title $title `
- -AccountExpirationDate $expiredate
- #-Initials $minit `
- $date = Get-Date -Format g
- Add-Content "CreateStudent_LOG_Created.txt" `n$ou
- Add-Content "CreateStudent_LOG_Created.txt" "$name created as $sam."
- Add-Content "CreateStudent_newstudents.csv" "$fname,$lname,$program,$sam,$room"
- }
- Function enablemailbox {
- $fname = $_.fname
- $lname = $_.lname
- $name = $fname + " " + $lname
- $sam = $_.sam
- $room = $_.room
- Add-ADGroupMember -Identity $room -Members $sam
- Add-ADGroupMember -Identity Students -Members $sam
- Enable-Mailbox -Database $mbdb -DomainController ADServer.domain.com -Identity $sam
- }
- Function enablelync {
- $fname = $_.fname
- $lname = $_.lname
- $name = $fname + " " + $lname
- $sam = $_.sam
- ##########################################
- # Enable for lync and configure settings #
- ##########################################
- "Enabling " + $fname + " " + $lname + " for Lync"
- #$lineuri = "tel:+1" + $phone + ";ext=" + $ext
- Get-mailbox -identity $sam | Enable-CsUser -DomainController ADServer.domain.com -RegistrarPool $Registrar -SipAddressType EmailAddress -SipDomain $sipdomain
- ################################
- # Enable For Unified Messaging #
- ################################
- #"Configuring " + $fname + " " + $lname + " for Unified Messaging"
- #$sipuri = $fname + "." + $lname + "@" + $sipdomain
- #Get-Mailbox -identity $name | Enable-UMMailbox -PinExpired $true -UMMailboxPolicy $umpolicy -Extensions $ext -SIPResourceIdentifier $sipuri
- }
- $countpath= "\\Server\Share\Folder\ps_students.csv"
- $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
- $count=($importCSVVar).Count-1
- $count
- $countfull = $count
- $count = 1
- $powerschoolimport = Import-Csv "\\Server\Share\Folder\ps_students.csv" -Header fname,lname,program,status,sid | sort sid
- $powerschoolimport | ForEach-Object {
- Write-Host $count " of " $countfull
- $fname = $_.fname
- $lname = $_.lname
- $program = $_.program
- $status = $_.status
- $sid = $_.sid
- $name = $fname + " " + $lname
- # Remove unwanted characters
- $lname = $lname -replace $remspecchars, ""
- $fname = $fname -replace $remspecchars, ""
- $lname2 = $lname + "02"
- $lname3 = $lname + "03"
- $sam1 = $fname + "." + $lname
- $sam2 = $fname + "." + $lname2
- $sam3 = $fname + "." + $lname3
- $dupfile = Import-Csv "CreateStudent_dupcheck.csv" -Header fname2,lname2,program2,sid2,sam,name
- $dupcheck1 = $dupfile | where {$_.sam -eq $sam1}
- $dupcheck2 = $dupfile | where {$_.sam -eq $sam2}
- IF ($dupcheck2.sam -eq $sam2)
- {
- "DUPLICATE FOUND FOR " + $sam2 + "! Appending 03 to last name."
- Add-Content "CreateStudent_LOG_Duplicates.txt" `n$date
- Add-Content "CreateStudent_LOG_Duplicates.txt" "$name is a third duplicate."
- Add-Content "CreateStudent_LOG_Duplicates.txt" "Adjusted Login: $sam3"
- Add-Content "CreateStudent_LOG_Duplicates.txt" "Program: $program"
- Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname3,$program,$sid,$sam3,$name"
- $sam3
- }
- ELSEIF ($dupcheck1.sam -eq $sam1)
- {
- "DUPLICATE FOUND FOR " + $sam + "! Appending 02 to last name."
- Add-Content "CreateStudent_LOG_Duplicates.txt" `n$date
- Add-Content "CreateStudent_LOG_Duplicates.txt" "$name is a duplicate."
- Add-Content "CreateStudent_LOG_Duplicates.txt" "Adjusted Login: $sam2"
- Add-Content "CreateStudent_LOG_Duplicates.txt" "Program: $program"
- Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname2,$program,$sid,$sam2,$name"
- $sam2
- }
- ELSE
- {
- Add-Content "CreateStudent_dupcheck.csv" "$fname,$lname,$program,$sid,$sam1,$name"
- $sam1
- }
- $count ++
- Write-Host "------------------------------"
- }
- $countpath= "CreateStudent_dupcheck.csv"
- $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
- $count=($importCSVVar).Count-1
- $count
- $countfull = $count
- $count = 1
- Import-Csv "CreateStudent_dupcheck.csv" -Header fname2,lname2,program2,sid2,sam,name | ForEach-Object {
- Write-Host $count " of " $countfull
- dataimport
- $count ++
- Write-Host "------------------------------"
- }
- $countpath= "CreateStudent_newstudents.csv"
- $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
- $count=($importCSVVar).Count-1
- $count
- $countfull = $count
- $count = 1
- Import-Csv "CreateStudent_newstudents.csv" -Header fname,lname,program,sam,room | ForEach-Object {
- enablemailbox
- Write-Host $count " of " $countfull
- $count ++
- Write-Host "------------------------------"
- }
- $countpath= "CreateStudent_newstudents.csv"
- $importCSVVar=Import-Csv $countpath | Measure-Object | Select-Object
- $count=($importCSVVar).Count-1
- $count
- $countfull = $count
- $count = 1
- Import-Csv "CreateStudent_newstudents.csv" -Header fname,lname,program,sam | ForEach-Object {
- enablelync
- Write-Host $count " of " $countfull
- $count ++
- Write-Host "------------------------------"
- }
- Write-Host "------------------------------"
- Write-Host $countexists " users updated"
- Write-Host "------------------------------"
- Write-Host $countinvalid " users with invalid characters"
- Write-Host "------------------------------"
- Write-Host $countnoclass " users have no class assigned"
- Write-Host "------------------------------"
- Write-Host $countnew " new users created"
- Write-Host "------------------------------"
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "$countexists users updated Import Finished"
- Add-Content "CreateStudent_LOG_AlreadyExists.txt" "########################################################"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "$countinvalid users with invalid characters Import Finished"
- Add-Content "CreateStudent_LOG_InvalidCharacters.txt" "########################################################"
- Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
- Add-Content "CreateStudent_LOG_NoClass.txt" "$countnoclass users have no class assigned Import Finished"
- Add-Content "CreateStudent_LOG_NoClass.txt" "########################################################"
- Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
- Add-Content "CreateStudent_LOG_Created.txt" "$countnew new users created Import Finished"
- Add-Content "CreateStudent_LOG_Created.txt" "########################################################"
- IF ($countnew -gt 0)
- {
- $studentsnew = Get-Content -Path CreateStudent_LOG_Created.txt
- $studentsnoclass = Get-Content -Path CreateStudent_LOG_NoClass.txt
- Send-MailMessage -From UserAccountCreation<email@domain.com> `
- -Subject "Student Creation Script" `
- -To email@domain.com `
- -Attachments CreateStudent_LOG_Created.txt `
- -Body "List of new students created" `n `n `
- "$studentsnew" `n `n
- "$studentsnoclass"
- -SmtpServer email.domain.com
- }
- IF ($countnoclass -gt 0)
- {
- Send-MailMessage -From UserAccountCreation<email@domain.com> `
- -Subject "Student Creation Script" `
- -To studentupdater@domain.com `
- -Attachments CreateStudent_LOG_NoClass.txt `
- -Body "List of new studends that need a program assigned in Powerschool under custom screens/demographics" `
- -SmtpServer email.domain.com
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement